简体   繁体   English

Ruby和Haml表中的条件CSS

[英]Conditional CSS in Ruby and Haml Table

I have a list of items in a table. 我有一个表中的项目列表。 Some are true and some are false. 有些是正确的,有些是错误的。 Is there a way that the true elements can have one background colour whilst the false elements have another. 有没有一种方法可以使真元素具有一种背景色,而假元素具有另一种背景色。 I was considering writing a method in the application helper but I am not sure how is best to write this. 我当时正在考虑在应用程序助手中编写一个方法,但是我不确定如何最好地编写此方法。 So far all i have is 到目前为止,我所拥有的只是

%td.success= person.success

This currently prints out true or false to the table but i would just like to add some background colour to this? 目前,这会在表中打印出true或false,但我想为此添加一些背景颜色?

Or why not something like this: 还是为什么不这样呢?

css CSS

td.success { background-color: 'green' }
td.failure { background-color: 'red' }

then in the haml 然后在雾中

%td{class: "#{person.success ? 'success' : 'failure'}"} = person.success

Or 要么

- if person.success
  %td.success = person.success
- else
  %td.failure = person.success

You may want to consider doing this with some JavaScript/jQuery when the page renders: 当页面呈现时,您可能要考虑使用一些JavaScript / jQuery来执行此操作:

$(document).ready(function() {

    $("td").each(function() {

        if ( $(this).html() === true ) {
             $(this).css("background", "blue");
        }
        else {
             $(this).css("background", "red");
        }

    }

}

With some more context to what your rendered HTML looks like, we can tweak this to get something more specific to your case. 在呈现的HTML外观有更多上下文的情况下,我们可以对其进行调整,以获得更具体的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM