6

I'd like to change the background color of rows based on a condition.

<t:dataTable id="data"
                styleClass="history-table"
                headerClass="history-table-header"
                rowClasses="history-table-row-default"
                border="2" cellpadding="5" cellspacing="2"
                var="entry"
                value="#{historyBean.logEntryList}"
                preserveDataModel="false"
                rows="#{historyBean.history.rowCount}"
                sortable="true">

           <h:column>
               <f:facet name="header">
                 <h:outputText value="Debug Status" />
               </f:facet>
               <h:outputText value="#{entry.action}" />
           </h:column>

If the value of "entry.action" is X I like to use "history-table-row-incomplete" (name of styleclass), if the value is Y I like to use "history-table-row-error" (name of styleclass). All other cases should use the default value.

I guess i have to get the current object of entry somehow to my bean, analyze it and return a string with the name of the stylclass to outputText to change the color. But I don't know how... (I'm new in JSF...)

Can someone help me please?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Michael S
  • 731
  • 2
  • 10
  • 26

2 Answers2

12

Use the rowStyleClass attribute of the <t:dataTable> instead of rowClasses. The rowStyleClass is evaluated on a per-row basis where the var="entry" is available, while the rowClasses is only evaluated on a per-table basis.

<t:dataTable ... rowStyleClass="#{entry.action == 'X' ? 'history-table-row-incomplete' : (entry.action == 'Y' ? 'history-table-row-error' : 'history-table-row-default')}">
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    This cause a parsing error. Have to say i use myfaces 2.1.5. Is there a way to get the value of extry.action into my bean to handle it there? – Michael S Jan 05 '12 at 15:52
  • rowStyleClass="#{entry.action == 'F' ? 'history-table-row-error' : (entry.action == 'Y' ? 'history-table-row-error' : '') : ''}" Error Parsing: #{entry.action == 'F' ? 'history-table-row-error' : (entry.action == 'Y' ? 'history-table-row-error' : '') : ''} Caused by: org.apache.el.parser.ParseException - Encountered " ":" ": "" at line 1, column 108. Was expecting one of: "}" ... "." ... "[" ... ">" ... "gt" ... "<" ... "lt" ... ">=" ... "ge" ... "<=" ... "le" ... "==" ... "eq" ... "!=" ... "ne" ... "&&" ... "and" ... "||" ... "or" ... "*" ... "+" ... "-" ... "/" ... "div" ... "%" ... "mod" – Michael S Jan 05 '12 at 15:58
  • 1
    I had a syntax error in my initial answer. Please check the updated example which I posted 12 minutes ago. – BalusC Jan 05 '12 at 15:59
  • @BalusC is there a way to do this on Majora `h:datatable` ? – Youans Jun 12 '17 at 13:48
-2

You can use JSF EL Ternary operator, as below:

rowStyleClass="#{entry.action eq X ? 'history-table-row-incomplete' :  (entry.action eq Y ? 'history-table-row-error' : 'default')}"
bchetty
  • 2,231
  • 1
  • 19
  • 26
  • 2
    The `styleClass` is applied on ``, not on ``.
    – BalusC Jan 05 '12 at 15:46
  • Somebody negged my post! I might not have used the exact 'attribute', but my point was to make the OP understand how to implement it with EL and ternary operator. – bchetty Mar 26 '14 at 14:27
  • 3
    Huh? OP asked *"I'd like to change the background color of rows based on a condition."*. This answer doesn't answer that. So this answer is wrong. Simple as that. Just fix or delete it if you're bothered. I can't for life understand why wrong answers should be upvoted. It would completely destroy the point of Stack Overflow. – BalusC Mar 27 '14 at 07:29
  • I know what you mean and I didn't ask for upvoting. As I said before, "my point was to make the OP understand how to implement it with EL and ternary operator." – bchetty Mar 27 '14 at 08:17