数据表按钮 - CSV导出问题不占用单元格值

时间:2018-03-21 20:56:00

标签: jquery datatables datatable-buttons

我遇到的问题是csv导出没有使用fnRowCallback导出值集,我试图查找正交但似乎不想它工作。

我的按钮代码如下:

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

在页面加载时动态生成列,因为这不是固定的:

 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" });

然后一旦完成,我使用fnRowCallback从方法中获取值:

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);
    },

然而,一旦我导出它就是使用我在添加到列时添加的原始值而不是我在fnRowCallback中设置的值。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

好的,我最后管理了

我修改了列部分以包含此内容:

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;
            }

        }
    });
}