luni, 2 noiembrie 2015

[OmniFaces utilities (2.0)] Split the given list into a list of sublists of the given fragment size


[OmniFaces utilities] The splitList() function splits the given list into a list of sublists of the given fragment size. This is useful for creating nested <ui:repeat> structures, for example, when positioning a list of items into a grid based layout system such as Twitter Bootstrap.

Function:
Usage:
Let's suppose that we have a managed bean named ColorsBean that contains the following list of colors:

@Named
@ViewScoped
public class ColorsBean implements Serializable {

 List<String> colors = new ArrayList<>();

 public ColorsBean() {
  colors.add("#045FB4");
  colors.add("#F781BE");
  colors.add("#0B3B0B");
  colors.add("#F7819F");
  colors.add("#2E2E2E");
  colors.add("#B40404");
  colors.add("#04B404");
  colors.add("#D0A9F5");
  colors.add("#FFFF00");
 }

 public List<String> getColors() {
  return colors;
 }

 public void setColors(List<String> colors) {
  this.colors = colors;
 }
}

Now, as an example, we shape this list via <ul> and <li> like in figure  below:

<ul>
 <ui:repeat value="#{of:splitList(colorsBean.colors, 3)}" var="subList">
  <li>
   <ui:repeat value="#{subList}" var="item">
    <div style="color:#{item}">#{item}</div>
   </ui:repeat>
  </li>
 </ui:repeat>
</ul>

Niciun comentariu:

Trimiteți un comentariu