datatables and server-side processing - load on demand

懵懂的女人 提交于 2019-12-13 14:19:58

问题


I've build simple table using DataTables and I successfully connected server-side script to populate my table with data.

Above my table I have 2 checkboxes and 2 date selects to specify filter criterias. When my page is loading I get table filled, because at startup datatable is pulling first page of data from server.

I would like to disable first initial data load, so when page is loaded I'll get empty table, then after I'll select criterias and press 'Load' button data will be loaded.

I know how to add my filter criterias to server params, what I need to do is to load data from server after user click button.

Below is my datatable script:

var myTable= $('table#myTable').dataTable({
    "table-layout": "fixed",
    "bJQueryUI": true,
    "sDom": '<"H"lpr>t<"F"ip>',
    "iDisplayLength": 25,
    "aLengthMenu": [[25, 50, 100, 500], [25, 50, 100, 500]],
    "bSort": false,
    "sPaginationType": "full_numbers",
    "bPaginate": true,
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "Data.asmx/Sales",
    "fnServerData": function(sSource, aoData, fnCallback) {
        var sEcho = aoData[0].value;
        var iDisplayStart = aoData[3].value;
        var iDisplayLength = aoData[4].value;

        $.ajax({
            contentType: "application/json; charset=utf-8",
            type: "POST",
            url: sSource,
            //below are my parameters
            data: "{'sEcho': '" + sEcho
                + "','iDisplayStart': '" + iDisplayStart
                + "','iDisplayLength': '" + iDisplayLength
                + "'}",
            success: function(msg) {
                fnCallback(msg.d);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.responseText);
            }
        });
    },
    "bAutoWidth": false,
    "aoColumns": [{
            "sType": "numeric",
            "mData": "Nr",
            "sWidth": "50px"
        }, {
            "sType": "string",
            "mData": "Name"
        }, {
            "sType": "string",
            "mData": "Surname"
        }]
});

回答1:


You simply specify the iDeferLoading parameter to datatables, with the number of records your table contains already in the DOM, or 0 if no records.

$(document).ready(function() {
$('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "scripts/server_processing.php",
    "iDeferLoading": 0
} );
} );



回答2:


Don't call the .dataTable() method until you want the table. Example below:

var myTable = null,
    dtOptions = {
        "table-layout": "fixed",
        "bJQueryUI": true,
        //...rest of options...
    };

$('#button').click(function (e) {
    myTable = $('table#myTable').dataTable(dtOptions);
});

or, if you don't need to access options after the table has been initialized:

$('#button').click(function (e) {
    myTable = $('table#myTable').dataTable({
        "table-layout": "fixed",
        "bJQueryUI": true,
        "sDom": '<"H"lpr>t<"F"ip>',
        "iDisplayLength": 25,
        "aLengthMenu": [[25, 50, 100, 500], [25, 50, 100, 500]],
        "bSort": false,
        "sPaginationType": "full_numbers",
        "bPaginate": true,
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "Data.asmx/Sales",
        "fnServerData": function(sSource, aoData, fnCallback) {
            var sEcho = aoData[0].value;
            var iDisplayStart = aoData[3].value;
            var iDisplayLength = aoData[4].value;

            $.ajax({
                contentType: "application/json; charset=utf-8",
                type: "POST",
                url: sSource,
                //below are my parameters
                data: "{'sEcho': '" + sEcho
                    + "','iDisplayStart': '" + iDisplayStart
                    + "','iDisplayLength': '" + iDisplayLength
                    + "'}",
                success: function(msg) {
                    fnCallback(msg.d);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.responseText);
                }
            });
        },
        "bAutoWidth": false,
        "aoColumns": [{
                "sType": "numeric",
                "mData": "Nr",
                "sWidth": "50px"
            }, {
                "sType": "string",
                "mData": "Name"
            }, {
                "sType": "string",
                "mData": "Surname"
            }]
    });
});


来源:https://stackoverflow.com/questions/16086090/datatables-and-server-side-processing-load-on-demand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!