How to export all rows from Datatables using Ajax?

前端 未结 12 1185
广开言路
广开言路 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-29 23:58

    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: ' Excel Export',
                exportOptions: {
                    modifier: {
                        search: 'applied',
                        order: 'applied'
                    }
                }
            }
        ],
        // other options
    });
    

提交回复
热议问题