Class \Prado\Web\UI\WebControls\TDataGrid
TDataGrid represents a data bound and updatable grid control.
To populate data into the datagrid, sets its \Prado\Web\UI\WebControls\setDataSource to a tabular data source and call dataBind(). Each row of data will be represented by an item in the \Prado\Web\UI\WebControls\getItems collection of the datagrid.
An item can be at one of three states: browsing, selected and edit. The state determines how the item will be displayed. For example, if an item is in edit state, it may be displayed as a table row with input text boxes if the columns are of type TBoundColumn; and if in browsing state, they are displayed as static text.
To change the state of an item, set \Prado\Web\UI\WebControls\setEditItemIndex or \Prado\Web\UI\WebControls\setSelectedItemIndex property.
Each datagrid item has a \Prado\Web\UI\WebControls\TDataGridItem::getItemType which tells the position and state of the item in the datalist. An item in the header of the repeater is of type Header. A body item may be of either Item, AlternatingItem, SelectedItem or EditItem, depending whether the item index is odd or even, whether it is being selected or edited.
A datagrid is specified with a list of columns. Each column specifies how the corresponding table column will be displayed. For example, the header/footer text of that column, the cells in that column, and so on. The following column types are currently provided by the framework,
- TBoundColumn, associated with a specific field in datasource and displays the corresponding data.
- TEditCommandColumn, displaying edit/update/cancel command buttons
- TButtonColumn, displaying generic command buttons that may be bound to specific field in datasource.
- TDropDownListColumn, displaying a dropdown list when the item is in edit state
- THyperLinkColumn, displaying a hyperlink that may be bound to specific field in datasource.
- TCheckBoxColumn, displaying a checkbox that may be bound to specific field in datasource.
- TTemplateColumn, displaying content based on templates.
There are three ways to specify columns for a datagrid.
- Automatically generated based on data source. By setting \Prado\Web\UI\WebControls\setAutoGenerateColumns to true, a list of columns will be automatically generated based on the schema of the data source. Each column corresponds to a column of the data.
- Specified in template. For example,
```php
- Manually created in code. Columns can be manipulated via the \Prado\Web\UI\WebControls\setColumns property of the datagrid. For example, ```php $column=new TBoundColumn; $datagrid->Columns[]=$column; ```
TDataGrid supports sorting. If the \Prado\Web\UI\WebControls\setAllowSorting is set to true, a column with nonempty \Prado\Web\UI\WebControls\setSortExpression will have its header text displayed as a clickable link button. Clicking on the link button will raise \Prado\Web\UI\WebControls\onSortCommand event. You can respond to this event, sort the data source according to the event parameter, and then invoke databind() on the datagrid to show to end users the sorted data.
TDataGrid supports paging. If the \Prado\Web\UI\WebControls\setAllowPaging is set to true, a pager will be displayed on top and/or bottom of the table. How the pager will be displayed is determined by the \Prado\Web\UI\WebControls\getPagerStyle property. Clicking on a pager button will raise an \Prado\Web\UI\WebControls\onPageIndexChanged event. You can respond to this event, specify the page to be displayed by setting \Prado\Web\UI\WebControls\setCurrentPageIndex property, and then invoke databind() on the datagrid to show to end users a new page of data.
TDataGrid supports two kinds of paging. The first one is based on the number of data items in datasource. The number of pages \Prado\Web\UI\WebControls\getPageCount is calculated based the item number and the \Prado\Web\UI\WebControls\setPageSize property. The datagrid will manage which section of the data source to be displayed based on the \Prado\Web\UI\WebControls\setCurrentPageIndex property. The second approach calculates the page number based on the \Prado\Web\UI\WebControls\setVirtualItemCount property and the \Prado\Web\UI\WebControls\setPageSize property. The datagrid will always display from the beginning of the datasource up to the number of \Prado\Web\UI\WebControls\setPageSize data items. This approach is especially useful when the datasource may contain too many data items to be managed by the datagrid efficiently.
When the datagrid contains a button control that raises an \Prado\Web\UI\WebControls\onCommand event, the event will be bubbled up to the datagrid control. If the event's command name is recognizable by the datagrid control, a corresponding item event will be raised. The following item events will be raised upon a specific command:
- OnEditCommand, if CommandName=edit
- OnCancelCommand, if CommandName=cancel
- OnSelectCommand, if CommandName=select
- OnDeleteCommand, if CommandName=delete
- OnUpdateCommand, if CommandName=update
- onPageIndexChanged, if CommandName=page
- OnSortCommand, if CommandName=sort Note, an \Prado\Web\UI\WebControls\onItemCommand event is raised in addition to the above specific command events.
TDataGrid also raises an \Prado\Web\UI\WebControls\onItemCreated event for every newly created datagrid item. You can respond to this event to customize the content or style of the newly created item.
Note, the data bound to the datagrid are reset to null after databinding. There are several ways to access the data associated with a datagrid row:
- Access the data in \Prado\Web\UI\WebControls\onItemDataBound event
- Use \Prado\Web\UI\WebControls\getDataKeys to obtain the data key associated with the specified datagrid row and use the key to fetch the corresponding data from some persistent storage such as DB.
- Save the data in viewstate and get it back during postbacks.
Class hierarchy
- \Prado\Web\UI\WebControls\TDataGrid implements INamingContainer
- \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
|
addParsedObject(mixed $object) : mixed
Adds objects parsed in template to datagrid.
Datagrid columns are added into \Prado\Web\UI\WebControls\getColumns collection. |
public
|
bubbleEvent(TControl $sender, TEventParameter $param) : bool
This method overrides parent's implementation to handle
{@see onItemCommand OnItemCommand} event which is bubbled from
{@see \Prado\Web\UI\WebControls\TDataGridItem} child controls.
If the event parameter is TDataGridCommandEventParameter 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
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
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(TDataGridCommandEventParameter $param) : mixed
Raises <b>OnCancelCommand</b> event.
This method is invoked when a button control raises OnCommand event with cancel command name. |
public
|
onDeleteCommand(TDataGridCommandEventParameter $param) : mixed
Raises <b>OnDeleteCommand</b> event.
This method is invoked when a button control raises OnCommand event with delete command name. |
public
|
onEditCommand(TDataGridCommandEventParameter $param) : mixed
Raises <b>OnEditCommand</b> event.
This method is invoked when a button control raises OnCommand event with edit command name. |
public
|
onItemCommand(TDataGridCommandEventParameter $param) : mixed
Raises <b>OnItemCommand</b> event.
This method is invoked when a button control raises OnCommand event. |
public
|
onItemCreated(TDataGridItemEventParameter $param) : mixed
Raises <b>OnItemCreated</b> event.
This method is invoked right after a datagrid item is created and before added to page hierarchy. |
public
|
onItemDataBound(TDataGridItemEventParameter $param) : mixed
Raises <b>OnItemDataBound</b> event.
This method is invoked for each datagrid item after it performs databinding. |
public
|
onPageIndexChanged(TDataGridPageChangedEventParameter $param) : mixed
Raises <b>OnPageIndexChanged</b> event.
This method is invoked when current page is changed. |
public
|
onPagerCreated(TDataGridPagerEventParameter $param) : mixed
Raises <b>OnPagerCreated</b> event.
This method is invoked right after a datagrid pager is created and before added to page hierarchy. |
public
|
onSortCommand(TDataGridSortCommandEventParameter $param) : mixed
Raises <b>OnSortCommand</b> event.
This method is invoked when a button control raises OnCommand event with sort command name. |
public
|
onUpdateCommand(TDataGridCommandEventParameter $param) : mixed
Raises <b>OnUpdateCommand</b> event.
This method is invoked when a button control raises OnCommand event with update command name. |
public
|
|
public
|
renderBeginTag(THtmlWriter $writer) : mixed
Renders the openning tag for the datagrid control which will render table caption if present.
|
public
|
|
public
|
saveState() : mixed
Saves item count in viewstate.
This method is invoked right before control state is to be saved. |
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
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
|
|
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
|
|
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
|
|
protected
|
|
protected
|
createAutoColumns(Traversable $dataSource) : TDataGridColumnCollection
Automatically generates datagrid columns based on datasource schema
|
protected
|
createItem(int $itemIndex, mixed $dataSourceIndex, TListItemType $itemType) : TDataGridItem
Creates a datagrid item instance based on the item type and index.
|
protected
|
|
protected
|
createPagerButton(mixed $pager, string $buttonType, bool $enabled, string $text, string $commandName, string $commandParameter) : mixed
Creates a pager button.
Depending on the button type, a TLinkButton or a TButton may be created. If it is enabled (clickable), its command name and parameter will also be set. Derived classes may override this method to create additional types of buttons, such as TImageButton. |
protected
|
createStyle() : TTableStyle
Creates a style object for the control.
This method creates a TTableStyle to be used by datagrid. |
protected
|
|
protected
|
|
protected
|
initializeItem(TDataGridItem $item, TDataGridColumnCollection $columns) : mixed
Initializes a datagrid item and cells inside it
|
protected
|
performDataBinding(Traversable $data) : mixed
Performs databinding to populate datagrid items from data source.
This method is invoked by dataBind(). You may override this function to provide your own way of data population. |
protected
|
|
protected
|
|
private
|
createItemInternal(mixed $itemIndex, mixed $dataSourceIndex, mixed $itemType, mixed $dataBind, mixed $dataItem, mixed $columns) : mixed
|
private
|
|
private
|
public
mixed
|
CMD_CANCEL
|
'Cancel'
|
public
mixed
|
CMD_DELETE
|
'Delete'
|
public
mixed
|
CMD_EDIT
|
'Edit'
|
public
mixed
|
CMD_PAGE
|
'Page'
|
public
mixed
|
CMD_PAGE_FIRST
|
'First'
|
public
mixed
|
CMD_PAGE_LAST
|
'Last'
|
public
mixed
|
CMD_PAGE_NEXT
|
'Next'
|
public
mixed
|
CMD_PAGE_PREV
|
'Previous'
|
public
mixed
|
CMD_SELECT
Command name that TDataGrid understands.
|
'Select'
|
public
mixed
|
CMD_SORT
|
'Sort'
|
public
mixed
|
CMD_UPDATE
|
'Update'
|