使用服务器端处理数据表jQuery导出表的所有数据

时间:2019-05-07 11:34:25

标签: jquery datatable export

我正在尝试通过使用服务器端处理将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');
    }
});

请帮助我。有什么问题,我该如何解决?

1 个答案:

答案 0 :(得分:0)

首先在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();
     }