jQuery Datatables Server Side分頁獲取 data的方式如何匯出所有記錄

// datables 的匯出、打印按鈕。action = exportAllRow
let buttons = [                
                {
                    className: 'btn btn-default',
                    extend: 'collection',
                    text: '匯出',
                    buttons: [
                        {
                            filename: function () {
                                return `VaccineList_${moment().format("YYYY-MM-DD HH:mm")}`;
                            },
                             extend: "excel",                    
                             action: exportAllRow,                      
                             extend: 'excelHtml5',
                             exportOptions: {
                                columns: [1, 2,3, 4, 5, 6, 7, 8, 9,10,11]
                            }
                        },
                        {
                            extend: 'print',
                            action: exportAllRow,   
                            exportOptions: {
                                columns: [1,2, 3, 4, 5, 6, 7, 8, 9,10,11]
                            }
                        }
                    ]
                },
                {
                    extend: 'colvis',
                    className: 'btn btn-default mr-2'
                }
            ];



function exportAllRow(e, dt, button, config) {
    // Call the original action function    
    let rowsAmt = dt.page.info().recordsDisplay;
    if (rowsAmt > 10000) {//所有数据的总量   
        if (!confirm(`匯出或列印的記錄數量較多( ${rowsAmt} 條記錄)

                           您確定繼續嗎?`)) {
            return;
        }
    }
    var self = this;
    var oldStart = dt.settings()[0]._iDisplayStart;
    dt.one('preXhr', function (e, s, data) {
        // Just this once, load all data from the server...               
        data.start = 0;
        data.length = 5000000;//note : int type, 最大值約21億。 該數會乘以上面  "ajax": $.fn.dataTable.pipeline   -->  "pages",所以要注意不要大于 int max number,否則超過會被重置為0.
        dt.one('preDraw', function (e, settings) {
            if (button[0].className.indexOf('buttons-copy') >= 0) {
                $.fn.dataTable.ext.buttons.copyHtml5.action.call(self, e, dt, button, config);
            } else if (button[0].className.indexOf('buttons-excel') >= 0) {
                $.fn.dataTable.ext.buttons.excelHtml5.available(dt, config) ?
                    $.fn.dataTable.ext.buttons.excelHtml5.action.call(self, e, dt, button, config) :
                    $.fn.dataTable.ext.buttons.excelFlash.action.call(self, e, dt, button, config);
            } else if (button[0].className.indexOf('buttons-csv') >= 0) {
                $.fn.dataTable.ext.buttons.csvHtml5.available(dt, config) ?
                    $.fn.dataTable.ext.buttons.csvHtml5.action.call(self, e, dt, button, config) :
                    $.fn.dataTable.ext.buttons.csvFlash.action.call(self, e, dt, button, config);
            } else if (button[0].className.indexOf('buttons-pdf') >= 0) {
                $.fn.dataTable.ext.buttons.pdfHtml5.available(dt, config) ?
                    $.fn.dataTable.ext.buttons.pdfHtml5.action.call(self, e, dt, button, config) :
                    $.fn.dataTable.ext.buttons.pdfFlash.action.call(self, e, dt, button, config);
            } else if (button[0].className.indexOf('buttons-print') >= 0) {
                setTimeout(function () { $.fn.dataTable.ext.buttons.print.action(e, dt, button, config); }, 0);
            }
            dt.one('preXhr', function (e, s, data) {
                // DataTables thinks the first item displayed is index 0, but we're not drawing that.
                // Set the property to what it was before exporting.                      
                settings._iDisplayStart = oldStart;
                data.start = oldStart;
            });
            // Reload the grid with the original page. Otherwise, API functions like table.cell(this) don't work properly.
            setTimeout(dt.ajax.reload, 0);
            // Prevent rendering of the full data to the DOM
            return false;
        });
    });
    // Requery the server with the new one-time export settings         
    dt.ajax.reload();
};

你可能感兴趣的:(前端,jquery,javascript,前端)