0

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.

5
  • what do you mean by "state of datatable" Commented Sep 13, 2016 at 5:18
  • @madalinivascu DataTables has the option of being able to save the state of a table (its paging position, ordering state etc) so that is can be restored when the user reloads a page, or comes back to the page after visiting a sub-page. Commented Sep 13, 2016 at 5:28
  • why do you want to save it to the db? Commented Sep 13, 2016 at 5:31
  • I want to save ordering state of datatable in DB . By default it is stored in local storage. For more details refer datatables.net/reference/option/stateSave Commented Sep 13, 2016 at 5:32
  • @madalinivascu If a user have changed some ordering of table it is stored in client side. If user login in different machine he will not be able to see those changes if state is saved on client side.So I want to save the satate on server side rather than client side Commented Sep 13, 2016 at 5:39

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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 (e.g 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

1 Comment

DataTables has the option of being able to save the state of a table (its paging position, ordering state etc) so that is can be restored when the user reloads a page, or comes back to the page after visiting a sub-page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.