我以下列方式启用了正则表达式过滤,因为默认值为false
-
$(document).ready(
function() {
var table = $('#companies').DataTable({
stateSave: true,
stateDuration: 0, //force the use of Local Storage
// other code for 'dom' & 'buttons' properties
});
table.search($(this).val(), true, true, true).draw();
});
但是,stateSave
以前在页面刷新时恢复搜索参数的工作正常已停止工作。它仍然记得对列进行排序,这使得我认为它不适用于搜索,因为正在使用外部实现正则表达式过滤器。
This answer提及state.loaded()
的使用,但其下方的评论说这是一个草率的黑客,应该使用stateSave
。 Another similar question没有得到答复。
那么,如何使用search()
API的自定义实现保存搜索参数?
编辑 - Obfuscated fiddle,JavaScript完好无损但删除了所有JSP代码。
答案 0 :(得分:0)
根据this answer和Prashant的建议,我可以使用state.loaded()
保存搜索字词并在重新加载时填充搜索框,如下所示 -
$(document).ready(
function() {
var table = $('#companies').DataTable({
stateSave: true,
stateDuration: 0, //force the use of Local Storage
// other code for 'dom' & 'buttons' properties
});
if (searchText !== null && searchText !== "") {
table.search(searchText.search, true, true, true).draw();
} else {
table.search($(this).val(), true, true, true).draw();
}
});
$(document).on('init.dt', function(e, settings) {
var api = new $.fn.dataTable.Api(settings);
var state = api.state.loaded();
searchText = state.search;
});