Jquery无限滚动和XML

发布于 2024-10-18 17:23:14 字数 110 浏览 3 评论 0原文

我见过一些插件,当您滚动到页面底部时会加载更多内容,但其中大多数都链接到数据库中具有自动增量 ID 的列。我只需要使用 XML 文件来完成此操作。

有什么想法吗?

非常感谢。

I have seen a few plugins that load more content when you scroll to the bottom of the page, but most of these are linked to a column in a database with an autoincremented id. I need to do this only with an XML file.

Any ideas?

Much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里的微风 2024-10-25 17:23:14

xml 文件位于服务器上,但它
我想可以用 jQuery 来解析吗?
我可以使用 ajax 并解析整个
XML 文件,但这会击败
无限滚动/延迟加载?

如果您将 XML 发送到客户端,这将导致浏览器解析它并构建 DOM。根据它的大小(几千字节?兆字节?),这可能很快,也可能需要一段时间。默认情况下将所有 if 发送到浏览器也会占用您的带宽,是的,某种程度上违背了延迟加载的目的。

然而,无限滚动是一种 UI 范例,因此您可以实现它,即使您总是将整个 XML 文件发送到浏览器。同样,这取决于您想要实现的目标 - 节省带宽或只是“即时”创建内容。

假设您将文件留在服务器上,因为它太大了:

  • 让服务器将其加载到线程安全(!)DOM 对象中。不要在应用程序生命周期内销毁该对象,通过存储在应用程序(或会话)中为每个请求重新使用它。事实上,这创建了“数据库”。
  • 构建一个访问 XML 并将其部分内容返回到浏览器的代理页面。您可以使用 XPath 从 XML DOM 对象中检索请求的位。
  • 使用您最喜欢的无限滚动 jQuery 插件并调整代理页面以接受其请求参数。

来自无限滚动插件的假设请求可能如下所示

http://server.name/path/data.aspx?start=516&count=10

然后您的 data.aspx 将使用 XPath 查询从第 516 个元素检索 10 个 XML 元素:

/root/data[position() >= 516 and position() < 516 + 10]

然后您可以从中构造 HTML 或将其作为原始 XML 发送并通过以下方式进行评估客户端上的 jQuery。

可能有定制的框架可以完成所有这些工作,但如果我要实现这样一个系统,以上大致就是我会做的。

The xml file is on the server but it
could be parsed with jQuery I suppose?
I could use ajax and parse the whole
XML file but that would defeat the
infinite scroll/lazy load?

If you send your XML down to the client, this will cause that the browser parses it and builds a DOM. Depending on how large it is (A few kilobytes? Megabytes?) this can be fast, or it can take a while. Sending all of if to the browser by default also hogs your bandwidth and yes, somehow defeats the purpose of lazy loading.

Infinite scroll however is a UI paradigm and as such you can implement it even though you always send the entire XML file to the browser. Again it depends on what you want to achieve - conserve bandwidth or just create content "on the fly".

Supposing you leave the file on the server because it is so large:

  • Have the server load it into a thread-safe (!) DOM object. Do not destroy the object in the application lifetime, re-use it for every request by storing in the application (or the session). In fact, this creates the "database".
  • Build a proxy page that accesses the XML and returns part of it to the browser. You can use XPath to retrieve the requested bits from the XML DOM object.
  • Use your favorite infinite scroll jQuery plugin and adapt the proxy page to accept its request parameters.

A hypothetical request from an infinite scroll plugin could look like

http://server.name/path/data.aspx?start=516&count=10

And your data.aspx would then use an XPath query to retrieve 10 XML elements from 516th element on:

/root/data[position() >= 516 and position() < 516 + 10]

You would then either construct HTML from that or send it as raw XML and evaluate that via jQuery on the client.

There are probably tailored frameworks that do all this, but if I were to implement such a system, the above is roughly what I would do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文