How to use <ui:repeat> to iterate over a nested list?

user2228591 picture user2228591 · Mar 31, 2013 · Viewed 38k times · Source

Using JSF 2.0, I need to display a table wherein each row contains a link which opens a popup. I have two models: A which has id and List<B> properties and B which has id and name properties. In my backing bean, I have a List<A> property. In my view, I am using <ui:repeat> to iterate over List<A>.

The requirement is, depending on the row that the user clicks, the corresponding List<B> of A needs to be displayed. However, the <ui:repeat> does not accept a nested list to be assigned in the var attribute. Hence, I need to do a lot of workarounds which is not efficient.

How do I efficiently solve this problem?

Answer

skuntsel picture skuntsel · Mar 31, 2013

What you need is to nest another <ui:repeat> tag in your outer iteration:

<ui:repeat value="#{bean.listOfA}" var="a">
    ...
    <ui:repeat value="#{a.listOfB}" var="b">
        ...
    </ui:repeat>
</ui:repeat>

The only thing left that is worth noting is that nested <ui:repeat> tags used to have problems with state management until Mojarra 2.1.15 version (details in jsf listener not called inside nested ui:repeat and in many not so recent questions and their answers), which could result in action listeners not called, etc. but if you're currently on the latest Mojarra JSF implementation - just skip this part altogether.