简体   繁体   English

数据表 - 回调后仅保留选定的页码(不排序或搜索或过滤)并在刷新时清除

[英]Datatables - Keeping only selected page number(not order or search or filter) after callback and clear on refresh

I have a data table which I should change something on it, for example, I want to change the title of the content, but this content is on the 6th page of the table.我有一个数据表,我应该对其进行更改,例如,我想更改内容的标题,但该内容位于表的第 6 页。 When I change it, the data table refreshes itself to the 1st page.当我更改它时,数据表会自行刷新到第一页。 What I'm trying to do is to keep the selected page number and call it back after refresh.我要做的是保留选定的页码并在刷新后将其回调。 I have tried all solutions but I just want to save page number nothing else.我已经尝试了所有解决方案,但我只想保存页码。 Is that possible?那可能吗?

<script type="text/javascript">
        $(function () {
        var dataTableoOBJ = $("#forumList").dataTable({
        "lengthChange": true,
        "processing": true,
        "serverSide": true,
        "ajax": "/admin/forums/data",
        "createdRow": function (row, data, rowIndex) {
         $.each($('td', row), function (colIndex) {
         $(this).attr('data-id', data.id);
         });
        },
        'columns': [{...}]
        });
        });
</script>

I have also tried我也试过

"saveState": true and for clearing state on refresh/reload "saveState": true并且用于在刷新/重新加载时清除 state

if (performance.navigation.type == 1){
   var table = $('#forumList').DataTable();
   table.state.clear();
   table.ajax.reload();
} 

but it's only clearing page numbers is there any way to clear sorting/ordering/searching/filtering?但它只是清除页码有没有办法清除排序/排序/搜索/过滤?

I've got 2 solutions for this problem.对于这个问题,我有 2 个解决方案。

Solution 1:解决方案1:

Add these fields to the data table将这些字段添加到数据表中

 "stateSave": true,
 stateLoadParams: function( settings, data ) {
      var a = data.order;
      if(data.search.search !== '' || a[0][0] !== 0 || data.length !== 10) {
           delete data.order;
           delete data.search;
           delete data.length;
           delete data.start;
           window.location.reload();
       }
       if (data.start === 0) {
           delete data.order;
           delete data.search;
           delete data.length;
           delete data.start;
       }
  }

And required actions for refreshing the page以及刷新页面所需的操作

if (performance.navigation.type == 1){
     var table = $('#forumList').DataTable();
     table.state.clear();
     table.ajax.reload();
}

Solution 2:解决方案2:

Add "stateSave": true field to the data table"stateSave": true字段添加到数据表中

And required actions for refreshing the page以及刷新页面所需的操作

if (performance.navigation.type == 1){
  var table = $('#forumList').DataTable();
  table.state.clear();
  table.search("").draw(); 
  table.column( '0:visible' ).order( 'asc' ).draw();
  table.page.len(10).draw();
}

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

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