简体   繁体   English

Jquery Datatable 在数据库中保存状态而不是在客户端

[英]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 ?我需要在 DB 中保存数据表的状态。我想在按下时有一个保存状态按钮,我应该在保存状态按钮的点击事件中做什么? Which functions I have to override to send ajax request to server containing state of datatable?我必须覆盖哪些函数才能将 ajax 请求发送到包含数据表状态的服务器? 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).您通常在客户端所做的是将表单输入数据捆绑并使用 AJAX 将其发送到服务器,然后在服务器端处理表单数据并最终保存到数据库(例如 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.原始(和旧)方法是在您的表单中有一个提交按钮,并确保设置您的表单的“操作”和“方法”属性 - 然后当点击提交按钮时,表单数据将被提交到路径在“action”属性中给出,它是服务器上的路径,应该具有用于​​处理和将数据存储在数据库中的服务器端代码。

Hope it helps a bit希望它有点帮助

Use the stateSaveCallback callback使用stateSaveCallback回调

"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更多信息: https : //datatables.net/reference/option/stateSaveCallback

For loading use stateLoadCallback callback加载使用stateLoadCallback回调

 "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更多信息: https : //datatables.net/reference/option/stateLoadCallback

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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