Class \Prado\Web\UI\WebControls\TDataList
TDataList represents a data bound and updatable list control.
Like TRepeater, TDataList displays its content repeatedly based on the data fetched from \Prado\Web\UI\WebControls\setDataSource. The repeated contents in TDataList are called items, which are controls and can be accessed through \Prado\Web\UI\WebControls\getItems. When dataBind() is invoked, TDataList creates an item for each row of data and binds the data row to the item. Optionally, a TDataList can have a header, a footer and/or separators between items.
TDataList differs from TRepeater in that it supports tiling the items in different manners and it maintains status of items to handle data update.
The layout of the repeated contents are specified by inline templates. TDataList items, header, footer, etc. are being instantiated with the corresponding templates when data is being bound to the repeater.
Since v3.1.0, the layout can also be by renderers. A renderer is a control class that can be instantiated as datalist items, header, etc. A renderer can thus be viewed as an external template (in fact, it can also be non-templated controls).
A renderer can be any control class.
- If the class implements IDataRenderer, the Data property will be set as the data row during databinding. Many PRADO controls implement this interface, such as TLabel, TTextBox, etc.
- If the class implements IItemDataRenderer, the ItemIndex property will be set as the zero-based index of the item in the datalist item collection, and the ItemType property as the item's type (such as TListItemType::Item). TDataListItemRenderer may be used as the convenient base class which already implements IDataItemRenderer.
The following properties are used to specify different types of template and renderer for a datalist:
- \Prado\Web\UI\WebControls\setItemTemplate, \Prado\Web\UI\WebControls\setItemRenderer: for each repeated row of data
- \Prado\Web\UI\WebControls\setAlternatingItemTemplate, \Prado\Web\UI\WebControls\setAlternatingItemRenderer: for each alternating row of data. If not set, \Prado\Web\UI\WebControls\setItemTemplate or \Prado\Web\UI\WebControls\setItemRenderer will be used instead.
- \Prado\Web\UI\WebControls\setHeaderTemplate, \Prado\Web\UI\WebControls\setHeaderRenderer: for the datalist header.
- \Prado\Web\UI\WebControls\setFooterTemplate, \Prado\Web\UI\WebControls\setFooterRenderer: for the datalist footer.
- \Prado\Web\UI\WebControls\setSeparatorTemplate, \Prado\Web\UI\WebControls\setSeparatorRenderer: for content to be displayed between items.
- \Prado\Web\UI\WebControls\setEmptyTemplate, \Prado\Web\UI\WebControls\setEmptyRenderer: used when data bound to the datalist is empty.
- \Prado\Web\UI\WebControls\setEditItemTemplate, \Prado\Web\UI\WebControls\setEditItemRenderer: for the row being editted.
- \Prado\Web\UI\WebControls\setSelectedItemTemplate, \Prado\Web\UI\WebControls\setSelectedItemRenderer: for the row being selected.
If a content type is defined with both a template and a renderer, the latter takes precedence.
When dataBind() is being called, TDataList undergoes the following lifecycles for each row of data:
- create item based on templates or renderers
- set the row of data to the item
- raise \Prado\Web\UI\WebControls\onItemCreated:
- add the item as a child control
- call dataBind() of the item
- raise \Prado\Web\UI\WebControls\onItemDataBound:
TDataList raises an \Prado\Web\UI\WebControls\onItemCommand whenever a button control within some datalist item raises a OnCommand event. Therefore, you can handle all sorts of OnCommand event in a central place by writing an event handler for \Prado\Web\UI\WebControls\onItemCommand.
An additional event is raised if the OnCommand event has one of the following command names:
- edit: user wants to edit an item. OnEditCommand event will be raised.
- update: user wants to save the change to an item. OnUpdateCommand event will be raised.
- select: user selects an item. OnSelectedIndexChanged event will be raised.
- delete: user deletes an item. OnDeleteCommand event will be raised.
- cancel: user cancels previously editting action. OnCancelCommand event will be raised.
TDataList provides a few properties to support tiling the items. The number of columns used to display the data items is specified via \Prado\Web\UI\WebControls\setRepeatColumns property, while the \Prado\Web\UI\WebControls\setRepeatDirection governs the order of the items being rendered. The layout of the data items in the list is specified via \Prado\Web\UI\WebControls\setRepeatLayout, which can take one of the following values:
- Table (default): items are organized using HTML table and cells. When using this layout, one can set \Prado\Web\UI\WebControls\setCellPadding and \Prado\Web\UI\WebControls\setCellSpacing to adjust the cellpadding and cellpadding of the table, and \Prado\Web\UI\WebControls\setCaption and \Prado\Web\UI\WebControls\setCaptionAlign to add a table caption with the specified alignment.
- Flow: items are organized using HTML spans and breaks.
- Raw: TDataList does not generate any HTML tags to do the tiling.
Items in TDataList can be in one of the three status: normal browsing, being editted and being selected. To change the status of a particular item, set \Prado\Web\UI\WebControls\setSelectedItemIndex or \Prado\Web\UI\WebControls\setEditItemIndex. The former will change the indicated item to selected mode, which will cause the item to use \Prado\Web\UI\WebControls\setSelectedItemTemplate or \Prado\Web\UI\WebControls\setSelectedItemRenderer for presentation. The latter will change the indicated item to edit mode and to use corresponding template or renderer. Note, if an item is in edit mode, then selecting this item will have no effect.
Different styles may be applied to items in different status. The style application is performed in a hierarchical way: Style in higher hierarchy will inherit from styles in lower hierarchy. Starting from the lowest hierarchy, the item styles include
- item's own style
- \Prado\Web\UI\WebControls\getItemStyle
- \Prado\Web\UI\WebControls\getAlternatingItemStyle
- \Prado\Web\UI\WebControls\getSelectedItemStyle
- \Prado\Web\UI\WebControls\getEditItemStyle. Therefore, if background color is set as red in \Prado\Web\UI\WebControls\getItemStyle, \Prado\Web\UI\WebControls\getEditItemStyle will also have red background color unless it is set to a different value explicitly.
When a page containing a datalist is post back, the datalist will restore automatically all its contents, including items, header, footer and separators. However, the data row associated with each item will not be recovered and become null. To access the data, use one of the following ways:
- Use \Prado\Web\UI\WebControls\getDataKeys to obtain the data key associated with the specified datalist item and use the key to fetch the corresponding data from some persistent storage such as DB.
- Save the whole dataset in viewstate, which will restore the dataset automatically upon postback. Be aware though, if the size of your dataset is big, your page size will become big. Some complex data may also have serializing problem if saved in viewstate.
Class hierarchy
- \Prado\Web\UI\WebControls\TDataList implements INamingContainer, IRepeatInfoUser
- \Prado\Web\UI\WebControls\TBaseDataList
- \Prado\Web\UI\WebControls\TDataBoundControl
- \Prado\Web\UI\WebControls\TWebControl implements IStyleable
- \Prado\Web\UI\TControl implements IRenderable, IBindable
- \Prado\TApplicationComponent
- \Prado\TComponent
Since: 3.0
public
|
bubbleEvent(TControl $sender, TEventParameter $param) : bool
This method overrides parent's implementation to handle
{@see onItemCommand OnItemCommand} event which is bubbled from
datalist items and their child controls.
If the event parameter is TDataListCommandEventParameter and the command name is a recognized one, which includes 'select', 'edit', 'delete', 'update', and 'cancel' (case-insensitive), then a corresponding command event is also raised (such as \Prado\Web\UI\WebControls\onEditCommand). This method should only be used by control developers. |
public
|
generateItemStyle(string $itemType, int $index) : TStyle
Returns a style used for rendering items.
This method is required by IRepeatInfoUser interface. |
public
|
|
public
|
|
public
|
|
public
|
|
public
deprecated
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
getHasFooter() : bool
Returns a value indicating whether this control contains footer item.
This method is required by IRepeatInfoUser interface. |
public
|
getHasHeader() : bool
Returns a value indicating whether this control contains header item.
This method is required by IRepeatInfoUser interface. |
public
|
getHasSeparators() : bool
Returns a value indicating whether this control contains separator items.
This method is required by IRepeatInfoUser interface. |
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
loadState() : mixed
Loads item count information from viewstate.
This method is invoked right after control state is loaded. |
public
|
onCancelCommand(TDataListCommandEventParameter $param) : mixed
Raises <b>OnCancelCommand</b> event.
This method is invoked when a child control of the data list raises an OnCommand event and the command name is 'cancel' (case-insensitive). |
public
|
onDeleteCommand(TDataListCommandEventParameter $param) : mixed
Raises <b>OnDeleteCommand</b> event.
This method is invoked when a child control of the data list raises an OnCommand event and the command name is 'delete' (case-insensitive). |
public
|
onEditCommand(TDataListCommandEventParameter $param) : mixed
Raises <b>OnEditCommand</b> event.
This method is invoked when a child control of the data list raises an OnCommand event and the command name is 'edit' (case-insensitive). |
public
|
onItemCommand(TDataListCommandEventParameter $param) : mixed
Raises <b>OnItemCommand</b> event.
This method is invoked when a child control of the data list raises an OnCommand event. |
public
|
onItemCreated(TDataListItemEventParameter $param) : mixed
Raises <b>OnItemCreated</b> event.
This method is invoked after a data list item is created and instantiated with template, but before added to the page hierarchy. The datalist item control responsible for the event can be determined from the event parameter. If you override this method, be sure to call parent's implementation so that event handlers have chance to respond to the event. |
public
|
onItemDataBound(TDataListItemEventParameter $param) : mixed
Raises <b>OnItemDataBound</b> event.
This method is invoked right after an item is data bound. The datalist item control responsible for the event can be determined from the event parameter. If you override this method, be sure to call parent's implementation so that event handlers have chance to respond to the event. |
public
|
onUpdateCommand(TDataListCommandEventParameter $param) : mixed
Raises <b>OnUpdateCommand</b> event.
This method is invoked when a child control of the data list raises an OnCommand event and the command name is 'update' (case-insensitive). |
public
|
render(THtmlWriter $writer) : mixed
Renders the data list control.
This method overrides the parent implementation. |
public
|
renderItem(THtmlWriter $writer, TRepeatInfo $repeatInfo, string $itemType, int $index) : mixed
Renders an item in the list.
This method is required by IRepeatInfoUser interface. |
public
|
|
public
|
saveState() : mixed
Saves item count in viewstate.
This method is invoked right before control state is to be saved. |
public
|
setAlternatingItemRenderer(string $value) : mixed
Sets the alternative item renderer class.
If not empty, the class will be used to instantiate as alternative datalist items. This property takes precedence over \Prado\Web\UI\WebControls\getAlternatingItemTemplate. |
public
|
|
public
|
|
public
deprecated
|
|
public
|
setEditItemIndex(int $value) : mixed
Edits an item by its index in {@see getItems Items}.
Previously editting item will change to normal item state. If the index is less than 0, any existing edit item will be cleared up. |
public
|
setEditItemRenderer(string $value) : mixed
Sets the renderer class for the datalist item being editted.
If not empty, the class will be used to instantiate as the datalist item. This property takes precedence over \Prado\Web\UI\WebControls\getEditItemTemplate. |
public
|
|
public
|
setEmptyRenderer(string $value) : mixed
Sets the datalist empty renderer class.
The empty renderer is created as the child of the datalist if data bound to the datalist is empty. This property takes precedence over \Prado\Web\UI\WebControls\getEmptyTemplate. |
public
|
|
public
|
setFooterRenderer(string $value) : mixed
Sets the datalist footer renderer class.
If not empty, the class will be used to instantiate as datalist footer item. This property takes precedence over \Prado\Web\UI\WebControls\getFooterTemplate. |
public
|
|
public
|
setHeaderRenderer(string $value) : mixed
Sets the datalist header renderer class.
If not empty, the class will be used to instantiate as datalist header item. This property takes precedence over \Prado\Web\UI\WebControls\getHeaderTemplate. |
public
|
|
public
|
setItemRenderer(string $value) : mixed
Sets the item renderer class.
If not empty, the class will be used to instantiate as datalist items. This property takes precedence over \Prado\Web\UI\WebControls\getItemTemplate. |
public
|
|
public
|
|
public
|
|
public
|
|
public
|
setSelectedItemIndex(int $value) : mixed
Selects an item by its index in {@see getItems Items}.
Previously selected item will be un-selected. If the item to be selected is already in edit mode, it will remain in edit mode. If the index is less than 0, any existing selection will be cleared up. |
public
|
setSelectedItemRenderer(string $value) : mixed
Sets the renderer class for the datalist item being selected.
If not empty, the class will be used to instantiate as the datalist item. This property takes precedence over \Prado\Web\UI\WebControls\getSelectedItemTemplate. |
public
|
|
public
|
setSeparatorRenderer(string $value) : mixed
Sets the datalist item separator renderer class.
If not empty, the class will be used to instantiate as datalist item separators. This property takes precedence over \Prado\Web\UI\WebControls\getSeparatorTemplate. |
public
|
|
public
|
|
public
|
|
protected
|
applyItemStyles() : mixed
Applies styles to items, header, footer and separators.
Item styles are applied in a hierarchical way. Style in higher hierarchy will inherit from styles in lower hierarchy. Starting from the lowest hierarchy, the item styles include item's own style, \Prado\Web\UI\WebControls\getItemStyle, \Prado\Web\UI\WebControls\getAlternatingItemStyle, \Prado\Web\UI\WebControls\getSelectedItemStyle, and \Prado\Web\UI\WebControls\getEditItemStyle. Therefore, if background color is set as red in \Prado\Web\UI\WebControls\getItemStyle, \Prado\Web\UI\WebControls\getEditItemStyle will also have red background color unless it is set to a different value explicitly. |
protected
|
|
protected
|
createItem(int $itemIndex, TListItemType $itemType) : TControl
Creates a datalist item instance based on the item type and index.
|
protected
|
|
protected
|
performDataBinding(Traversable $data) : mixed
Performs databinding to populate data list items from data source.
This method is invoked by dataBind(). You may override this function to provide your own way of data population. |
protected
|
|
private
|
createItemInternal(int $itemIndex, TListItemType $itemType) : TControl
Creates a datalist item.
This method invokes createItem to create a new datalist item. |
private
|
createItemWithDataInternal(int $itemIndex, TListItemType $itemType, mixed $dataItem) : TControl
Creates a datalist item and performs databinding.
This method invokes createItem to create a new datalist item. |
private
|
|
private
|
|
private
|
|
private
|
public
mixed
|
CMD_CANCEL
|
'Cancel'
|
public
mixed
|
CMD_DELETE
|
'Delete'
|
public
mixed
|
CMD_EDIT
|
'Edit'
|
public
mixed
|
CMD_SELECT
Command name that TDataList understands. They are case-insensitive.
|
'Select'
|
public
mixed
|
CMD_UPDATE
|
'Update'
|