简体   繁体   English

数据表和服务器端处理 - 按需加载

[英]datatables and server-side processing - load on demand

I've build simple table using DataTables and I successfully connected server-side script to populate my table with data. 我使用DataTables构建了简单的表,并且我成功连接了服务器端脚本以使用数据填充我的表。

Above my table I have 2 checkboxes and 2 date selects to specify filter criterias. 在我的表格上方,我有2个复选框和2个日期选择来指定过滤标准。 When my page is loading I get table filled, because at startup datatable is pulling first page of data from server. 当我的页面加载时,我得到表填充,因为在启动时,datatable正在从服务器中提取第一页数据。

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"
        }]
});

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. 您只需将iDeferLoading参数指定给datatables,您的表已包含在DOM中的记录数,如果没有记录,则为0。

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

Don't call the .dataTable() method until you want the table. 在您需要表之前,请不要调用.dataTable()方法。 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"
            }]
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM