我所知道的:
Rails 具有 cycle()
方法,该方法允许表中的奇数/偶数行具有不同的 CSS 类。我们可以将多个类传递给 Cycle() 方法,效果很好。
我想要以三为一组的行;所以,我使用了这个:
...some html-table code...
<tr class="<%= cycle("table_row_1","table_row_2","table_row_3","table_row_4","table_row_5","table_row_6") %>">
...some more html-table code...
我定义的相应CSS是:
.table_row_1 { background-color: red;}
.table_row_2 { background-color: red;}
.table_row_3 { background-color: red;}
.table_row_4 { background-color: blue;}
.table_row_5 { background-color: blue;}
.table_row_6 { background-color: blue;}
它可以工作,但感觉不“干净”。
Rails 有推荐的方法吗?
更新
- 与@Ryan Bigg的回答类似,是否可以按顺序生成字符串,以便cycle()方法可以发送
prefix_1 prefix_2 prefix_3 以某种方式使用 *3
语法或其他方式?
- 使用这种“每隔三行”条纹的灵感来自 Edward Tufte 网站上的文章
- 看来 CSS3 可以做到这一点 (链接),但目前尚未得到广泛支持:(
- 与问题无关,但我只是在Google上搜索了这个问题,以及 questionhub.com!我从来没有在 Questionhub.com 上问过这个问题!
- 现在它在 http://loopingrecursion.com 也是!这是怎么回事。
What I know:
Rails has the cycle()
method that enables odd/even rows in a table to have different CSS classes. We can pass multiple classes to the cycle()
method and that works great.
I wanted rows which are grouped in three's; so, I used this:
...some html-table code...
<tr class="<%= cycle("table_row_1","table_row_2","table_row_3","table_row_4","table_row_5","table_row_6") %>">
...some more html-table code...
The corresponding CSS I defined is:
.table_row_1 { background-color: red;}
.table_row_2 { background-color: red;}
.table_row_3 { background-color: red;}
.table_row_4 { background-color: blue;}
.table_row_5 { background-color: blue;}
.table_row_6 { background-color: blue;}
It works, but it doesn't feel "clean".
Is there a recommended way to do this in Rails ?
UPDATES
- On similar lines to @Ryan Bigg's answer, is it possible to generate strings in sequence so that the cycle() method can be sent strings of the form
prefix_1 prefix_2 prefix_3
somehow using the *3
syntax or some other way?
- The inspiration to use this "every third row" striping came from an article on Edward Tufte's site
- It seems CSS3 can do this (link), but that's not widely supported at this time :(
- Not related to question, but I just searched for this problem I have on Google, and the questions listed at questionhub.com! I never asked this on questionhub.com!
- Now its on http://loopingrecursion.com too! What's going on.
发布评论
评论(3)
这个怎么样?
How about this?
实际上,这听起来像是我会使用纯 CSS 来解决的问题:
虽然这很短而且花哨,但这个 在 IE 中不起作用(它将在新的 IE9 中起作用)。
要实现此功能,您可以使用 jQuery,它提供相同的选择器并且完全跨浏览器兼容。
在你的CSS中,你添加两个类:
然后你编写一些JavaScript(例如在
application.js
中),像这样,你的表必须有类
highlight
(您应该将其更改为更合适的名称,这只是一个示例);但没有任何特定于您的tr
标记的内容,因为它们将由 javascript 代码添加。但要实现此功能,您需要在项目中引入 jQuery。好处是:它可以让您的 ruby 代码免受执行此操作所需的混乱的影响。
希望这有帮助!
Actually this sounds like something i would solve using pure CSS:
While this is short and dandy, this does not work in IE unfortunately (it will work in the new IE9).
To get this working you could use jQuery, which offers the same selectors and is completely cross-browser compatible.
In your css you add two classes:
and then you write some javascript (inside
application.js
for instance), like thisand your table would have to have the class
highlight
(you should change that to a better suiting name, this is just an example); but nothing specific for yourtr
tags, as they will be added by the javascript code.But for this to work you would need to introduce jQuery inside your project. The good thing: it would keep your ruby-code clear from the clutter needed to do this.
Hope this helps!
我找到了一种方法来做到这一点,这种方法有点“干净”并且目前适合我。详细信息:
在控制器助手中(还没有错误检查!):
HTML:
CSS:
我想知道这是解决这个问题的好方法吗?我可能没有意识到 Rails 中在辅助方法中调用 Cycle() 的陷阱,而不是在 rhtml/erb 文件中调用它?
I found a way to do this, which is sort of "clean" and suits me for now. Details:
In a controller helper (no error checking, yet!):
HTML:
CSS:
Is that a good way to go about this I wonder? Any pitfalls in Rails I may not be aware of, of calling
cycle()
inside a helper method as opposed to calling it in the rhtml/erb file?