How to export all rows from Datatables using Ajax?

前端 未结 12 1146
广开言路
广开言路 2020-11-29 23:38

I am using new feature in Datatables: \"HTML5 export buttons\". I am loading data with Ajax.

https://datatables.net/extensions/buttons/examples/html5/simple.html

12条回答
  •  有刺的猬
    2020-11-30 00:09

    This button definition worked for me in a scrolled table (instead of paging):

    {
      text: 'PDF',
      action: function(e, dt, button, config) {
        dt.one('preXhr', function(e, s, data) {
          data.length = -1;
        }).one('draw', function(e, settings, json, xhr) {
          var pdfButtonConfig = $.fn.DataTable.ext.buttons.pdfHtml5;
          var addOptions = { exportOptions: { "columns" : ":visible" }};
    
          $.extend(true,pdfButtonConfig,addOptions);
          pdfButtonConfig.action(e, dt, button, pdfButtonConfig);
        }).draw();
      }
    }

    It will force the DataTable to request all rows for the current filtering for one request. Then it calls the desired action of the Export button directly. The variable addOptions can be used to alter the standard configuration of the export button.

    You might run into problems though if you have a lot of rows as they are all loaded into the DOM.

提交回复
热议问题