tags:

views:

305

answers:

2

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?

+2  A: 

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;}
Naganalf
Sorry, that's not really an option - this table displays certain application-wide reference data, except in this particular instance that data can only be displayed under certain conditions.
Elie
Two collections then, one with all the data, the second with the stuff for display?
Naganalf
The issue is that I have 2 rows, and depending on the case, sometimes display row A, sometimes row B, and sometimes both. To keep multiple sets would mean that I have to keep three versions of this, which is not ideal. I'm hoping there's another way to do this.
Elie
It would still only be 2 collections, you just change the contents of the display collection as needed.But there is a way to do it programatically with a single collection if you don't mind putting a little V in your M. Edited above.
Naganalf
Naganalf is right. Filter it at the bean level. Iterate over the main `List` and collect only the entries of interest in another `List`. It shouldn't be that expensive since a `List` only contains references.
BalusC
A: 

I've successfully hidden rows by putting a rendered attribute in all the <h:column> tags. The problem is that it suppresses the table headers. You're table has no table headers (they are <f:facet name="header"> tags embedded in the <h:column>), so this approach might work for you.

I ended up using multiple lists in the backing bean, as I needed the table headers.

Brian Leathem