Solution for ui:repeat with varStatus and new parameters "from" and "to"
rehdie Aug 7, 2009 8:25 AMSince I needed a varStatus for ui:repeat, I've create my own version of the ui:repeat tag, which has the following new features:
It now supports the attribute varStatus
. The status object has the following attributes:
- index: the current - zero based - index
- count: the number of iterations. If the count is not available (e.g. for variables of type ResultSet): -1
- last: true, if the current iteration is the last one
- first: true, if the current iteration is the first one
- from, to: for simple for-loops without a collection
example 1:
<ui:repeat value="#{myCollection}" var="item" varStatus="status">
<h:outputText value="...." rendered="#{status.first}"/>
<h:outputText value="...." rendered="#{status.index gt 5}"/>
.......
</ui:repeat>
example 2:
<ui:repeat var="i" varStatus="status" from="5" to="7">
value: #{i}<br/>
index: #{status.index}<br/>
</ui:repeat>
The second example renders the following output:
value: 5 index: 0 value: 6 index: 1 value: 7 index: 2
The sourcecode for the new version of the class UIRepeat can be downloaded from here (unfortunately this forum does not support file uploads).
To use it, you have to add a new entry to the file WEB-INF/faces-config.xml:
<?xml version='1.0' encoding='UTF-8'?> <faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"> <!-- this component entry is new: --> <component> <component-type>facelets.ui.Repeat</component-type> <component-class>com.alturos.gui.facelets.UIRepeat</component-class> </component> <application> <locale-config> <default-locale>de</default-locale> <supported-locale>cs</supported-locale> <supported-locale>de</supported-locale> <supported-locale>en</supported-locale> <supported-locale>fr</supported-locale> <supported-locale>it</supported-locale> <supported-locale>ru</supported-locale> </locale-config> <view-handler>com.sun.facelets.FaceletViewHandler</view-handler> </application> </faces-config>