4

我已经使用 DataTables 构建了简单的表,并且我成功连接了服务器端脚本以使用数据填充我的表。

在我的表格上方,我有 2 个复选框和 2 个日期选择来指定过滤条件。当我的页面加载时,我的表格被填满,因为在启动时数据表正在从服务器中提取第一页数据。

我想禁用第一次初始数据加载,所以当页面加载时我会得到空表,然后在我选择标准并按下“加载”按钮后,数据将被加载。

我知道如何将过滤条件添加到服务器参数,我需要做的是在用户单击按钮后从服务器加载数据。

下面是我的数据表脚本:

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

2 回答 2

7

您只需将 iDeferLoading 参数指定给数据表,其中包含您的表在 DOM 中已包含的记录数,如果没有记录,则为 0。

$(document).ready(function() {
$('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "scripts/server_processing.php",
    "iDeferLoading": 0
} );
} );
于 2014-04-30T09:54:22.643 回答
1

.dataTable()在需要表格之前不要调用该方法。下面的例子:

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

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

或者,如果您在表初始化后不需要访问选项:

$('#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"
            }]
    });
});
于 2013-04-18T15:10:06.883 回答