【问题标题】:Export All data of table using server side processing datatable jquery使用服务器端处理数据表jquery导出表的所有数据
【发布时间】:2019-05-07 11:34:24
【问题描述】:

我正在尝试通过使用服务器端处理将 HTML5 导出按钮集成到数据表中。数据正在导出,但只有那些在数据表中可见的数据。我想要的只是导出所有数据。我的代码如下。我是新手,谁能帮帮我。

jQuery("#table_data").dataTable({
    'processing': true,
    'serverSide': true,
   "scrollX":true,
    'bSearchable': true,
    lengthMenu: [[10, 25, 100, -1], [10, 25, 100, "All"]],
    "ajax": {
        'type': 'POST',
        "url": ajax_object.ajaxurl,
        "dataType": "json",
        "data": {action: 'ajax_datatable', id: form_id}
    },
   "dom": 'Bflrtip',
    buttons: [
        {
            extend: 'csvHtml5',
            exportOptions: {
                columns: ':visible',
                modifier : {
                    // DataTables core 
                    page : 'all'    
                }                                
            }

        },
        {
            extend: 'pdfHtml5',
            exportOptions: {
                columns: ':visible',
                modifier : {
                    // DataTables core 
                    page : 'all'    
                } 
            },
            orientation: 'landscape',
            PdfSize: "A3"

        }],
        "order": [[ data.dt_column_order, "desc" ]],
    "columnDefs": [{
            "targets": [parseInt(data.dt_column_target)],
            "visible": false,
            "searchable": false
        }],
    "oLanguage": {
        "sProcessing": "<div></div><div></div><div></div><div></div><div></div>"
    },
    "fnPreDrawCallback": function (oSettings) {
        jQuery('#table_data').css('opacity', '0.2');
    },
    "fnDrawCallback": function () {
        jQuery('#table_data').css('opacity', '1');
    }
});

请帮助我。出了什么问题,我该如何解决这个问题?

【问题讨论】:

    标签: jquery datatable export


    【解决方案1】:

    首先在DataTable中添加如下代码

    "dom": 'Blfrtip',
                        "buttons": [
                            {
                                "extend": 'excel',
                                "text": '<button class="btn"><i class="fa fa-file-excel-o" style="color: green;"></i>  Excel</button>',
                                "titleAttr": 'Excel',
                                "action": newexportaction
                            },
                        ],
    

    然后在$(document).ready()函数里面加入这个函数

    function newexportaction(e, dt, button, config) {
             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 = 2147483647;
                 dt.one('preDraw', function (e, settings) {
                     // Call the original action function
                     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) {
                         $.fn.dataTable.ext.buttons.print.action(e, dt, button, config);
                     }
                     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();
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多