Color the rows of datatable based a condition in JSF 2

谁都会走 提交于 2019-12-18 06:11:31

问题


I'd like to change the background color of rows based on a condition.

<t:dataTable id="data"
                styleClass="history-table"
                headerClass="history-table-header"
                rowClasses="history-table-row-default"
                border="2" cellpadding="5" cellspacing="2"
                var="entry"
                value="#{historyBean.logEntryList}"
                preserveDataModel="false"
                rows="#{historyBean.history.rowCount}"
                sortable="true">

           <h:column>
               <f:facet name="header">
                 <h:outputText value="Debug Status" />
               </f:facet>
               <h:outputText value="#{entry.action}" />
           </h:column>

If the value of "entry.action" is X I like to use "history-table-row-incomplete" (name of styleclass), if the value is Y I like to use "history-table-row-error" (name of styleclass). All other cases should use the default value.

I guess i have to get the current object of entry somehow to my bean, analyze it and return a string with the name of the stylclass to outputText to change the color. But I don't know how... (I'm new in JSF...)

Can someone help me please?


回答1:


Use the rowStyleClass attribute of the <t:dataTable> instead of rowClasses. The rowStyleClass is evaluated on a per-row basis where the var="entry" is available, while the rowClasses is only evaluated on a per-table basis.

<t:dataTable ... rowStyleClass="#{entry.action == 'X' ? 'history-table-row-incomplete' : (entry.action == 'Y' ? 'history-table-row-error' : 'history-table-row-default')}">



回答2:


You can use JSF EL Ternary operator, as below:

rowStyleClass="#{entry.action eq X ? 'history-table-row-incomplete' :  (entry.action eq Y ? 'history-table-row-error' : 'default')}"


来源:https://stackoverflow.com/questions/8745017/color-the-rows-of-datatable-based-a-condition-in-jsf-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!