jQuery DataTables:延迟搜索,直到输入 3 个字符或单击按钮
有没有一个选项可以在输入 3 个字符后才开始搜索?
我为同事编写了一个显示 20,000 个条目的 PHP 脚本,他们抱怨说,当输入一个单词时,前几个字母会导致所有内容都冻结。
另一种方法是通过单击按钮而不是通过字符键入来启动搜索。
下面是我当前的代码:
$("#my_table").dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"bAutoWidth": false,
"aoColumns": [
/* qdatetime */ { "bSearchable": false },
/* id */ null,
/* name */ null,
/* category */ null,
/* appsversion */ null,
/* osversion */ null,
/* details */ { "bVisible": false },
/* devinfo */ { "bVisible": false, "bSortable": false }
],
"oLanguage": {
"sProcessing": "Wait please...",
"sZeroRecords": "No ids found.",
"sInfo": "Ids from _START_ to _END_ of _TOTAL_ total",
"sInfoEmpty": "Ids from 0 to 0 of 0 total",
"sInfoFiltered": "(filtered from _MAX_ total)",
"sInfoPostFix": "",
"sSearch": "Search:",
"sUrl": "",
"oPaginate": {
"sFirst": "<<",
"sLast": ">>",
"sNext": ">",
"sPrevious": "<"
},
"sLengthMenu": 'Display <select>' +
'<option value="10">10</option>' +
'<option value="20">20</option>' +
'<option value="50">50</option>' +
'<option value="100">100</option>' +
'<option value="-1">all</option>' +
'</select> ids'
}
} );
Is there please an option to start the search only after 3 characters have been typed in?
I have written a PHP-script for colleagues displaying 20,000 entries and they complain, that when typing a word, the first few letters cause everything to freeze.
An alternative would be to have the search to be started by a button clicked and not by character typing.
Below is my current code:
$("#my_table").dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"bAutoWidth": false,
"aoColumns": [
/* qdatetime */ { "bSearchable": false },
/* id */ null,
/* name */ null,
/* category */ null,
/* appsversion */ null,
/* osversion */ null,
/* details */ { "bVisible": false },
/* devinfo */ { "bVisible": false, "bSortable": false }
],
"oLanguage": {
"sProcessing": "Wait please...",
"sZeroRecords": "No ids found.",
"sInfo": "Ids from _START_ to _END_ of _TOTAL_ total",
"sInfoEmpty": "Ids from 0 to 0 of 0 total",
"sInfoFiltered": "(filtered from _MAX_ total)",
"sInfoPostFix": "",
"sSearch": "Search:",
"sUrl": "",
"oPaginate": {
"sFirst": "<<",
"sLast": ">>",
"sNext": ">",
"sPrevious": "<"
},
"sLengthMenu": 'Display <select>' +
'<option value="10">10</option>' +
'<option value="20">20</option>' +
'<option value="50">50</option>' +
'<option value="100">100</option>' +
'<option value="-1">all</option>' +
'</select> ids'
}
} );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
用这个
Use this
尽管它没有回答最初的问题,但我对数据表进行了复杂而缓慢的搜索。每次按键后都会触发过滤器事件,这意味着 10 个字符后会出现相当明显的延迟。因此,通过在触发过滤事件之前在按键后引入短暂的延迟,随后的按键会重置计数器并阻止之前的搜索,我能够使搜索看起来更快。其他人可能会发现这很有帮助。
我用斯托尼和克里斯蒂安·诺埃尔的答案来做到这一点:
although it does not answer the original question, i had a complex and slow search on my datatables. the filter event was firing after each keypress, which meant a quite noticeable delay after 10 characters. so by introducing a short delay after a keypress before the filter event was fired, where a subsequent keypress reset the counter and prevented the previous search, i was able to make the search seem much faster. others may find this helpful.
i used the answers from stony and christian noel to make this:
你可以通过这个延迟对服务器的ajax调用
如果按键之间的时间小于300毫秒,这段代码将停止ajax调用,这样当你写一个单词时,只有一个ajax调用会运行,并且只有当你停止打字。
您可以使用延迟参数(300)来“玩”以获得更多或更少的延迟
You can delay the ajax call to the server by this
This code will stop the ajax call if the time between to key press is less then 300 ms, in that way when you write a word, only one ajax call will run and only when you stop typing.
You can 'play' with the delay param ( the 300) in order to get more or less delay
对于 1.10 版本,请将此代码添加到选项中的 javascript 中。
initComplete 覆盖搜索方法并等待写入 3 个字符。
感谢 http://webteamalpha.com/triggering- datatables-to-search-only-on-enter-key-press/ 为我提供了线索。
for 1.10 version add this code to your javascript in the options.
The initComplete overrides the search method and wait to 3 characters are written.
Thanks to http://webteamalpha.com/triggering-datatables-to-search-only-on-enter-key-press/ for giving me the light.
您可能需要修改该插件。
不要使用 X 个字符,而是使用延迟,这样一旦他们停止输入 1 秒左右,搜索就会开始。
因此,当前触发搜索的 keydown/keyup 绑定将使用计时器进行修改......
You'll probably have to modify the plugin.
And instead of making it X characters, use a delay, so the search starts once they stopped typing for 1 second or so.
So the keydown/keyup binding which is currently triggering the search would be modified with a timer...
您可以使用 data.currentTarget.value.length 获取传入数据的长度,请参见下文。
显然,您希望在删除文本时运行此代码,因此将值设置为 0
You can get the length of the data that is being passed in using data.currentTarget.value.length, please see below.
and obviously you would want this code to run when removing text so set the value to 0
使用 API 并正确解除绑定“输入”的数据表 1.10.12 的修复版本。还添加了字符限制下的退格搜索清除功能。
Fixed version for datatables 1.10.12 using API and correctly unbinding the 'input'. Also added search clear on backspace under the character limit.
如果您使用的是旧版本,看起来就像这样。理查德的解决方案效果很好。
但是当我使用它时,我只是添加了新事件,而不是删除。因为当代码运行时,表还没有创建。
所以我发现有 fnInitComplete 方法(创建表时触发),并将其应用于 Ricard 的解决方案。
这里是
If you're using the old version, it looks like it. Richard's solution works fine.
But when I use it, I just added new events, not deleting. Because when code run, table is not yet created.
So I found that there is the fnInitComplete method (fire when table created) and I applied it to Ricard's solution.
Here it is
您可以编写自己的函数来测试附加到 onKeyUp 事件处理程序的输入字符串的长度,并在达到最小长度后触发搜索函数吗?
大致如下:
...也就是说,以伪代码的方式,但你明白了要点。
Can you write your own function to test for the length of the inputed string attached to an onKeyUp event handler and trigger the search function once the min length has been reached?
Something along the lines of:
...that is, in a pseudo code kind of way but you get the jist.
您可以使用名称 minlength 参数来将搜索限制为 3 个字符:
You can use the parameter by name minlength in order to restrict the search until 3 characters:
这里的大多数答案都以某种方式操纵现有的 DataTable 事件绑定,但就我个人而言,在花费了太长时间试图使其工作后,我最终发现最好的方法是在
search< 中发送一个虚拟值ajax 调用期间的 /code> 参数。
##EmptySearch##
字符串只是充当一个不应该与任何返回的数据匹配的值(使用什么完全是个人喜好,但它应该是一个保证不与任何数据匹配的字符串)。由于尚未对绑定进行操作,所以所有常用功能仍然有效,但在搜索大于或等于三个字符之前不会返回任何有意义的内容。诚然,这并不理想,宁愿根本不发出服务器请求,但这是(在我看来)最简单的方法,不会破坏数据表搜索的现有功能。Most of the answers here in some way manipulate the existing DataTable event bindings but personally after spending far too long trying to get this working the best approach I found, in the end, was to just send a dummy value in the
search
parameter during the ajax call.The
##EmptySearch##
string simply acts as a value that shouldn't match any returned data (it's entirely personal preference what to use, but it should be a string that is guaranteed to not match any data). Because the bindings have not been manipulated all the usual bells and whistles still work but nothing meaningful is returned until the search is greater than or equal to three characters. Admittedly it isn't ideal, would prefer to not make the server request at all but this is (in my opinion) the simplest approach that doesn't ruin the existing functionality of the DataTable search.需要修改jquery.datatables.js
-----更新
当然,您可以检查长度> 3、但我觉得你还是需要一个定时器。如果您有大量数据,您不希望在每次角色更新后继续对其进行过滤。
在此方法中:
向 keyup 添加一个计时器,如答案之一所示。
然后访问这个网站 http://jscompress.com/
并输入修改后的代码,js 将被缩小。
You need to modify the jquery.datatables.js
----- updated
ofcourse you can do a check on lenght > 3, but I think you still need a timer. if you have a lot of data, you don't want to keep getting it filtered after every character update.
Within this method:
Add a timer to the keyup, like shown in one of the answers.
Then go to this site http://jscompress.com/
And past your modified code and the js wil get minified.
您可以在 Medtronic 数据表或其他代码上使用此代码在使用 3 个字符后进行搜索:
searchInput.length() ==0 首次显示。
You can use this code on Medtronic datatable or other code to search after using 3 character :
searchInput.length() ==0 for first show.
这适用于 DataTables 版本 1.10.19。它只需要在您的网站模板中包含 js - 对于在不同页面上配置了多个数据表的网站非常有用。对于任何缓慢的 xhr 加载表也很有用,在所有当前运行完成之前不会允许任何新的 xhr 请求。使用的搜索功能与插件设置 搜索功能本来。
This works with DataTables version 1.10.19. It only requires including the js in your website template - useful for a site that has multiple dataTables configured on different pages. Also useful for any slow xhr loading tables, will not allow any new xhr requests until all currently running finish. The search function used is very similar to how the plugin sets up the search function originally.
您是否有理由不只检查“更改”的长度?
Is there a reason you wouldn't just check length on 'change'?
版本 1.10 的解决方案 -
在这里寻找完整的答案但没有找到答案后,我写了这个(利用文档中的代码,以及这里的一些答案)。
下面的代码可以延迟搜索,直到输入至少 3 个字符:
Solution for version 1.10 -
After looking here for a complete answer and not finding one, I've written this (utilizing code from the documentation, and a few answers here).
The below code works to delay searching until at least 3 characters are entered:
注意:这是针对早期版本的数据表,请参阅 jQuery 数据表 v1.10 的此答案
这将修改输入框的行为,使其仅在按下回车键或搜索中至少有 3 个字符时进行过滤:
您可以在此处看到它的工作原理:http://jsbin.com/umuvu4/2。我不知道为什么 dataTables 人员同时绑定到 keypress 和 keyup,但我会重写它们以保持兼容,尽管我认为 keyup 就足够了。
希望这有帮助!
Note: This was for a much earlier version of data tables, please see this answer for jQuery datatables v1.10 and above.
This will modify the behaviour of the input box to only filter when either return has been pressed or there are at least 3 characters in the search:
You can see it working here: http://jsbin.com/umuvu4/2. I don't know why the dataTables folks are binding to both keypress and keyup, but I'm overriding both of them to stay compatible although I think keyup is sufficient.
Hope this helps!
为什么不尝试 Stony 答案的这个扩展版本:)
这会延迟搜索,直到用户停止打字。
希望有帮助。
Why not try this extended version of Stony's answer :)
This will delay the search until the user has stopped typing.
Hope it helps.
这是1.10版本中api更改的处理方法
Here is how to handle it with the api change in version 1.10
我的数据表版本 1.10.10
我改变了一些东西,现在它可以工作了。所以,我正在分享,因为很难使其适用于 1.10.10 版本。感谢 cale_b、Stony 和 Sam Barnes。查看代码看看我做了什么。
My version of datatables 1.10.10
I changed a little things and it works now. So, i'm sharing, cause it was difficulty to make it work for version 1.10.10. Thanks to cale_b, Stony and Sam Barnes. Look at the code to see what i did.
这适用于 DataTables 1.10.4:
JSFiddle
This works on DataTables 1.10.4:
JSFiddle
要在用户在搜索框中键入最少字符后调用服务器调用,您可以遵循 艾伦的建议:
因此,对于至少 3 个字符,只需更改 插件:
To invoke the server call after the user has typed the mininum characters in the search box, you can follow Allan's suggestion:
So for a minimum of 3 characters, just change line #19 in the plug-in to:
这是一个扩展数据表的类似插件的脚本。
用法:
Here's a plugin-like script that extends datatables.
usage:
以前的解决方案都不适合我,所以我制作了这个修改版本,还添加了去抖功能。与所有最新版本完美配合。
您可以简单地更改或删除最小字符限制和去抖超时值。
None of the previous solutions worked for me so I have made this modified version that also adds a debounce. Working perfectly with all the latest versions.
You can simply change or remove the min character limit and debounce timeout value.