3

I'm trying to set the color of a row to red using the dataTable's rowStyleClass attribute. The condition is comparing a enum, so if invoice.status eq InvoiceStatus.CANCELLED, then it should apply the css class 'cancelled' to the row. I think this might not be the proper way to compare an enum with EL. Can you tell me how to do it?


        <p:dataTable id="invoicesTable" widgetVar="invoicesTable" 
                                        value="#{invoiceManager.invoices}"
                                        var="invoice" 
                                        filteredValue = #{invoiceManager.filteredInvoices}"
                                        paginator="true" 
                                        rows="15" 
                                        paginatorPosition="bottom"
                                        paginatorAlwaysVisible="false"
                                        emptyMessage="#{msg['warning.noData']}"
           HERE's THE PROBLEM  -->      rowStyleClass="#{invoice.status eq CANCELLED ? 'cancelled' : null}">
            <f:facet name="header">
                <h:outputText value="#{msg['title.invoices']}" />
                <p:outputPanel style="position: relative; left: 42%;">
                    <h:inputText id="globalFilter" onkeyup="invoicesTable.filter()" />
                </p:outputPanel>                
            </f:facet>

            <p:column headerText="#{msg['label.number']}" filterBy="#{invoice.number}" filterStyle="display: none;">
                <h:outputText value="#{invoice.number}" />
            </p:column>

            <p:column headerText="#{msg['label.customerName']}">
                <h:outputText value="#{invoice.customer.name}" />
            </p:column>

            <p:column headerText="#{msg['label.action']}" styleClass="actionsColumn">
                <p:commandButton process="@this" action="confirmInvoice" icon="ui-icon-search" title="#{msg['button.viewInvoice']}" >
                    <f:setPropertyActionListener target="#{invoiceManager.invoice}" value="#{invoice}" />
                </p:commandButton>
                <p:commandButton process="@this" action="#{invoiceManager.changeInvoiceStatus}" icon="ui-icon-flag" title="#{msg['button.changeInvoiceStatus']}" >
                    <f:setPropertyActionListener target="#{invoiceManager.invoice}" value="#{invoice}" />
                </p:commandButton>
            </p:column>
        </p:dataTable>
Rafael Companhoni
  • 1,780
  • 1
  • 15
  • 31

1 Answers1

5

Enums are in EL interpreted as strings. You need to quote the enum value.

rowStyleClass="#{invoice.status eq 'CANCELLED' ? 'cancelled' : null}"

Alternatively, you could add a new method to the enum,

public String getStyleClass() {
    return name().toLowerCase();
}

and use it as follows

rowStyleClass="#{invoice.status.styleClass}"
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BalsusC Cant one just go with the normal java syntax too? E.g `invoice.status eq StatusEnum.Cancelled` – Sebastian Hoffmann Mar 22 '13 at 15:28
  • 1
    @Paranaix: No. You can however use a taghandler to import enum constants into EL scope. E.g. OmniFaces ``. See also showcase: http://showcase.omnifaces.org/taghandlers/importConstants After having declared ``, you could use `#{invoice.status eq Status.CANCELLED ? 'cancelled' : null}`. – BalusC Mar 22 '13 at 15:29
  • @BalsusC Ah right, some time passed since i dealt with jsf ^^ Rember that i used OmnniFaces especially for this usecase – Sebastian Hoffmann Mar 22 '13 at 15:30