使用 JavaScript 和 JQuery 数据表缓冲/延迟 ajax 请求/击键
我目前正在制作一个企业网络应用程序的原型,其中将包括一个快速搜索屏幕。 此屏幕使用 JQuery 和 Datatables 提供搜索结果的表格视图。根据与文本字段中输入的字符串的匹配来执行搜索。每次按下按键时,都会将新的 AJAX 请求发送回 servlet,执行搜索(跨大型数据库),并通过 JSON 返回结果以在表上更新。
对数据库的搜索是一项成本高昂的操作。很多时候,当用户输入姓名时,他们会快速连续输入多个字母。这将导致后端触发多次搜索,有时是不必要的。
我想知道是否有一种方法可以巧妙地缓冲这些请求。例如,如果用户正在键入“Jonathan”,则不要发送对 J、Jo、Jona、Jonath... 的请求(如果它们都在彼此的时间限制内键入),并且仅在存在时才发送请求打字时有足够的间隙,即当键入末尾的字母 n 时。
我知道这会稍微延迟,但这可能是一个值得的妥协。
你觉得这个说法有道理吗?是否有此类操作的现有模式/库?它是否会导致我没有想到的可用性问题?
非常感谢您的意见!
I am currently prototyping an enterprise web app which will include a quick search screen.
This screen uses JQuery and Datatables to provide a tabular view of the search results. Searches are performed based on matches to string entered in a text field. Each time a key is pressed, a new AJAX request is sent back to a servlet, the search is performed (across a large database) and results are returned via JSON to be updated on the table.
The search on the database is a costly operation. A lot of times when a user is typing, say, a name, they would type a number of letters in quick succession. This will result in multiple searches being triggered in the backend, sometimes unnecessarily.
I was wondering if there is a way of cleverly buffering those request. For example, if a user is typing "Jonathan", do not send a request for J, Jo, Jona, Jonath, ... if they are all typed within a time limit of each other, and only send the request when there is a sufficient gap in typing, i.e. when the letter n at the end is typed.
I know this will cost a slight delay but it may be a worthy compromise.
Do you think think this makes sense? Is there an existing pattern/library for such operations? Does it cause usability issues that I haven't thought of?
Greatly appreciate your opinions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以防万一有人看到类似的问题:在 Spike 的评论之后,我检查了数据表插件,发现有一些东西完全符合我上面的要求:请参阅 fnSetFilteringDelay
Just in case someone looking at a similar problem: after Spike's comment I checked datatables plugins and there's something that does exactly what I wanted above: see fnSetFilteringDelay
您可以使用 jquery-ui 框架的自动完成功能,它有一个延迟选项,可以完全满足您的需求。
http://jqueryui.com/demos/autocomplete/#option-delay
You could use the jquery-ui framework's autocomplete function, which has a delay option that does exactly what you're looking for.
http://jqueryui.com/demos/autocomplete/#option-delay