A ListView is a repeater that makes it easy to display/work with {@link List}s. However, there are situations where it is necessary to work with other collection types, for repeaters that might work better with non-list or database-driven collections see the org.apache.wicket.markup.repeater package. Also notice that in a list the item's uniqueness/primary key/id is identified as its index in the list. If this is not the case you should either override {@link #getListItemModel(IModel,int)}to return a model that will work with the item's true primary key, or use a different repeater that does not rely on the list index. A ListView holds ListItem children. Items can be re-ordered and deleted, either one at a time or many at a time.
Example:
<tbody> <tr wicket:id="rows" class="even"> <td><span wicket:id="id">Test ID</span></td> ...
Though this example is about a HTML table, ListView is not at all limited to HTML tables. Any kind of list can be rendered using ListView.
The related Java code:
add(new ListView<UserDetails>("rows", listData) { public void populateItem(final ListItem<UserDetails> item) { final UserDetails user = item.getModelObject(); item.add(new Label("id", user.getId())); } });
NOTE: When you want to change the default generated markup it is important to realize that the ListView instance itself does not correspond to any markup, however, the generated ListItems do.
This means that methods like {@link #setRenderBodyOnly(boolean)} and{@link #add(org.apache.wicket.behavior.Behavior)} should be invoked on the {@link ListItem}that is given in {@link #populateItem(ListItem)} method.
WARNING: though you can nest ListViews within Forms, you HAVE to set the setReuseItems property to true in order to have validation work properly. By default, setReuseItems is false, which has the effect that ListView replaces all child components by new instances. The idea behind this is that you always render the fresh data, and as people usually use ListViews for displaying read-only lists (at least, that's what we think), this is good default behavior.
However, as the components are replaced before the rendering starts, the search for specific messages for these components fails as they are replaced with other instances. Another problem is that 'wrong' user input is kept as (temporary) instance data of the components. As these components are replaced by new ones, your user will never see the wrong data when setReuseItems is false.
@author Jonathan Locke
@author Juergen Donnerstag
@author Johan Compagner
@author Eelco Hillenius
@param < T> Model object type