JQuery1.8点击按钮可以保存状态 [英] JQuery1.8 Datatables saving state on click of a button

查看:331
本文介绍了JQuery1.8点击按钮可以保存状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jsp,一个表和两个按钮home和back,并在其中导入了jquery.datatable.js。



点击的家页面应该加载,不需要任何分页或过滤器保存。并且点击返回表格应该保存所有的过滤器和分页。



在初始化数据表时,我已经尝试设置bStateSave标志为true。这总是加载带有过滤器和分页的表。



有没有办法我可以加载表保存状态点击返回按钮及其原始状态点击首页按钮

解决方案

对于DataTable 1.10:



以下是与状态保存相关的DataTables 1.10的 API函数


state()

获取最后保存的表的状态



state.clear()

清除表的保存状态。



state.loaded()

获取表状态在初始化期间加载。



state.save()

触发状态保存。 p>

您需要具有 stateSave:true 初始化选项才能启用状态保存。然后,当用户单击Home按钮时,可以调用 $('#example)。DataTable()。state.clear()来清除保存的状态。



对于DataTables 1.9及更早版本:



不幸的是没有直接的API方法清除保存的状态。



您需要具有 bStateSave:true 初始化选项才能启用状态保存。



选项1:使用document.cookie:



清除包含状态数据的cookie。
当用户单击主页按钮时,需要执行以下代码。 重要信息:示例替换为您的表格ID。

  //重置当前状态
document.cookie = name +'SpryMedia_DataTables_'+'example'+'=; expires = Thu,01 Jan 1970 00:00:01 GMT;';

选项2:使用fnStateLoadParams:



或者,您可能希望使用 fnStateLoadParams 回调函数来确定何时加载或忽略状态数据。通过将 true false 分配给来确定页面加载是否应该加载或忽略状态loadStateParams 变量:

  $(document).ready(function(){
$ ('#example')。dataTable({
bStateSave:true,
fnStateLoadParams:function(oSettings,oData){
//通过返回false禁止状态加载
var loadStateParams = false;

return loadStateParams;
}
});
});


I have a jsp with a table and two buttons "home" and "back" in it and have imported jquery.datatable.js in it.

On click of "home" the page should load without any pagination or filter saved. and on click of "back" the table should have all the filter and pagination saved.

I have tried setting the "bStateSave" flag true while initializing the data table. This always loads the table with the filter and pagination.

Is there any way I can load the table with saved state on click of "back" button and its original state on click of "home" button

解决方案

For DataTables 1.10:

Below are API functions for DataTables 1.10 related to state saving:

state()
Get the last saved state of the table

state.clear()
Clear the saved state of the table.

state.loaded()
Get the table state that was loaded during initialisation.

state.save()
Trigger a state save.

You need to have stateSave: true initialization option to enable state saving. Then, when user clicks the "Home" button, you can call $('#example').DataTable().state.clear() to clear the saved state.

For DataTables 1.9 and earlier:

Unfortunately there is no direct API method to clear saved state.

You need to have bStateSave: true initialization option to enable state saving.

Option 1: Using document.cookie:

Below is a hack intended to clear cookie that contains state data. When user clicks the "Home" button, you need to execute the code below. IMPORTANT: Replace example with your table ID.

// Reset current state
document.cookie = name + 'SpryMedia_DataTables_' + 'example' + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';

Option 2: Using fnStateLoadParams:

Alternatively, you may want to use fnStateLoadParams callback function to determine when to load or ignore state data. Determine on page load whether state should be loaded or ignored by assigning true or false respectively to loadStateParams variable:

$(document).ready( function() {
  $('#example').dataTable( {
    "bStateSave": true,
    "fnStateLoadParams": function (oSettings, oData) {
       // Disallow state loading by returning false  
       var loadStateParams = false;

       return loadStateParams;
    }
  });
});

这篇关于JQuery1.8点击按钮可以保存状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆