每行显示 5 条记录? [英] Display 5 records per row?

查看:36
本文介绍了每行显示 5 条记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前这只是一个很长的清单.如何显示每行 5 条记录而不是 1 条记录?

Currently this just makes a long list. How do I display this with 5 records per row instead of 1?

<% @tags.each do |tag| %>

<p><%= tag_search(tag) %></p>

<% end %>

目前

tag1
tag2
tag3
tag4
tag5
tag6
tag7
...

想要

tag1 tag2 tag3 tag4 tag5
tag6 tag7

我知道这真的很基础,但我无法找到正确的谷歌搜索词来获得答案.谢谢!

I know this is really basic but I just cannot manage to find the right google search terms to get the answer on my own. Thanks!

这是我最终使用的

<table>
<% @tags.in_groups_of(4, false) do |row_tag| %>
  <tr>
    <% for tag in row_tag %>
      <td><%= tag_search(tag) %></td>
    <% end %>
  </tr>
<% end %>
</table>

推荐答案

你可以使用 ActiveSupport 方法 in_groups_of 取一个数组并把它分组

You can use the ActiveSupport method in_groups_of to take an array and put it into groups

<% @tags.in_groups_of(5).each do |tag_array| %>
  <% tag_array.each |tag| %>
  ...

来自 Rails Docs 的原始文档

%w(1 2 3 4 5 6 7).in_groups_of(3) {|g| p g}
  ["1", "2", "3"]
  ["4", "5", "6"]
  ["7", nil, nil]

这篇关于每行显示 5 条记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆