9

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

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();
});
Tjahid
  • 249
  • 1
  • 2
  • 7

1 Answers1

14

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
            });
}
Laurence
  • 58,936
  • 21
  • 171
  • 212
Tjahid
  • 249
  • 1
  • 2
  • 7
  • Table is undefined is showing under the Ajax response – CyberAbhay May 30 '18 at 14:07
  • @CyberAbhay define on the top of javascript code var Table; – iWizard Nov 27 '18 at 09:55
  • I User version 1.10.20 and throw this :ExtJS:197 Uncaught TypeError: myDataTable.clear is not a function at Object.success (ExtJS:197) at c (VM15 jquery.min.js:2) at Object.fireWith [as resolveWith] (VM15 jquery.min.js:2) at l (VM15 jquery.min.js:2) at XMLHttpRequest. (VM15 jquery.min.js:2) – toha Dec 21 '21 at 07:14