DataTable control: Zebra striping

UI Components for JSF
foolshat
Posts: 59
Joined: 03 Nov 2010, 21:36

03 Nov 2010, 21:47

The DataTable is a great control. My client would like the rows "zebra striped" i.e alternating colours. I could not find anything to do this, other than using conditional colouring with the rowStyleClass attribute and an expression.
This seems overkill for this particular usecase, is there a simpler and more CPU efficient way ?
PrimeFaces 2.2 RC2 , Glassfish3.0.1 Mojarra 2.0.2, Netbeans 6.9.1 Java, Scala and AKKA

callahan
Posts: 768
Joined: 27 May 2010, 22:52

03 Nov 2010, 22:12

It's the only way that I'm aware of. It wouldn't worry about cpu cycles, after all, a data table rarely displays more that 10 or 20 rows at the same time :) .

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

03 Nov 2010, 23:41

You can create a feature request for this, we haven't added alternating rows as ThemeRoller has none. Still rowStyleClass can be used though.

User avatar
bumble.bee
Posts: 723
Joined: 29 Sep 2010, 21:39
Location: United States

05 Nov 2010, 14:32

floolshat - did you actually get this to work? It would seem zebra striping is not available at all due to the bug mentioned here:

http://primefaces.prime.com.tr/forum/vi ... f=3&t=5753

foolshat
Posts: 59
Joined: 03 Nov 2010, 21:36

05 Nov 2010, 14:51

No. It is nearly working. I found another posting which suggests using the following attribute on the datatable.

Code: Select all

  rowStyleClass="row-#{rowIndex % 2}"
but, the problem is the first two <tr> have a class of row-0, rather than row-0 then row-1. So I have left it for the moment.
PrimeFaces 2.2 RC2 , Glassfish3.0.1 Mojarra 2.0.2, Netbeans 6.9.1 Java, Scala and AKKA

callahan
Posts: 768
Joined: 27 May 2010, 22:52

05 Nov 2010, 15:50

If the DataTables rows attribute is set to an even number, the following should work:

Code: Select all

      <p:dataTable
          ...
          rows="6"
          rowIndexVar="rowIx"
          rowStyleClass="#{empty rowIx or rowIx mod 2 ne 0 ? 'even-row' : 'odd-row'}"
        >

foolshat
Posts: 59
Joined: 03 Nov 2010, 21:36

09 Nov 2010, 16:24

Thanks. Yes, at the time my table had 5 rows. They are not even and I get the alternate odd/even required.
PrimeFaces 2.2 RC2 , Glassfish3.0.1 Mojarra 2.0.2, Netbeans 6.9.1 Java, Scala and AKKA

callahan
Posts: 768
Joined: 27 May 2010, 22:52

09 Nov 2010, 16:46

This issue with the unusual behavior of rowIndexVar in the rowStyleClass has been fixed http://code.google.com/p/primefaces/iss ... il?id=1408. This means that

Code: Select all

<p:dataTable
          ...
          rows="6"
          rowIndexVar="rowIx"
          rowStyleClass="#{rowIx mod 2 ne 0 ? 'even-row' : 'odd-row'}"
        >
should work correctly now.

ethermion
Posts: 117
Joined: 25 Aug 2010, 15:11

09 Nov 2010, 17:28

Can somebody post the rest of the story on this? I am clueless on styles, so if somebody could throw out the correct style syntax for coloring rows, I would be grateful.
-e
Mojarra 2.2.8
Primefaces 5.1
Wildfly 8.2

foolshat
Posts: 59
Joined: 03 Nov 2010, 21:36

11 Nov 2010, 16:23

My datatable looks like this. The rowStyleClass discussed above, works fine. You then have to modify your css to provide the styles

Code: Select all

    <p:dataTable id="resultstable"
                 var="row"
                 value="#{p_entity.dataRows}"
                 paginator="true"
                 rows="20"
                 dynamic="true"
                 lazy="true"
                 
                 paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                 rowsPerPageTemplate="10,20,30,40,50"
                 rowStyleClass="#{empty rowIx or rowIx mod 2 ne 0 ? 'even-row' : 'odd-row'}"
                 rowIndexVar="rowIx"
                 rowEditListener="#{p_entity.rowEditListener}"
                 onRowEditUpdate ="growlmessages"
                 loadingMessage="#{resources.datatable_loading}"
                 emptyMessage="#{resources.datatable_no_data_found}"
                 selection="#{p_entity.selectedItems}"                 
                 >


style.css contains this
.ui-datatable .ui-datatable-data .even-row td{
background-color:#DDE3FD;
}

.ui-datatable .ui-datatable-data .odd-row td{
background-color:#C9D1F7;
}
PrimeFaces 2.2 RC2 , Glassfish3.0.1 Mojarra 2.0.2, Netbeans 6.9.1 Java, Scala and AKKA

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: Google [Bot] and 19 guests