duminică, 1 noiembrie 2015

[OmniFaces utilities (2.0)] Split the given array into an array of subarrays of the given fragment size


[OmniFaces utilities] The splitArray() function splits the given array into an array of subarrays 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 array of colors:

@Named
@ViewScoped
public class ColorsBean implements Serializable {

 private String[] colors = new String[9];       

 public ColorsBean() {
  colors[0] = "#045FB4";
  colors[1] = "#F781BE";
  colors[2] = "#0B3B0B";
  colors[3] = "#F7819F";
  colors[4] = "#2E2E2E";
  colors[5] = "#B40404";
  colors[6] = "#04B404";
  colors[7] = "#D0A9F5";
  colors[8] = "#FFFF00";
 }

 public String[] getColors() {
  return colors;
 }

 public void setColors(String[] colors) {
  this.colors = colors;
 }
}

Now, as an example, we shape this array as a 3x3 table like in figure  below:

<table border="1">
 <ui:repeat value="#{of:splitArray(colorsBean.colors, 3)}" var="subArray">
  <tr>
   <ui:repeat value="#{subArray}" var="item">
    <td style="background-color:#{item}">#{item}</td>
   </ui:repeat>
  </tr>
 </ui:repeat>
</table>

Niciun comentariu:

Trimiteți un comentariu