Class ListView<T>
- Type Parameters:
T
- type of elements contained in the model's list
- All Implemented Interfaces:
Serializable
,Iterable<Component>
,IEventSink
,IEventSource
,IFeedbackContributor
,IConverterLocator
,IMetadataContext<Serializable,
,Component> IHeaderContributor
,IRequestableComponent
,IHierarchical<Component>
,IClusterable
- Direct Known Subclasses:
PageableListView
,PropertyListView
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 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 Component.setRenderBodyOnly(boolean)
and
Component.add(org.apache.wicket.behavior.Behavior...)
should be invoked on the ListItem
that is given in 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, Juergen Donnerstag, Johan Compagner, Eelco Hillenius
- See Also:
-
Field Summary
Fields inherited from class org.apache.wicket.Component
ENABLE, FLAG_INITIALIZED, FLAG_RESERVED1, FLAG_RESERVED2, FLAG_RESERVED3, FLAG_RESERVED4, FLAG_RESERVED5, FLAG_RESERVED8, PARENT_PATH, PATH_SEPARATOR, RENDER, RFLAG_CONTAINER_DEQUEING, RFLAG_CONTAINER_HAS_REMOVALS
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiongetList()
Gets the list of items in the listView.getListItemModel
(IModel<? extends List<T>> listViewModel, int index) Subclasses may provide their own ListItemModel with extended functionality.getModel()
Gets modelGets model objectboolean
If true re-rendering the list view is more efficient if the windows doesn't get changed at all or if it gets scrolled (compared to paging).final int
Get index of first cell in page.int
Based on the model object's list size, firstIndex and view size, determine what the view size really will be.moveDownLink
(String id, ListItem<T> item) Returns a link that will move the given item "down" (towards the end) in the listView.moveUpLink
(String id, ListItem<T> item) Returns a link that will move the given item "up" (towards the beginning) in the listView.Create a new ListItem for list item at index.protected void
onBeginPopulateItem
(ListItem<T> item) Comes handy for ready made ListView based components which must implement populateItem() but you don't want to lose compile time error checking reminding the user to implement abstract populateItem().protected final void
Callback to let the repeater know it should populate itself with its items.protected abstract void
populateItem
(ListItem<T> item) Populate a given item.removeLink
(String id, ListItem<T> item) Returns a link that will remove this ListItem from the ListView that holds it.protected final void
renderChild
(Component child) Render a single child.protected void
renderItem
(ListItem<?> item) Render a single item.Returns an iterator for the collection of child components to be rendered.Sets the model as the provided list and removes all children, so that the next render will be using the contents of the model.final void
Sets modelfinal void
setModelObject
(List<T> object) Sets model objectsetReuseItems
(boolean reuseItems) If true re-rendering the list view is more efficient if the windows doesn't get changed at all or if it gets scrolled (compared to paging).setStartIndex
(int startIndex) Set the index of the first item to rendersetViewSize
(int size) Define the maximum number of items to render.Methods inherited from class org.apache.wicket.markup.repeater.AbstractRepeater
dequeue, getMarkup, onBeforeRender, onRender
Methods inherited from class org.apache.wicket.markup.html.WebMarkupContainer
getWebApplication, getWebPage, getWebRequest, getWebResponse, getWebSession
Methods inherited from class org.apache.wicket.MarkupContainer
add, addDequeuedComponent, addOrReplace, autoAdd, canDequeueTag, contains, dequeue, dequeuePreamble, findChildComponent, findComponentToDequeue, get, getAssociatedMarkup, getAssociatedMarkupStream, getMarkupType, getRegionMarkup, internalAdd, internalInitialize, iterator, iterator, newDequeueContext, onComponentTagBody, onDetach, onInitialize, queue, remove, remove, removeAll, renderAll, renderAssociatedMarkup, renderNext, replace, setDefaultModel, size, stream, streamChildren, toString, toString, visitChildren, visitChildren
Methods inherited from class org.apache.wicket.Component
add, addStateChange, beforeRender, canCallListener, canCallListenerAfterExpiry, checkComponentTag, checkComponentTagAttribute, checkHierarchyChange, clearOriginalDestination, configure, continueToOriginalDestination, createConverter, debug, detach, detachModel, detachModels, determineVisibility, error, exceptionMessage, fatal, findMarkupStream, findPage, findParent, findParentWithAssociatedMarkup, getAjaxRegionMarkupId, getApplication, getBehaviorById, getBehaviorId, getBehaviors, getBehaviors, getClassRelativePath, getConverter, getDefaultModel, getDefaultModelObject, getDefaultModelObjectAsString, getDefaultModelObjectAsString, getEscapeModelStrings, getFeedbackMessages, getFlag, getId, getInnermostModel, getInnermostModel, getLocale, getLocalizer, getMarkup, getMarkupAttributes, getMarkupId, getMarkupId, getMarkupIdFromMarkup, getMarkupIdImpl, getMarkupSourcingStrategy, getMarkupTag, getMetaData, getModelComparator, getOutputMarkupId, getOutputMarkupPlaceholderTag, getPage, getPageRelativePath, getParent, getPath, getRenderBodyOnly, getRequest, getRequestCycle, getRequestFlag, getResponse, getSession, getSizeInBytes, getStatelessHint, getString, getString, getString, getStyle, getVariation, hasBeenRendered, hasErrorMessage, hasFeedbackMessage, info, initModel, internalOnModelChanged, internalRenderComponent, internalRenderHead, isActionAuthorized, isAuto, isBehaviorAccepted, isEnableAllowed, isEnabled, isEnabledInHierarchy, isIgnoreAttributeModifier, isInitialized, isRenderAllowed, isRendering, isStateless, isVersioned, isVisibilityAllowed, isVisible, isVisibleInHierarchy, markRendering, modelChanged, modelChanging, newMarkupSourcingStrategy, onAfterRender, onComponentTag, onConfigure, onEvent, onModelChanged, onModelChanging, onReAdd, onRemove, redirectToInterceptPage, remove, remove, render, renderComponentTag, rendered, renderHead, renderPart, renderPlaceholderTag, replaceComponentTagBody, replaceWith, sameInnermostModel, sameInnermostModel, send, setAuto, setDefaultModelObject, setEnabled, setEscapeModelStrings, setFlag, setIgnoreAttributeModifier, setMarkup, setMarkupId, setMarkupIdImpl, setMetaData, setOutputMarkupId, setOutputMarkupPlaceholderTag, setParent, setRenderBodyOnly, setResponsePage, setResponsePage, setResponsePage, setVersioned, setVisibilityAllowed, setVisible, success, urlFor, urlFor, urlFor, urlForListener, urlForListener, visitParents, visitParents, warn, wrap
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
ListView
- See Also:
-
ListView
- Parameters:
id
- component idmodel
- model containing a list of- See Also:
-
ListView
- Parameters:
id
- See Componentlist
- List to cast to Serializable- See Also:
-
-
Method Details
-
getList
Gets the list of items in the listView. This method is final because it is not designed to be overridden. If it were allowed to be overridden, the values returned by getModelObject() and getList() might not coincide.- Returns:
- The list of items in this list view.
-
getReuseItems
If true re-rendering the list view is more efficient if the windows doesn't get changed at all or if it gets scrolled (compared to paging). But if you modify the listView model object, then you must manually call listView.removeAll() in order to rebuild the ListItems. If you nest a ListView in a Form, ALLWAYS set this property to true, as otherwise validation will not work properly.- Returns:
- Whether to reuse items
-
getStartIndex
Get index of first cell in page. Default is: 0.- Returns:
- Index of first cell in page. Default is: 0
-
getViewSize
Based on the model object's list size, firstIndex and view size, determine what the view size really will be. E.g. default for viewSize is Integer.MAX_VALUE, if not set via setViewSize(). If the underlying list has 10 elements, the value returned by getViewSize() will be 10 if startIndex = 0.- Returns:
- The number of items to be populated and rendered.
-
moveDownLink
Returns a link that will move the given item "down" (towards the end) in the listView.- Parameters:
id
- Name of move-down link component to createitem
-- Returns:
- The link component
-
moveUpLink
Returns a link that will move the given item "up" (towards the beginning) in the listView.- Parameters:
id
- Name of move-up link component to createitem
-- Returns:
- The link component
-
removeLink
Returns a link that will remove this ListItem from the ListView that holds it.- Parameters:
id
- Name of remove link component to createitem
-- Returns:
- The link component
-
setList
Sets the model as the provided list and removes all children, so that the next render will be using the contents of the model.- Parameters:
list
- The list for the new model. The list must implementSerializable
.- Returns:
- This for chaining
-
setReuseItems
If true re-rendering the list view is more efficient if the windows doesn't get changed at all or if it gets scrolled (compared to paging). But if you modify the listView model object, than you must manually call listView.removeAll() in order to rebuild the ListItems. If you nest a ListView in a Form, always set this property to true, as otherwise validation will not work properly.- Parameters:
reuseItems
- Whether to reuse the child items.- Returns:
- this
-
setStartIndex
Set the index of the first item to render- Parameters:
startIndex
- First index of model object's list to display- Returns:
- This
-
setViewSize
Define the maximum number of items to render. Default: render all.- Parameters:
size
- Number of items to display- Returns:
- This
-
getListItemModel
Subclasses may provide their own ListItemModel with extended functionality. The default ListItemModel works fine with mostly static lists where index remains valid. In cases where the underlying list changes a lot (many users using the application), it may not longer be appropriate. In that case your own ListItemModel implementation should use an id (e.g. the database' record id) to identify and load the list item model object.- Parameters:
listViewModel
- The ListView's modelindex
- The list item index- Returns:
- The ListItemModel created
-
newItem
Create a new ListItem for list item at index.- Parameters:
index
-itemModel
- object in the list that the item represents- Returns:
- ListItem
-
onPopulate
Description copied from class:AbstractRepeater
Callback to let the repeater know it should populate itself with its items.- Specified by:
onPopulate
in classAbstractRepeater
- See Also:
-
onBeginPopulateItem
Comes handy for ready made ListView based components which must implement populateItem() but you don't want to lose compile time error checking reminding the user to implement abstract populateItem().- Parameters:
item
-
-
populateItem
Populate a given item.be careful to add any components to the list item. So, don't do:
add(new Label("foo", "bar"));
but:item.add(new Label("foo", "bar"));
- Parameters:
item
- The item to populate
-
renderChild
Description copied from class:AbstractRepeater
Render a single child. This method can be overridden to modify how a single child component is rendered.- Overrides:
renderChild
in classAbstractRepeater
- Parameters:
child
- Child component to be rendered- See Also:
-
renderItem
Render a single item.- Parameters:
item
- The item to be rendered
-
renderIterator
Description copied from class:AbstractRepeater
Returns an iterator for the collection of child components to be rendered. Users can override this to change order of rendered children.- Specified by:
renderIterator
in classAbstractRepeater
- Returns:
- iterator over child components to be rendered
- See Also:
-
getModel
Gets model- Returns:
- model
-
setModel
Sets model- Parameters:
model
-
-
getModelObject
Gets model object- Returns:
- model object
-
setModelObject
Sets model object- Parameters:
object
-
-