2

I have a ui:repeat that returns a list of entity. Is there any way to know that the returned list is empty?

<ui:repeat id="resulta" value="#{testController.testList}" var="list"> 
   <div>list.name</div>
</ui:repeat>

something like if ui:repeat is empty then display a div saying "List is empty"

i've heard about varStatus -> Facelets repeat Tag Index

but i don't think there is something for empty list. or is there?

UPDATED

<ui:repeat id="resulta" value="#{testController.testList}" var="list"> 
   <div>list.name</div>
    <h:panelGroup rendered="#{empty list}">
        list is empty!
    </h:panelGroup>
</ui:repeat>

I tried to render the "list is empty!" when the list is empty but then it doesn't show.

Community
  • 1
  • 1
galao
  • 1,281
  • 8
  • 26
  • 50

2 Answers2

8
<ui:repeat id="resulta" 
           value="#{testController.testList}" 
           var="list">
    <div>
        #{list.name}
    </div>

</ui:repeat>

<h:panelGroup rendered="#{empty testController.testList}">
    List is empty!
</h:panelGroup>

rendered is a conditional statement which only if it is true renders. In case you want to render the last h:panelGroup as a div instead of a span, consider adding layout='block' to the element.

Menno
  • 12,175
  • 14
  • 56
  • 88
  • i see, but what if i want to render something saying that the list is empty? i updated my code above, but it doesnt work. please have a look. thanks! – galao May 16 '13 at 06:10
  • Add your just after the and invert the rendered parameter from ! – Alexandre Lavoie May 16 '13 at 06:14
  • As Alexandre already stated, just add a panel (edit on my answer for more info). In your provided example the panel never renders because it never loops (since the list is empty). – Menno May 16 '13 at 06:30
1

You can display your empty list message outside your <ui:repeat> element as:

<ui:repeat id="resulta" 
       value="#{testController.testList}" 
       var="list" 
       rendered="#{not empty testController.testList}">
    <div>
        #{list.name}
    </div>
</ui:repeat>
<h:panelGroup rendered="#{empty testController.testList}">
    list is empty!
</h:panelGroup>
Tasos P.
  • 3,994
  • 2
  • 21
  • 41