保存/恢复DataTables中的搜索值 [英] Save / restore searched values in DataTables

查看:79
本文介绍了保存/恢复DataTables中的搜索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有多个搜索框的数据表,并启用了stateSave。该表确实会保存状态,但是在重新加载表时,搜索框不会保存。

I am using data tables with multiple search boxes, and stateSave enabled. The table will indeed save the state, but when reloading the table, the search boxes do not save.

例如,我有3列...书籍作者,书籍标题和书籍ISBN。我为每列提供了一个搜索框,以提高搜索能力。如果我在作者中搜索Mark,则会按预期显示作者中名为Mark的所有结果。刷新页面或执行任何其他操作会强制表重新加载(例如添加书籍),将显示已保存的搜索结果,但搜索框为空白。要让所有项目重新出现,我需要转到图书作者搜索框并按退格键。

For example, I have 3 columns... Book Author, Book Title, and Book ISBN. I have a search box for each column for greater searching power. If I search for "Mark" in author, all results with the name Mark in the author appear, as expected. Refreshing the page, or doing any other action which will force the table to reload (such as adding a book), the saved search results appear, but the search box is blank. To get all the items to reappear, I need to go to the book author search box and press backspace.

所以...我怎样才能保存searchByAuthor的状态文本框?

So... how can I save the state of the searchByAuthor text box?

或者......如何获取搜索到的值以及使用的列?如果我能得到,我可以重新填充正确的搜索框。

Or... how can I get the value that was searched for, and what column it was used on? If I can get that, I can repopulate the correct search box.

推荐答案

首先,请参阅此论坛帖子由该插件的作者说出

First of all, see this forum post by the author of the plugin who says


这是预期的。过滤输入不是DataTables所知的 - 您从它们调用列()。search()方法,但这可能来自任何地方),因此它不会't(并且不能)能够将值重新插入输入。

This is expected. The filtering inputs are not something DataTables has any knowledge of - you call the column().search() method from them, but that could be from anywhere), therefore it doesn't (and can't) have the ability to reinsert the values into the inputs.

但是,他也提供了解决方案,这是使用 state.loaded() API调用来获取保存状态的值,并从那里获取列搜索框中的值。您这样做的方式如下(这里是<$的链接 c $ c> state.loaded()此示例来自的文档):

However, he does also offer a solution, which is to use state.loaded() API call to get the value of the saved state, and from there you can obtain the values in the column search boxes. The way you would do that is as follows (here's a link to the state.loaded() documentation where this example is from):

$(document).on( 'init.dt', function ( e, settings ) {
    var api = new $.fn.dataTable.Api( settings );
    var state = api.state.loaded();
    var searchText = state.columns[/*columnNumber*/].search;
} );

然后,从那里,您可以使用 searchText 重新填充字段中的值。有关可以从 state 变量获取的数据的更多信息,请查看此链接州()文档。

Then, from there, you can use searchText to repopulate the value in the field. For more information on what data you can obtain from the state variable, check out this link to the state() documentation.

查看那些链接如果有任何不清楚的地方,我解释的大部分内容都是直接从他们那里获得的,并且会有更详细的解释。

Check out those links if anything is unclear, most of what I explained is obtained directly from them, and will be explained in greater detail.

这篇关于保存/恢复DataTables中的搜索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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