A project of The Apache Software Foundation
Navigation

How to render a dynamic number of items

Learn how to repeat a simple HTML fragment once per item using RepeatingView

Component org.apache.wicket.markup.repeater.RepeatingView is a container which renders its children components using the tag it is bound to. It can contain an arbitrary number of children elements and we can obtain a new valid id for a new child calling its method newChildId(). This component is particularly suited when we have to repeat a simple markup fragment, for example when we want to display some items as a HTML list:

HTML:

<ul>
    <li wicket:id="listItems"></li>
</ul>

Java Code:

RepeatingView listItems = new RepeatingView("listItems");

listItems.add(new Label(listItems.newChildId(), "green"));
listItems.add(new Label(listItems.newChildId(), "blue"));
listItems.add(new Label(listItems.newChildId(), "red"));

Generated markup:

<ul>
    <li>green</li>
    <li>blue</li>
    <li>red</li>
</ul>

As we can see in this example, each child component has been rendered using the parent markup as if it was its own.