Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style ENUM in Primefaces datatable

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>
like image 850
Rafael Companhoni Avatar asked May 08 '26 15:05

Rafael Companhoni


1 Answers

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}"
like image 95
BalusC Avatar answered May 11 '26 05:05

BalusC