3

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?

2 Answers 2

3

Or why not something like this:

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Is there a way to do this within a helper file instead of within the haml?
@Em8 depends on what you mean by helper? Are we creating a view helper that will generate the DOM element? A boolean helper to just return the appropriate class String? maybe something like def success_cell(val); haml_tag ("#{val ? 'td.success' : 'td.failure'}", val);end and then you could just call success_cell(person.success)
1

You may want to consider doing this with some JavaScript/jQuery when the page renders:

$(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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.