简体   繁体   English

如何在Ruby on Rails网站的HTML表的左右列中显示数据?

[英]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) %>&nbsp;|
                    <%= link_to 'Show', category_path(category) %>&nbsp;|
                    <%= 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) %>&nbsp;|
                    <%= link_to 'Show', category_path(category) %>&nbsp;|
                    <%= 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) %>&nbsp;|
                    <%= link_to 'Show', category_path(category) %>&nbsp;|
                    <%= 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) %>&nbsp;|
                    <%= link_to 'Show', category_path(category) %>&nbsp;|
                    <%= 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.

相关问题 "如何左对齐或右对齐 HTML、CSS 中表格特定列中的文本" - How do I left or right justify, text within specific columns of a table in HTML,CSS 如何强制 html 表的“td”元素中的单独元素显示内联(从左到右)? - How can I force separate elements within a “td” element of an html table to display inline (left to right)? 如何创建具有固定左右列和水平滚动的表格? - How do I create a table with fixed left and right columns and horizontal scrolling? 如何使用 CSS 和 HTML 从右向左移动表格 - How do I move a table from right to left using CSS and HTML 如何在html和css中获得td背景颜色从左到右伸展,而不仅仅是表格宽​​度? - How do I get a td background color to stretch from left to right, not just the table width, in html & css? 如何在 html 表中显示 JSON 响应数据 - How do I display JSON response data in a html table 如何在HTML表中显示此XML数据? - How do I display this XML data in an HTML table? 使用Ruby on Rails以列而非行显示数据 - Display data in columns not rows using Ruby on Rails 如何在表格的左侧和右侧添加阴影 - how do i add shadow on left and right side of column of table 具有2列的html表上的CSS。 (左列包含图像,右列包含有关图像的数据) - Css on html table with 2 columns. (left column contains image, right one contains data about image)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM