1 Reply Latest reply on Jul 30, 2008 3:12 AM by Zebediah Bandersnatch

    rich:datatable - Way to sort rowClasses?

    Zebediah Bandersnatch Newbie

      Hi all,

      I'm building an event tracking system and would like to use the rowClasses attribute of rich:datatable to apply different background colors to each row based on their priority. This works ok:

       <rich:dataTable
       id="evttable"
       value="#{eventAction.eventList}"
       var="evt"
       ...
       rowClasses="#{eventAction.rowClasses}">
       <rich:column id="priorityId" sortBy="#{evt.priority}">
       <f:facet name="header"><h:outputText value="Priority" /></f:facet>
       <h:outputText value="#{evt.priorityName}"/>
       </rich:column>
       <rich:column id="type" sortBy="#{evt.type}">
       <f:facet name="header"><h:outputText value="Type" /></f:facet>
       <h:outputText value="#{evt.typeName}"/>
       </rich:column>
       ...
      


      with the corresponding getRowClasses() method in the bean returning a comma-delimited list of CSS style classes based on the priority of each event. However, upon sorting the table data (by clicking on column headers) the row styles do not "stick" to the rows--even though the data is rearranged, the row styles stay the way they were before the sort.

      What I'd like to be able to do is something like this:

       <rich:dataTable
       id="evttable"
       value="#{eventAction.eventList}"
       var="evt"
       ...
       rowClasses="#{evt.rowClass}">
      


      where the rowClasses attribute would iterate over the eventList collection, as the datatable does when displaying data for each row. I've tried this, but it doesn't work as the rowClasses attribute is expecting a String value rather than a collection.

      The odd thing is, I've played with randomizing the order of the styles in getRowClasses(), and the new styles *are* displayed when the table is re-rendered. It's meaningless, though, unless I can find a way to match the style order with the way the data is sorted. If I could somehow send the sort criteria back to the bean, I could modify getRowClasses() to send back the styles in the correct order.

      Does anyone know of a way to do this? I've tried attaching actions to the column headers, but that doesn't work. Is there a method to retrieve the sort order somehow, either through a datatable attribute (stateVar looked promising, but I can't find any documentation on how to use it) or even through JavaScript?

      Any help would be very much appreciated!

      ZB

        • 1. Re: rich:datatable - Way to sort rowClasses?
          Zebediah Bandersnatch Newbie

          Well, I got it to work, using the external sorting method on the demo site:

           <rich:column id="Priority" sortBy="#{evt.priority}" sortOrder="#{eventAction.priorityDirection}" selfSorted="false">
           <f:facet name="header">
           <a4j:commandLink ajaxSingle="true" value="Priority" reRender="evttable" styleClass="sortHeader">
           <h:graphicImage value="#{eventAction.priorityImage}" width="#{eventAction.priorityImageSize}" height="#{eventAction.priorityImageSize}" styleClass="dr-table-header-sort-img" />
           <a4j:actionparam name="param" value="Priority" assignTo="#{eventAction.sortKey}"/>
           </a4j:commandLink>
           </f:facet>
           <h:outputText value="#{evt.priorityName}"/>
           </rich:column>
          


          Code in the eventAction bean reorders the styles based on the sort key and order. The commandLink and graphicImage stuff is my attempt to mimic the look of the table with selfSorted="true", since I'm using the rich:datatable elsewhere in the application.

          God, what a pain. Dozens of getters and setters, and took ages to figure it out, since there are no examples in the documentation, and the demo site doesn't seem to show the source for the backing bean, just the JSF file. I did finally find the source at http://lists.jboss.org/pipermail/richfaces-svn-commits/2008-March/006862.html by googling for "firstSortOptionValueChanged", several hours after yanking my last remaining hair out.

          I'm new to JSF in general and Richfaces in particular. PLEASE tell me I did this wrong. Nothing would make me happier than a snotty reply indicating that I completely missed some monstrously easy way of doing this, and that I'm an idiot for searching through Subversion commits for backing bean code 'cause it's all really well documented somewhere accessible.