简体   繁体   中英

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.

I am assuming that by "save state" you actually mean save changes to a database ?

If so, what exactly do you wish to store in a database ?

By example:

The classic scenario is having a form that contains input elements such as a user profile or contact form etc..

What you usually do from the client, is bundling your form input data and sending it to the server using AJAX, then on the server side the form data is being processed and finally saved to a database (eg MySQL).

The original (and older) method is to have a submit button in your form, and make sure to set your form's "action" and "method" attributes - then when the submit button is clicked, the form data will be submitted to the path given in the "action" attribute which is a path on the server that should have server-side code for processing and storing the data in a database.

Hope it helps a bit

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM