How to do custom filtering with Datatables and server-side processing

前端 未结 5 1706
面向向阳花
面向向阳花 2021-02-02 17:30

I am using Datatables to display tabular data in my Web application, and have configured it to make use of server-side processing, i.e. query the server via AJAX for filtered da

5条回答
  •  清酒与你
    2021-02-02 18:05

    This answer is updated for version 1.10.6

    This is now can be done using the ajax option.

    Sample code

     $table.dataTable({
                "ajax":  {
                    "url": "example.com/GetData",
                    "type": "POST",
                    "data": function(d) {
                        d.Foo = "bar";
                        d.Bar = "foo";
                        d.FooBar = "foobarz";
                    }
                },
                "serverSide":true,
            });
    

    Foo, Bar and FooBar will be posted as Form Data as additional parameters along with other existing ones like draw, start, length, etc so depending on your server side language you can read them accordingly.

    In a real life scenerio, you would probably have a Search button and some input, you can trigger the filtering process by calling

            var table = $table.DataTable();
            table.ajax.reload(); 
    

    when the button is clicked.

提交回复
热议问题