Rails CSS 斑马条纹行,但以三行为一组

发布于 2024-09-26 06:42:15 字数 1482 浏览 4 评论 0 原文

我所知道的: 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 有推荐的方法吗?

更新

  1. 与@Ryan Bigg的回答类似,是否可以按顺序生成字符串,以便cycle()方法可以发送prefix_1 prefix_2 prefix_3 以某种方式使用 *3 语法或其他方式?
  2. 使用这种“每隔三行”条纹的灵感来自 Edward Tufte 网站上的文章
  3. 看来 CSS3 可以做到这一点 (链接),但目前尚未得到广泛支持:(
  4. 与问题无关,但我只是在Google上搜索了这个问题,以及 questionhub.com!我从来没有在 Questionhub.com 上问过这个问题!
  5. 现在它在 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

  1. 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?
  2. The inspiration to use this "every third row" striping came from an article on Edward Tufte's site
  3. It seems CSS3 can do this (link), but that's not widely supported at this time :(
  4. 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!
  5. Now its on http://loopingrecursion.com too! What's going on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆光飞翔i 2024-10-03 06:42:15

这个怎么样?

<tr class="<%= cycle(*[["striped"]*3,["unstriped"]*3].flatten) %>">

How about this?

<tr class="<%= cycle(*[["striped"]*3,["unstriped"]*3].flatten) %>">
此岸叶落 2024-10-03 06:42:15

实际上,这听起来像是我会使用纯 CSS 来解决的问题:

table tr:nth-child(6n+1), table tr:nth-child(6n+2), table tr:nth-child(6n+3) { background:red;} 
table tr:nth-child(6n+4), table tr:nth-child(6n+5), table tr:nth-child(6n)   { background:blue;} 

虽然这很短而且花哨,但这个 在 IE 中不起作用(它将在新的 IE9 中起作用)。

要实现此功能,您可以使用 jQuery,它提供相同的选择器并且完全跨浏览器兼容。

在你的CSS中,你添加两个类:

table_row_third_even { background: red;}
table_row_third_odd { background: blue;}

然后你编写一些JavaScript(例如在application.js中),像这样

$(function() {
  $('table tr:nth-child(6n+1), table tr:nth-child(6n+2), table tr:nth-child(6n+3)').addClass('table_row_third_even');
  $('table tr:nth-child(6n+4), table tr:nth-child(6n+5), table tr:nth-child(6n)').addClass('table_row_third_odd');
});

,你的表必须有类highlight(您应该将其更改为更合适的名称,这只是一个示例);但没有任何特定于您的 tr 标记的内容,因为它们将由 javascript 代码添加。

<table class='highlight'>
  <tr> ... </tr>
  <tr> ... </tr>
  <tr> ... </tr>
  <tr> ... </tr>
</table>

但要实现此功能,您需要在项目中引入 jQuery。好处是:它可以让您的 ruby​​ 代码免受执行此操作所需的混乱的影响。

希望这有帮助!

Actually this sounds like something i would solve using pure CSS:

table tr:nth-child(6n+1), table tr:nth-child(6n+2), table tr:nth-child(6n+3) { background:red;} 
table tr:nth-child(6n+4), table tr:nth-child(6n+5), table tr:nth-child(6n)   { background:blue;} 

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:

table_row_third_even { background: red;}
table_row_third_odd { background: blue;}

and then you write some javascript (inside application.js for instance), like this

$(function() {
  $('table tr:nth-child(6n+1), table tr:nth-child(6n+2), table tr:nth-child(6n+3)').addClass('table_row_third_even');
  $('table tr:nth-child(6n+4), table tr:nth-child(6n+5), table tr:nth-child(6n)').addClass('table_row_third_odd');
});

and 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 your tr tags, as they will be added by the javascript code.

<table class='highlight'>
  <tr> ... </tr>
  <tr> ... </tr>
  <tr> ... </tr>
  <tr> ... </tr>
</table>

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!

忘羡 2024-10-03 06:42:15

我找到了一种方法来做到这一点,这种方法有点“干净”并且目前适合我。详细信息:

在控制器助手中(还没有错误检查!):

def cycle_every_third(options = [])
  cycle(options[0],options[0],options[0],options[1],options[1],options[1])
end

HTML:

...
<tr class="<%= cycle_every_third(["table_row_1","table_row_2"] ) %>">
...

CSS:

.table_row_1 {   background-color: red;}
.table_row_2 {   background-color: blue;}

我想知道这是解决这个问题的好方法吗?我可能没有意识到 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!):

def cycle_every_third(options = [])
  cycle(options[0],options[0],options[0],options[1],options[1],options[1])
end

HTML:

...
<tr class="<%= cycle_every_third(["table_row_1","table_row_2"] ) %>">
...

CSS:

.table_row_1 {   background-color: red;}
.table_row_2 {   background-color: blue;}

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文