如何创建适用于无限滚动加载行为的方法查询
我正在创建一个输出 1000-3000 条记录列表的页面。当前流程是:
- 用户加载页面
- jQuery 访问服务器以获取所有记录并将它们注入到页面中。
这里的问题是,某些用户的这些记录可能需要 3 秒以上才能返回,这是一个可怕的用户体验。
我想做的是: 1. 用户加载页面 2. jQuery 命中服务器最多获取 100 条记录。然后继续循环访问服务器,直到加载的记录等于最大记录。
这里的想法是用户可以快速查看记录并且不会认为有什么问题。
所以它并不是真正的无限滚动,因为我不关心滚动位置,但它看起来像是一个类似的流程。
在 jQuery 中如何使服务器处于循环状态?考虑到偏移和限制,我如何在 Rails 中进行查询?
谢谢
I'm creating a page that outputs a list of 1000-3000 records. The current flow is:
- User loads a page
- jQuery hits the server for all the records and injects them into the page.
Problem here is that those records for some users can take 3+ seconds to return which is a horrible UX.
What I would like to do is the following:
1. User loads a page
2. jQuery hits the server and gets at most 100 records. Then keeps hitting the server in a loop until the records loaded equal the max records.
Idea here is the user gets to see records quickly and doesn't think something broke.
So it's not really an infinite scroll as I don't care about the scroll position but it seems like a similar flow.
How in jQuery can I the the server in a loop? And how in rails can I query taking into account a offset and limit?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需一遍又一遍地向服务器查询一批数据即可。
您可以实现许多 API。例如:
或者您可以让客户端传入参数,表示您需要资源 1-100,然后是 101-200,并循环执行此操作。
您始终会在数据传入时对其进行渲染。
您的服务器要么需要让您传入参数,表示您想要将 i 记录到 i + n。
或者你的服务器需要获取所有数据。将其存储在某处,然后返回一块数据以及某种唯一的 id 或 url,以请求另一块数据并重复此操作。
You can simply query the server for a batch of data over and over again.
There are numerous APIs you can implement. Like:
Or you can get the client to pass in parameters saying you want resource 1-100, then 101-200 and do this in a loop.
All the while you will render the data as it comes in.
Your server either needs to let you pass in parameters saying you want record i to i + n.
Or your server needs to get all the data. Store it somewhere then return a chunk of the data along with some kind unique id or url to request another chunk of data and repeat this.