Fox Fox - 1 year ago 1092
Ajax Question

How to export all rows from Datatables using Ajax?

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

The problem is that it only export the page that is currently displayed.

I am exporting like this:

buttons: [
extend: 'pdfHtml5',
text: 'PDF',
exportOptions: {
"columns": ':visible',

How can I export all rows?

Answer Source

According to DataTables documentation there is no way to export all rows when you are using server side:

Special note on server-side processing: When using DataTables in server-side processing mode (serverSide) the selector-modifier has very little effect on the rows selected since all processing (ordering, search etc) is performed at the server. Therefore, the only rows that exist on the client-side are those shown in the table at any one time, and the selector can only select those rows which are on the current page.

I worked this around by adding an 'ALL' parameter to the length menu and training end users to display all records before doing a PDF (or XLS) export:

var table = $('#example').DataTable({
    serverSide: true,
    ajax: "/your_ajax_url/",
    lengthMenu: [[25, 100, -1], [25, 100, "All"]],
    pageLength: 25,
    buttons: [
            extend: 'excel',
            text: '<span class="fa fa-file-excel-o"></span> Excel Export',
            exportOptions: {
                modifier: {
                    search: 'applied',
                    order: 'applied'
    // other options