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();
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值