views:

4757

answers:

4

How can I change the style of a particular row based on a condition? I can use JSF EL in rich:column style class attribute, but I have to write for each column. I want to change the entire row.

Thanks

+1  A: 

You can use the dataTables columnClasses and rowClasses properties.

That way you can produce the result which is shown here

ChrisAD
That won't allow you to set the style conditionally unless you put in as many class names as you have rows.
Damo
Ahh Im sorry he want conditional styling on every single row. I thought he wanted to place conditional formatting on all the rows, but didnt want to write the condition for all the columns
ChrisAD
+1  A: 

I do as you've already mentioned and put the style on the column.

However you could always try wrapping all of your columns in a <rich:columnGroup> which is supposed to output a <tr> and place your conditional style on that.

EDIT: (in response to comment): if the header facets in your columns are being broken then you can separate them into a column group as well. Should work - you may not even need the column group in the header??

Eg.

<rich:dataTable>
  <f:facet name="header">
    <rich:columnGroup>
      <rich:column>Header 1</rich:column>
      <rich:column>Header 1</rich:column>
    </rich:columnGroup>
  </f:facet>
  <rich:columnGroup>
    <rich:column>Data</rich:column>
    <rich:column>Data</rich:column>
  </rich:columnGroup>
</rich:dataTable>
Damo
Yes, This is a good solution, but unfortunately it breaks the header facet. Any ideas?
volothamp
Already tried putting header in a column group, but richfaces skin is not applied. Any other ideas? Thanks a lot :)
volothamp
+1  A: 

I've done an hybrid solution with Javascript.

<rich:column styleClass="expired" rendered="#{documento.expired}">
<f:facet name="header">
Da evadere entro
</f:facet>      
<h:outputText value="#{documento.timeAgoInWords}" />
</rich:column>

and then in Javascript (with Prototype which is included in Richfaces)

<script type="text/javascript">   
  function colorize() {    
    $$('td.expired').each(function(el) {
      el.up().addClassName('expired');   
    });
  }

  Event.observe(window, 'load', function() {
    colorize();
  });
</script>

edit:

this add a conditional css class with rendered:

<rich:column styleClass="expired" rendered="#{documento.expired}">

with javascript I loop on every td with css class expired $$('td.expired') and add the same css class to the upper node tr with el.up().

Event.observe(window, 'load', function() {});

this simply runs the function when the DOM is fully loaded.

Luke
Seems interesting: could you explain it a little? I mean, I understand you put the 'scaduto' and 'expired' class to <td> element, but the requirements where to color the entire row.I mean, since there's not condition on the column, how do you change the class based on a value on the bean? Thanks a lot :)
volothamp
I've edited the post, it's clear now? :) I can post full source code if you want.
Luke
+4  A: 

Specifically for each column:

<rich:column styleClass="#{someBean.isSomething ? 'styleIfTrue' : 'styleIfFalse' }">