在使用 will_paginate 时,如何解决需要将表分成两组、每组四个进行排序的问题?
我正在使用 will_paginate
一次对我的八个项目进行分页。
我想将它们组织在一个两行四列的表格中,同时仍然保持分页。我该怎么做?
到目前为止我已经:
<%= will_paginate @items, :page_links => false %>
<table id="item_table">
<tr>
<% for item in @items %>
<td>Test</td>
<% end %>
</tr>
</table>
但我需要以某种方式改变这一点,我可以让 @items
成为每四个项目的临时数组,而不是八个。这是理想的输出:
<-- 1 2 ... 5 6 -->
Test Test Test Test
Test Test Test Test
显然顶部链接指向接下来的八个项目。有什么想法吗?
I'm using will_paginate
to paginate my items eight at a time.
I want to organize them in a table with two rows and four columns while still maintaining pagination. How can I do this?
So far I have:
<%= will_paginate @items, :page_links => false %>
<table id="item_table">
<tr>
<% for item in @items %>
<td>Test</td>
<% end %>
</tr>
</table>
But I need to change this somehow that I can have @items
be a temporary array of every four items, instead of eight. Here's the ideal output:
<-- 1 2 ... 5 6 -->
Test Test Test Test
Test Test Test Test
Where obviously the top links lead to the next eight items. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更新
each_slice 本身就是一个可枚举的,所以没有需要在调用每个数组之前创建一个数组,我减少了一行代码
<% @item.each_slice(4) 执行 |item_new| %>
<% item_new.each 做 |item|%>
<%= 项目.名称 %>
<%结束%>
<%结束%>
该代码只是我的一个应用程序的一个片段,实现了与您想要实现的几乎相同的事情,我希望它确实有帮助,不要忘记包含 will_paginate 并完成表格。这只是您需要的一些逻辑。
Update
each_slice is an enumerable itself so there is not need to create an array before calling each on it, I reduced the code by a line
<% @item.each_slice(4) do |item_new| %>
<% item_new.each do |item|%>
<%= item.name %>
<% end %>
<% end %>
The code is just a snippet from one of my app, implementing almost the same thing you are trying to achieve, I hope it does help, don't forget to include the will_paginate and complete the table. This is just some logic you would require.
这与分页无关。这个问题与观点有关。您可以使用 CSS 来控制您的视图!
不过,如果您更喜欢从代码生成 2x4 网格,请尝试以下操作:
您可以轻松重构它以删除代码重复。
This is nothing to do with pagination. The question is related to views. You can use CSS to control your views!
Still, if you prefer to generate a 2x4 grid from code, try this:
You can refactor it easily to remove code repetition.
view:
controller:
这有点脏,可能有更好的方法来分割行,但这很快。
另外,这是我通常使用 div 做的事情,所以你可以让它们自由地环绕。
view:
controller:
This is a little dirty, there's probably a better way to split the rows, but this is quick.
Also, this is the kind of thing i'd normally use divs for, so then you can just let them wrap round freely.