ajax 通过按钮加载 DataTable 数据 单击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26717168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:26:47  来源:igfitidea点击:

Load DataTable data through button Click

ajaxdatatablepopulate

提问by Tjahid

I want to populate DataTable through a button click. Initially the dataTable should be empty:

我想通过单击按钮来填充 DataTable。最初 dataTable 应该是空的:

var searchText = $("#textBox").val();

    Table = $("#customerTable").dataTable({
        data:[],
        "columns": [
                    {"data": "Id"   },
                    { "data": "Name" },
                    { "data": "City" },
                    { "data": "Country" }
        ]        
        //"serverSide": true
    });

and the button click :

然后按钮单击:

$("#SearchButton").on("click", function (event) {

$.ajax({
            url: "/LoadCustomers",
            type: "post"
        });
Table.rows.add(result).draw();
});

回答by Tjahid

Solved !!!

解决了 !!!

My table looks like this :

我的桌子看起来像这样:

Table = $("#customerTable").DataTable({
    data:[],
    columns: [
                { "data": "CompanyId"  },
                { "data": "CompanyName" },
                { "data": "City" },
                { "data": "Country" }
    ],
    rowCallback: function (row, data) {},
    filter: false,
    info: false,
    ordering: false,
    processing: true,
    retrieve: true        
});

Button Click handler:

按钮点击处理程序:

$("#customerSearchButton").on("click", function (event) {
   $.ajax({
            url: "",
            type: "post",
            data: { searchText: searchText }
        }).done(function (result) {
            Table.clear().draw();
            Table.rows.add(result).draw();
            }).fail(function (jqXHR, textStatus, errorThrown) { 
                  // needs to implement if it fails
            });
}