jquerydatatablesdatatable-buttons

Datatable Buttons - CSV export issue not taking cell value


I am having an issue where the csv export is not exporting the value set using fnRowCallback, I have tried looking in to orthogonal but does not seem like it wants to work.

My button code is as follows:

buttons: [
       'selectNone',
       'selectAll',
       {
           extend: 'csv',
           title: 'Data export',
           exportOptions: {
               columns: colArr, rows: { selected: true }
           }
       }],

Am generating columns dynamically on page load as this is not fixed:

 var Columns = [
    { "data": "Name" },
    { "data": "EmployeeNum", "sClass": "text-center" },
    { "data": "DateStartedS", "sClass": "text-center" },
    { "data": "DateCompletedS", "sClass": "text-center" },

];

for (var a = 0; a < QuestionLength; a++) {
    Columns.push({ "data": "ID", "sClass": "text-center" });
}

Columns.push({ "data": "ID", "sClass": "text-center" });

then once that is complete I use fnRowCallback to get the value from a method:

fnRowCallback: function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {

        var QuestionColumns = $("#tblPollsReport thead td.trquestion");;

        $.each(QuestionColumns, function (idx, item) {

            var QuestionID = $(item).attr("data-questionid");

            var Result = FindAsnwer(QuestionID, aData["QuestionAsnwers"]);
            debugger;
            var QNum = idx + 4;
            //$('td:eq(' + QNum + ')', nRow).val("Hallo");
            $('td:eq(' + QNum + ')', nRow).html(Result);
        })
        //Actions
        var ActoinHtml = '<div class="btn-group">';
        ActoinHtml += '<button onclick="DeletePollUser(' + aData["ID"] + ')" title="Delete" class="btn btn-xs btn-danger js-tooltip"><i class="fa fa-times"></i></button>';
        ActoinHtml += '</div>';

        var ActionCol = (4 + QuestionLength);

        $('td:eq(' + ActionCol + ')', nRow).html(ActoinHtml);
    },

However once I export it is using the original value that I added when adding to columns and not the value I set in fnRowCallback.

Any help would be appreciated.


Solution

  • OK I managed at the end

    I modified the columns section to contain this:

    for (var a = 0; a < QuestionLength; a++) {
        Columns.push({
            "data": "ID", "sClass": "text-center", render: function (data, type, row, colinfo) {
    
                if (type === 'export') {
                  //Any logic you would like to overide with
                } else {
                    return data;
                }
    
            }
        });
    }