views:

19

answers:

1

How to skip some rows to be displayed using dataTable:

<h:dataTable cellspacing="0" id="dogs" value="#{dogBean.dogs}" var="dog" rendered="#{dogBeans.dogs != null}">

<h:column id="nameColumn">

    <h:outputText value="#{dog.name}"/>
</h:column>

<h:column id="breedColumn">

    <h:outputText value="#{dog.breed}"/>
</h:column>

</h:dataTable>

I want to display all dogs, but those how have an age greater than 10. dog.age > 10.

I'm using Seam.

+2  A: 

You can't do this nicely in the view side. You can at highest set the rendered attribute of every cell contents to false, but this doesn't avoid the <tr> element being rendered. You would see a blank row and its appearance may not be consistent among browsers.

Best is to filter the rows in the getter.

public List<Dog> getDogsOlderThan10() {
    List<Dog> dogsOlderThan10 = new ArrayList<Dog>();
    for (Dog dog : dogs) {
        if (dog.getAge() > 10) dogsOlderThan10.add(dog);
    }
    return dogsOlderThan10;
}  
BalusC
Nice piece of code. And it answers my initial question. jsf dataTable do not allow any sort of filtering on each element.
Marc
An issue with that though. my collection is actually a List, and this filter method returns a Collection (filtered collection implementation), now casting it to a list obviously fails.
Marc
Ah geez, fixed. I however wondered where's the `Lists#filter()` in Google Collections?
BalusC
As transform() is in the Lists package, I would guess Lists filter has not been implemented.Your new code is correct, but new allocation now :S
Marc
Yes indeed, forget it. Just loop over it yourself.
BalusC