himanshu dua himanshu dua - 1 year ago 211
Ajax Question

Jquery Datatable Save state in DB not in client side

I need to save state of datatable in DB.I want to have a save state button when it is pressed what should i do in on click event of save state button ? Which functions I have to override to send ajax request to server containing state of datatable? I also want to load from the saved state from DB.

Answer Source

Use the stateSaveCallback callback

"stateSaveCallback": function (settings, data) {
    // Send an Ajax request to the server with the state object
    $.ajax( {
      "url": "/state_save",
      "data": data,
      "dataType": "json",
      "type": "POST",
      "success": function () {}
    } );

for more info:https://datatables.net/reference/option/stateSaveCallback

For loading use stateLoadCallback callback

 "stateLoadCallback": function (settings) {
    var o;
    $.ajax( {
      "url": "/state_load",
      "async": false,
      "dataType": "json",
      "success": function (json) {
        o = json;
    } );

    return o;

more info: https://datatables.net/reference/option/stateLoadCallback

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download