Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally display row using JSF Datatable

Tags:

datatable

jsf

I have some JSF code that currently works (as shown below), and I need to modify it to conditionally suppress the display of certain rows of the table. I know how to conditionally suppress the display of a particular cell, but that seems to create an empty cell, while what I'm trying to do is to not display the row at all.

Any suggestions?

<h:dataTable styleClass="resultsTable" id="t1" value="#{r.common}" var="com" headerClass="headerBackgrnd" rowClasses="rowOdd, rowEven" columnClasses="leftAlign, rightAlign, leftAlign">
    <h:column>
        <h:outputText rendered="#{com.rendered}" styleClass="inputText" value="#{com.description}: " />
    </h:column>
    <h:column>
        <h:outputText styleClass="outputText" value="#{com.v1}" />
    </h:column>
    <h:column>
        <h:inputText styleClass="inputText" value="#{com.v2}" />
   </h:column>
</h:dataTable>

Basically, the line that says #{com.rendered} will conditionally display the contents of a single cell, producing an empty cell when com.rendered is false. But I want to skip an entire row of the display under certain conditions - how would I go about doing that?

like image 392
Elie Avatar asked May 13 '10 18:05

Elie


Video Answer


2 Answers

Rows correspond to data objects in the collection of your table. If you don't want the row, don't put the object in the collection.

Alternatively, you can use the rowClasses parameter for dataTable.

Bean code:

public String getRowClasses() {
    StringBuilder sb = new StringBuilder();
    for (Data data : myData) {
        sb.append(data.hide ? 'hide,' : 'show,');
    }
    return sb.toString();
}

CSS:

tr.hide {display:none;}
like image 170
Naganalf Avatar answered Sep 27 '22 02:09

Naganalf


For people using richFaces, you can use rich:column's filterExpression attribute.

<rich:column filterExpression="#{put your expression here}">
    ...
</rich>

If the condition is not met, the complete row is filtered out.

Example is using seam EL!

like image 41
Ernst-Jan Avatar answered Sep 23 '22 02:09

Ernst-Jan