marți, 13 octombrie 2015

[OmniFaces utilities (2.0)] Convert a Iterable<E> to a List<E>


[OmniFaces utilities] The iterableToList() function converts a Iterable<E> to a List<E>. Useful when you want to iterate over an Iterable, which includes any type of Collection (which includes e.g. a Set) in for example <ui:repeat> and <h:dataTable>.

Note When iterating specifically over a Set use Converters#setToList(Set) is an alternative to this.

Function:
Usage:

Beside the of:setToList(), OmniFaces provides a generic function for Iterables named, of:iterableToList(). This is useful when you need to iterate over an Iterable (e.g. Set), which includes any type of Collection. Especially in JSF 2.0/2.1 where <h:dataTable> and <ui:repeat> doesn't support Iterable directly. For example, via this method you can easily use #{fooBean.iterable} in <h:dataTable>:

Queue<Players> queue = new PriorityQueue<>();

<h:dataTable value="#{of:iterableToList(playersBean.queue)}" var="t">
 ...
</h:dataTable>

Starting with JSF 2.2 the <h:dataTable> support  Iterable directly, so you don't need of:iterableToList() anymore. You can simply write:

<h:dataTable value="#{playersBean.queue}" var="t">
...
</h:dataTable>

Well, <ui:repeat> still cannot directly use Iterable in JSF 2.2, so you can use of:iterableToList() or, via EL 2.2, toArray() method. Check the below examples for queue:

<ui:repeat value="#{of:iterableToList(playersBean.queue)}" var="t">
 ...
</ui:repeat>

<ui:repeat value="#{playersBean.queue.toArray()}" var="t">
 ...

Niciun comentariu:

Trimiteți un comentariu