使用jquery dataTables TableTools导出所有表数据

时间:2014-12-21 12:23:15

标签: jquery-datatables tabletools

我正在使用jquery datatable进行服务器端处理。我的数据表代码如下:

$('#DataGrid').dataTable({
    destroy: true,
    "processing": true,
    searching: false,
    serverSide: true,
    "scrollX": true,
    "bLengthChange": false,
    "iDisplayLength": pageSize,
    "bInfo": true,
    //stateSave: true,
    order: [
        [0, "desc"]
    ],
    "aoColumnDefs": [{
        'bSortable': false,
        'aTargets': [(lastColumn - 1)]
    }],
    "dom": 'T<"clear">lfrtip',
    "tableTools": {
        "aButtons": [
            "copy",
            "csv", "xls", "pdf"
        ],
        "sSwfPath": $("body").attr("data-project-root") + "Content/TableTools-2.2.3/swf/copy_csv_xls_pdf.swf"

    },
    ajax: {
        url: 'StudentProgramListForIdCardResult',
        type: 'POST',
        data: function(d) {
            d.programId = programId;
            d.sessionId = sessionId;
            d.branchId = branchId;
            d.campusId = campusId;
            d.batchName = batchName;
            d.course = course;
            if ($('#paymentStatus').val() > 0) {
                d.paymentStatus = $('#paymentStatus').val();
            } else {
                d.paymentStatus = paymentStatus;
            }
            if ($('#imageStatus').val() > 0) {
                d.imageStatus = $('#imageStatus').val();
                $('#imageStatus').val();
            } else {

                d.imageStatus = imageStatus;

            }
            if ($('#printingStatus').val() > 0) {
                d.printingStatus = $('#printingStatus').val();
            } else {
                d.printingStatus = printingStatus;
            }
            d.informationViewList = informationViewList;
            d.batchDays = batchDays;
            d.batchTime = batchTime;
        }
    }
});

但是当我导出数据时,TableTools正在导出当前页面中的数据。它没有加载表格中的所有数据。

1 个答案:

答案 0 :(得分:1)

dataTables插件非常棒,但是底层的表/表行仍然存在于DOM中,并且可以像遍历任何HTML表一样遍历:

//foo will be a nodeList of all the tr's in the table
var foo = document.getElementById('#DataGrid').querySelectorAll('tr');

var i = 0, string = '', len = foo.length, row, cells;

for (;i<len; ++i) {
    row = foo[i];
    cells = row.children; //td's
    string += 'however you want to package it for your ajax';
}

$.post({url: 'myServerScript', {data: string}, callback});

我无法轻易找到有关插件实现的适当导出的任何文档,所有导出示例似乎都集中在保存到本地驱动器的excel / csv上。