Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export all table data using jquery dataTables TableTools

I am doing server side processing using jquery datatable.My datatable code is as below:

$('#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;
        }
    }
});

But when I export data, TableTools is exporting the data in current page. It is not loading all data in the table.

like image 257
sajju217 Avatar asked Dec 21 '14 12:12

sajju217


1 Answers

The dataTables plugin is great and all, but the underlying table/table rows are still there in the DOM and can be traversed the same way you'd traverse any HTML table:

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

I was unable to easily find any documentation on a plugin-implemented appropriate export, all the export examples seem to focus on excel/csv saved to the local drive.

like image 52
Jared Smith Avatar answered Nov 12 '22 00:11

Jared Smith