will_paginate + ajax删除
我有一个资源列表,使用 will_paginate 显示在分页列表上。
我没有定制任何东西,这是我的观点:
<ul>
<%= render :partial => "item_li", :collection => @items%>
</ul>
<%= will_paginate @items %>
项目在控制器上是这样计算的:
@items = Item.find(:all)
生成的代码是一个简单的 html 列表,底部有分页导航链接:
<ul>
<li>Item 1</li>
<li>Item 2</li>
...
<li>Item 10</li>
</ul>
<div class="pagination">
<span class="disabled prev_page">Previous</span>
<span class="current">1</span>
...
</div>
我在我的项目上包含了一个“删除”按钮。当按下该链接时,会对服务器进行 ajax 调用,并且该项目从列表中消失(
将被 ""
替换)我的问题来了与导航链接。
- 假设我在第一页上有项目 1 到 10,在第二页上有项目 11 到 20,等等。
- 假设我删除了第一页上的项目 1、2 和 3。
- 然后我按“下一步”。
服务器将“重新计算”项目列表;由于我删除了前面列表中的 3 个,因此它将返回第 13 至 22 项。换句话说,第 10、11 和 12 项不会出现在第一页或第二页上。
我怎样才能以简单、优雅的方式解决这个问题?
到目前为止,我能找到的唯一解决方案是在每次删除后重新加载整个页面,但是使用 ajax 就变得毫无意义了。
多谢。
I've got a list of resources that I show on a paginated list using will_paginate.
I haven't customized anything, here's my view:
<ul>
<%= render :partial => "item_li", :collection => @items%>
</ul>
<%= will_paginate @items %>
Items is calculated like this on the controller:
@items = Item.find(:all)
The generated code is a simple html list with the pagination navigation links at the bottom:
<ul>
<li>Item 1</li>
<li>Item 2</li>
...
<li>Item 10</li>
</ul>
<div class="pagination">
<span class="disabled prev_page">Previous</span>
<span class="current">1</span>
...
</div>
I have included a "delete" button on my items. When that link is pressed, there's an ajax call to the server and the item disappears from the list (the <li>
will is replaced by ""
)
My problem comes with the navigation links.
- Say that I had items 1 to 10 on the first page, items 11 to 20 on the second, etc.
- Say that I delete items 1, 2 and 3 on the first page.
- And then I press "next".
The server will "recalculate" the list of items; since I removed 3 of the previous list, it will return items 13 to 22. In other words, items 10, 11 and 12 don't appear on the first page nor the second.
How can I solve this in a simple, elegant way?
So far the only solution I could find was reloading the whole page after each deletion, but then using ajax becomes pointless.
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的 AJAX 库,您需要实现一个回调方法,每当 AJAX 调用成功完成时就会调用该方法。
首先,将
<%= will_paginate @items %>
放入 div 或某种容器中。然后,在“成功”回调中,删除该 div 并将其替换为包含对 will_paginate @items 的新调用的新 div。假设您对删除方法的 AJAX 调用重新加载了 @items 变量,则此操作有效。
如果您使用 jQuery,它看起来像这样(假设您为保存分页的 div 提供了一个名为“my_pagination”的类:
您可能必须将 <%=will_paginate @items%> 用引号括起来,我不是100%确定。
Depending on your AJAX library, you need to implement a callback method that gets called whenever the AJAX call has completed successfully.
First, put the
<%= will_paginate @items %>
into a div or some sort of container. Then, in your "success" callback, remove that div and replace it with a new one containing a new call to will_paginate @items.This works assuming that your AJAX call to your delete method reloads the @items variable.
If you're using jQuery, it'd look something like this (assuming you give the div holding the pagination a class called "my_pagination":
You may have to wrap the <%=will_paginate @items%> in quotation marks, I'm not 100% sure.
我决定做一些简单一点的事情;每当我从列表中删除一个项目时,我都会加载页面上最后一个项目之后的项目,并将其插入到底部。这样我就可以随心所欲地删除了,而且整个页面感觉很清爽。
我只需要小心“没有更多物品”的情况。
非常感谢您的回答,扎卡里,但这感觉更正确。
I decided to do something a bit simpler; whenever I deleted an item from the list, I loaded the item that came right after the last item on the page, and inserted it at the bottom. This way, I can delete to my heart's content, and the whole page feels snappy.
I just have to be careful with the "no more items" situations.
Thanks a lot for your answer, Zachary, but this one feels more correct.