[英]How do I display data in a Ruby on Rails site in left and right columns of HTML table?
In my Ruby on Rails application I want to display half the data from one table in the database in a left column in a HTML table and the other half in the right column. 在我的Ruby on Rails应用程序中,我想在HTML表格的左列中显示数据库中一个表的一半数据,在右列中显示另一半数据。 What I have only been able to is display the data with an id of 1 in a left cell and then in a right cell in the same row and then data with an id of 2 in a left cell then again in a right cell in the next row: 我唯一能做的就是在同一行的左单元格中显示ID为1的数据,然后在同一行的右单元格中显示ID,然后在左单元格中的ID为2的数据中再次显示数据。下一行:
<table>
<% @categories.each do |category| %>
<tr>
<% if category.id == 1 %>
<td bgcolor="#03FF00">
<%= category.id %>
</td>
<td bgcolor="#f1f1c1">
<%= link_to 'Edit', edit_category_path(category) %> |
<%= link_to 'Show', category_path(category) %> |
<%= link_to 'Destroy', category_path(category), method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
<% end %>
<% if category.id == 1 %>
<td bgcolor="#03FF00">
<%= category.id %>
</td>
<td bgcolor="#f1f1c1">
<%= link_to 'Edit', edit_category_path(category) %> |
<%= link_to 'Show', category_path(category) %> |
<%= link_to 'Destroy', category_path(category), method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
<% end %>
<% if category.id == 2 %>
<td bgcolor="#03FF00">
<%= category.id %>
</td>
<td bgcolor="#f1f1c1">
<%= link_to 'Edit', edit_category_path(category) %> |
<%= link_to 'Show', category_path(category) %> |
<%= link_to 'Destroy', category_path(category), method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
<% end %>
<% if category.id == 2 %>
<td bgcolor="#03FF00">
<%= category.id %>
</td>
<td bgcolor="#f1f1c1">
<%= link_to 'Edit', edit_category_path(category) %> |
<%= link_to 'Show', category_path(category) %> |
<%= link_to 'Destroy', category_path(category), method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
<% end %>
</tr>
<% end %>
</table>
I have been trying this for ages and that piece of code was the best that I could come up with, but doesn't work when I try to use it to show half in one side and half on the other, which I have attempted to do using: 我已经尝试了很长时间了,这段代码是我能想到的最好的代码,但是当我尝试使用它来展示一侧的一半和另一侧的一半时,它不起作用。使用:
if category.id <= Category.count / 2
and 和
if category.id > Category.count / 2 如果category.id> Category.count / 2
Which I know in itself isn't a very efficient or 'safe' way of calculating due to using the id and not the row number. 我知道,由于使用id而不是行号,它本身并不是一种非常有效或“安全”的计算方式。
In my categories table I have two columns: id and genre, but I want to display only the genre of each category and the links: Edit, Show, and Destroy for each one. 在我的类别表中,我有两列:id和genre,但我只想显示每个类别的类型以及链接:每个类别的Edit,Show和Destroy。
Any ideas? 有任何想法吗? Please try to show me the code rather than tell me where to place code. 请尝试向我展示代码,而不是告诉我代码在哪里。
What I assume is you want to display the same rows of table in two parts on the page. 我假设您要在页面的两部分中显示相同的表行。 So I would suggest you to make two tables one for the left and one for the right. 因此,我建议您制作两张桌子,左边一张,右边一张。 And play with css. 并玩css。 In the table left give: 在左表中给出:
float: left
and in the table right give: 并在右表中给出:
float: right
Also you can play with position and other attributes of css. 您也可以使用CSS的位置和其他属性。 Or the next option is to make four columns but I am not sure as your question is a bit unclear. 或者下一个选择是制作四列,但是我不确定,因为您的问题有点不清楚。
Or if you just need to calculate then go with even and odd numbers. 或者,如果只需要计算,则使用偶数和奇数。 Ruby provides method for checking even and odd. Ruby提供了检查偶数和奇数的方法。 Eg: 例如:
a = 1
a.odd?
a.even?
Update: 更新:
A code snippet for float left and right: 左右浮动的代码段:
<html>
<head>
<style type="text/css">
.left{
float: left;
}
/*Optional*/
.right{
float: right;
}
</style>
</head>
<body>
<table class="left">
<thead>
<th>Name</th>
<th>Action</th>
</thead>
<tbody>
<td>Deep</td>
<td><a href="#">Edit</a></td>
</tbody>
</table>
<table class="right">
<thead>
<th>Name</th>
<th>Action</th>
</thead>
<tbody>
<td>Deep</td>
<td><a href="#">Edit</a></td>
</tbody>
</table>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.