ForEach |
||||
Developer’s Guide Home
Installation and Configuration Common Concepts Components Index Border Layout Panel Calendar Chart Command Button Command Link Confirmation Data Table Date Chooser Day Table Drop Down Field Dynamic Image Folding Panel For Each Graphic Text Hint Label Input Text Input Textarea Layered Pane Popup Layer Popup Menu Select Boolean Checkbox Select Many Checkbox Select One Radio Spinner Suggestion Field Tabbed Pane Tab Set Tree Table Two List Selection Window Focus Load Bundle Scroll Position Ajax Framework Validation Framework Tag Reference API Reference |
Key Features
Kinds of IterationThe ForEach component is represented by the <o:forEach> tag with a set of child components and/or HTML markup. The content of the <o:forEach> tag will be rendered several times once for each iteration step according to the iteration parameters specified with <o:forEach> tag's attributes. Note that although ForEach is identical in API to the JSTL <c:forEach> tag, the <o:forEach> tag doesn't create several instances of iteration components as <c:forEach> does. Instead of this, the same child components are rendered several but with different parameters – once per each iteration step. Below are the descriptions of various kinds of iteration possible with the ForEach component. Item IterationThe simplest and probably the most popular way of iteration is iterating over a collection of objects, the inner components of the <o:forEach> tag are rendered once for each object. Here's an example: <o:forEach items="#{MyBean.tasks}" var="task" varStatus="status"> <h:outputText value="#{status.count}. #{task.name}"/> <br/> </o:forEach> As you can see in this example, the collection of items can be specified using the items attribute, and this attribute should be declared as value binding to one of the following types:
The ForEach component will render its content once for each item, and the rendered content can use the current item and current iteration step parameter variables specified with the following attributes:
The iteration status object is of type IterationStatus, and it provides the following information on the current iteration step:
The example above uses the count property for displaying the task number before task name. Item Range IterationThis kind of iteration is actually an extension of the ordinary item iteration feature described above. It is possible to specify restrict the range of displayed items, display items in the reverse order, and specify the iteration step so that every n-th item be rendered. Here's an example that demonstrates all of these possibilities: <o:forEach items="#{MyBean.tasks}" begin="#{MyBean.lastTaskIndex}" end="0" step="-1" var="task" varStatus="status"> <h:outputText value="#{status.count}. (#{status.index}) -- #{task.name}"/> <br/> </o:forEach> The begin and end attributes specify the zero-based indexes that specify the sub-range of the displayed items. These numbers are inclusive which means that both boundaries of the range are included into iteration. The step attribute specifies the index increase step between the successive iteration rounds. It's possible to specify the begin value greater than the end value for the reverse iteration order, but in this case you'll also need to specify a negative step value, as shown in the example above. Note that the count property of the iteration status object always returns values 1, 2, 3, etc... regardless of the range settings, and the index property returns the actual zero-based collection index for the current iteration step, so in case of three-item collection, it will take the values of 2, 1, 0 in the example above. Pure Number IterationIt is possible not to specify the collection of items, and just specify the begin, end, and optionally step attributes for the <o:forEach> tag, which will perform the iteration similar to how it is described in the previous section but without using the collection items. The count and index iteration parameters will still have the same meaning as described in the previous section, but the value of the var variable (and the current property in iteration status) will return null. Here's a simple example of number-based, which creates five <div> tags with a 1-based number within each of them: <o:forEach begin="1" end="5" varStatus="status"> <div>#{status.index}</div> </o:forEach> |
|||
© 2009 TeamDev Ltd. | ![]() |