• 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();
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
  • 相关阅读:
    网页编程入门应该首先学些什么
    matlab数独运行不出来
    【MySQL架构篇】SQL执行流程与缓冲池
    Vue插值表达式及常用指令
    css滤镜
    RabbitMQ
    Linux与BL31之间添加SMC实现随机数获取
    Thymeleaf模板引擎
    由 mysql 中的一次慢查询,来看 order by 的文件排序和索引排序
    ssh 两次跳转,通过跳板机直接登录设备
  • 原文地址:https://blog.csdn.net/segclliwf/article/details/126407609