How to Keep table settings after page postback usi

2019-07-30 22:56发布

问题:

I'm actually using jquery dataTables pluging on a GridView for searching purposes as well as other great settings. If I go to the page number 2 and my page does postgback clicking any server object, I lose the page where I was as well as the number of entries selected, means the table just get the initial settings again. So my question is, is it possible to keep the settings of my table (GridView) after the page does Pastback, either getting my current page index of the table and entries number before postback and after postback set those settings to the table again? I tried but nothing seems to be working well, the table just go to default settings always. I tried this way:

$(document).ready(function () {
    var tableScopeitems = $('#gridScopeItem').DataTable({
        "sPaginationType": "full_numbers",
        "lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
        "iDisplayLength": 100
    });
    tableScopeitems.ajax.reload(null, false);
});

回答1:

I believe it was

"stateSave": true,
"stateDuration": 60 * 10,

This creates a cookie for the DataTable so when the page is reloaded or revisited it has the correct sorting and page number. And with stateDuration you can set the expiration time of those cookies. In this example it's 10 minutes.

More info:

  • https://datatables.net/reference/option/stateSave
  • https://datatables.net/reference/option/stateDuration