无法使用search()API的自定义正则表达式实现stateSave

时间:2017-11-15 13:45:59

标签: javascript jquery datatables

我以下列方式启用了正则表达式过滤,因为默认值为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()的使用,但其下方的评论说这是一个草率的黑客,应该使用stateSaveAnother similar question没有得到答复。

那么,如何使用search() API的自定义实现保存搜索参数?

编辑 - Obfuscated fiddle,JavaScript完好无损但删除了所有JSP代码。

1 个答案:

答案 0 :(得分:0)

根据this answerPrashant的建议,我可以使用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;
});