PHP Class Prado\Web\UI\WebControls\TDataGrid

TDataGrid represents a data bound and updatable grid control. To populate data into the datagrid, sets its {@link setDataSource DataSource} to a tabular data source and call {@link dataBind()}. Each row of data will be represented by an item in the {@link getItems Items} 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 {@link TBoundColumn}; and if in browsing state, they are displayed as static text. To change the state of an item, set {@link setEditItemIndex EditItemIndex} or {@link setSelectedItemIndex SelectedItemIndex} property. Each datagrid item has a {@link TDataGridItem::getItemType type} 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, - {@link TBoundColumn}, associated with a specific field in datasource and displays the corresponding data. - {@link TEditCommandColumn}, displaying edit/update/cancel command buttons - {@link TButtonColumn}, displaying generic command buttons that may be bound to specific field in datasource. - {@link TDropDownListColumn}, displaying a dropdown list when the item is in edit state - {@link THyperLinkColumn}, displaying a hyperlink that may be bound to specific field in datasource. - {@link TCheckBoxColumn}, displaying a checkbox that may be bound to specific field in datasource. - {@link TTemplateColumn}, displaying content based on templates. There are three ways to specify columns for a datagrid.
  • Automatically generated based on data source. By setting {@link setAutoGenerateColumns AutoGenerateColumns} 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,
  • Manually created in code. Columns can be manipulated via the {@link setColumns Columns} property of the datagrid. For example, $column=new TBoundColumn; $datagrid->Columns[]=$column;
Note, automatically generated columns cannot be accessed via the {@link getColumns Columns} property. TDataGrid supports sorting. If the {@link setAllowSorting AllowSorting} is set to true, a column with nonempty {@link setSortExpression SortExpression} will have its header text displayed as a clickable link button. Clicking on the link button will raise {@link onSortCommand OnSortCommand} event. You can respond to this event, sort the data source according to the event parameter, and then invoke {@link databind()} on the datagrid to show to end users the sorted data. TDataGrid supports paging. If the {@link setAllowPaging AllowPaging} 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 {@link getPagerStyle PagerStyle} property. Clicking on a pager button will raise an {@link onPageIndexChanged OnPageIndexChanged} event. You can respond to this event, specify the page to be displayed by setting {@link setCurrentPageIndex CurrentPageIndex} property, and then invoke {@link 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 {@link getPageCount PageCount} is calculated based the item number and the {@link setPageSize PageSize} property. The datagrid will manage which section of the data source to be displayed based on the {@link setCurrentPageIndex CurrentPageIndex} property. The second approach calculates the page number based on the {@link setVirtualItemCount VirtualItemCount} property and the {@link setPageSize PageSize} property. The datagrid will always display from the beginning of the datasource up to the number of {@link setPageSize PageSize} 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 {@link onCommand 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 {@link onItemCommand OnItemCommand} event is raised in addition to the above specific command events. TDataGrid also raises an {@link onItemCreated 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 {@link onItemDataBound OnItemDataBound} event - Use {@link getDataKeys DataKeys} 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.
Since: 3.0
Author: Qiang Xue ([email protected])
Inheritance: extends TBaseDataList, implements Prado\Web\UI\INamingContainer
ファイルを表示 Open project: pradosoft/prado Class Usage Examples

Public Methods

Method Description
addParsedObject ( $object ) Adds objects parsed in template to datagrid.
bubbleEvent ( $sender, $param ) : boolean This method overrides parent's implementation to handle {@link onItemCommand OnItemCommand} event which is bubbled from {@link TDataGridItem} child controls.
getAllowSorting ( ) : boolean
getAlternatingItemStyle ( ) : TTableItemStyle
getAutoColumns ( ) : TDataGridColumnCollection
getAutoGenerateColumns ( ) : boolean
getBackImageUrl ( ) : string
getBottomPager ( ) : TDataGridPager
getCaption ( ) : string
getCaptionAlign ( ) : TTableCaptionAlign
getColumns ( ) : TDataGridColumnCollection
getEditItem ( ) : TDataGridItem
getEditItemIndex ( ) : integer
getEditItemStyle ( ) : TTableItemStyle
getEmptyTemplate ( ) : Prado\Web\UI\ITemplate
getFooter ( ) : TDataGridItem
getFooterStyle ( ) : TTableItemStyle
getHeader ( ) : TDataGridItem
getHeaderStyle ( ) : TTableItemStyle
getItemCount ( ) : integer
getItemStyle ( ) : TTableItemStyle
getItems ( ) : TDataGridItemCollection
getPagerStyle ( ) : TDataGridPagerStyle
getSelectedItem ( ) : TDataGridItem
getSelectedItemIndex ( ) : integer
getSelectedItemStyle ( ) : TTableItemStyle
getShowFooter ( ) : boolean
getShowHeader ( ) : boolean
getTableBodyStyle ( ) : TStyle
getTableFootStyle ( ) : TStyle
getTableHeadStyle ( ) : TStyle
getTopPager ( ) : TDataGridPager
loadState ( ) Loads item count information from viewstate.
onCancelCommand ( $param ) Raises OnCancelCommand event.
onDeleteCommand ( $param ) Raises OnDeleteCommand event.
onEditCommand ( $param ) Raises OnEditCommand event.
onItemCommand ( $param ) Raises OnItemCommand event.
onItemCreated ( $param ) Raises OnItemCreated event.
onItemDataBound ( $param ) Raises OnItemDataBound event.
onPageIndexChanged ( $param ) Raises OnPageIndexChanged event.
onPagerCreated ( $param ) Raises OnPagerCreated event.
onSortCommand ( $param ) Raises OnSortCommand event.
onUpdateCommand ( $param ) Raises OnUpdateCommand event.
render ( $writer ) Renders the datagrid.
renderBeginTag ( $writer ) Renders the openning tag for the datagrid control which will render table caption if present.
reset ( ) Clears up all items in the datagrid.
saveState ( ) Saves item count in viewstate.
setAllowSorting ( $value )
setAutoGenerateColumns ( $value )
setBackImageUrl ( $value )
setCaption ( $value )
setCaptionAlign ( $value )
setEditItemIndex ( $value ) Edits an item by its index in {@link getItems Items}.
setEmptyTemplate ( $value )
setSelectedItemIndex ( $value ) Selects an item by its index in {@link getItems Items}.
setShowFooter ( $value )
setShowHeader ( $value )

Protected Methods

Method Description
applyItemStyles ( ) Applies styles to items, header, footer and separators.
buildNextPrevPager ( $pager ) Builds a next-prev pager
buildNumericPager ( $pager ) Builds a numeric pager
buildPager ( $pager ) Builds the pager content based on pager style.
createAutoColumns ( $dataSource ) : TDataGridColumnCollection Automatically generates datagrid columns based on datasource schema
createItem ( $itemIndex, $dataSourceIndex, $itemType ) : TDataGridItem Creates a datagrid item instance based on the item type and index.
createPager ( )
createPagerButton ( $pager, $buttonType, $enabled, $text, $commandName, $commandParameter ) : mixed Creates a pager button.
createStyle ( ) : TTableStyle Creates a style object for the control.
getAutoGenerateColumnName ( ) : string
getTagName ( ) : string
initializeItem ( $item, $columns ) Initializes a datagrid item and cells inside it
performDataBinding ( $data ) Performs databinding to populate datagrid items from data source.
renderTable ( $writer ) Renders the tabular data.
restoreGridFromViewState ( ) Restores datagrid content from viewstate.

Private Methods

Method Description
createItemInternal ( $itemIndex, $dataSourceIndex, $itemType, $dataBind, $dataItem, $columns )
getCellText ( $cell )
groupCells ( ) Merges consecutive cells who have the same text.

Method Details

addParsedObject() public method

Datagrid columns are added into {@link getColumns Columns} collection.
public addParsedObject ( $object )

applyItemStyles() protected method

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, {@link getItemStyle ItemStyle}, {@link getAlternatingItemStyle AlternatingItemStyle}, {@link getSelectedItemStyle SelectedItemStyle}, and {@link getEditItemStyle EditItemStyle}. Therefore, if background color is set as red in {@link getItemStyle ItemStyle}, {@link getEditItemStyle EditItemStyle} will also have red background color unless it is set to a different value explicitly.
protected applyItemStyles ( )

bubbleEvent() public method

If the event parameter is {@link 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 {@link onEditCommand OnEditCommand}). This method should only be used by control developers.
public bubbleEvent ( $sender, $param ) : boolean
return boolean whether the event bubbling should stop here.

buildNextPrevPager() protected method

Builds a next-prev pager
protected buildNextPrevPager ( $pager )

buildNumericPager() protected method

Builds a numeric pager
protected buildNumericPager ( $pager )

buildPager() protected method

Builds the pager content based on pager style.
protected buildPager ( $pager )

createAutoColumns() protected method

Automatically generates datagrid columns based on datasource schema
protected createAutoColumns ( $dataSource ) : TDataGridColumnCollection
return TDataGridColumnCollection

createItem() protected method

Creates a datagrid item instance based on the item type and index.
protected createItem ( $itemIndex, $dataSourceIndex, $itemType ) : TDataGridItem
return TDataGridItem created data list item

createPager() protected method

protected createPager ( )

createPagerButton() protected method

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 createPagerButton ( $pager, $buttonType, $enabled, $text, $commandName, $commandParameter ) : mixed
return mixed the button instance

createStyle() protected method

This method creates a {@link TTableStyle} to be used by datagrid.
protected createStyle ( ) : TTableStyle
return TTableStyle control style to be used

getAllowSorting() public method

public getAllowSorting ( ) : boolean
return boolean whether sorting is enabled. Defaults to false.

getAlternatingItemStyle() public method

public getAlternatingItemStyle ( ) : TTableItemStyle
return TTableItemStyle the style for each alternating item

getAutoColumns() public method

public getAutoColumns ( ) : TDataGridColumnCollection
return TDataGridColumnCollection automatically generated datagrid columns

getAutoGenerateColumnName() protected method

protected getAutoGenerateColumnName ( ) : string
return string Name of the class used in AutoGenerateColumns mode

getAutoGenerateColumns() public method

public getAutoGenerateColumns ( ) : boolean
return boolean whether datagrid columns should be automatically generated. Defaults to true.

getBackImageUrl() public method

public getBackImageUrl ( ) : string
return string the URL of the background image for the datagrid

getBottomPager() public method

public getBottomPager ( ) : TDataGridPager
return TDataGridPager the pager displayed at the bottom of datagrid. It could be null if paging is disabled.

getCaption() public method

public getCaption ( ) : string
return string caption for the datagrid

getCaptionAlign() public method

public getCaptionAlign ( ) : TTableCaptionAlign
return TTableCaptionAlign datagrid caption alignment. Defaults to TTableCaptionAlign::NotSet.

getColumns() public method

public getColumns ( ) : TDataGridColumnCollection
return TDataGridColumnCollection manually specified datagrid columns

getEditItem() public method

public getEditItem ( ) : TDataGridItem
return TDataGridItem the edit item

getEditItemIndex() public method

public getEditItemIndex ( ) : integer
return integer the zero-based index of the edit item in {@link getItems Items}. A value -1 means no item is in edit mode.

getEditItemStyle() public method

public getEditItemStyle ( ) : TTableItemStyle
return TTableItemStyle the style for edit item

getEmptyTemplate() public method

public getEmptyTemplate ( ) : Prado\Web\UI\ITemplate
return Prado\Web\UI\ITemplate the template applied when no data is bound to the datagrid

getFooter() public method

public getFooter ( ) : TDataGridItem
return TDataGridItem the footer item

getFooterStyle() public method

public getFooterStyle ( ) : TTableItemStyle
return TTableItemStyle the style for footer

getHeader() public method

public getHeader ( ) : TDataGridItem
return TDataGridItem the header item

getHeaderStyle() public method

public getHeaderStyle ( ) : TTableItemStyle
return TTableItemStyle the style for header

getItemCount() public method

public getItemCount ( ) : integer
return integer number of items

getItemStyle() public method

public getItemStyle ( ) : TTableItemStyle
return TTableItemStyle the style for every item

getItems() public method

public getItems ( ) : TDataGridItemCollection
return TDataGridItemCollection datagrid item collection

getPagerStyle() public method

public getPagerStyle ( ) : TDataGridPagerStyle
return TDataGridPagerStyle the style for pager

getSelectedItem() public method

public getSelectedItem ( ) : TDataGridItem
return TDataGridItem the selected item, null if no item is selected.

getSelectedItemIndex() public method

public getSelectedItemIndex ( ) : integer
return integer the zero-based index of the selected item in {@link getItems Items}. A value -1 means no item selected.

getSelectedItemStyle() public method

public getSelectedItemStyle ( ) : TTableItemStyle
return TTableItemStyle the style for selected item

getShowFooter() public method

public getShowFooter ( ) : boolean
return boolean whether the footer should be displayed. Defaults to false.

getShowHeader() public method

public getShowHeader ( ) : boolean
return boolean whether the header should be displayed. Defaults to true.

getTableBodyStyle() public method

Since: 3.1.1
public getTableBodyStyle ( ) : TStyle
return TStyle the style for tbody element, if any

getTableFootStyle() public method

Since: 3.1.1
public getTableFootStyle ( ) : TStyle
return TStyle the style for tfoot element, if any

getTableHeadStyle() public method

Since: 3.1.1
public getTableHeadStyle ( ) : TStyle
return TStyle the style for thead element, if any

getTagName() protected method

protected getTagName ( ) : string
return string tag name (table) of the datagrid

getTopPager() public method

public getTopPager ( ) : TDataGridPager
return TDataGridPager the pager displayed at the top of datagrid. It could be null if paging is disabled.

initializeItem() protected method

Initializes a datagrid item and cells inside it
protected initializeItem ( $item, $columns )

loadState() public method

This method is invoked right after control state is loaded.
public loadState ( )

onCancelCommand() public method

This method is invoked when a button control raises OnCommand event with cancel command name.
public onCancelCommand ( $param )

onDeleteCommand() public method

This method is invoked when a button control raises OnCommand event with delete command name.
public onDeleteCommand ( $param )

onEditCommand() public method

This method is invoked when a button control raises OnCommand event with edit command name.
public onEditCommand ( $param )

onItemCommand() public method

This method is invoked when a button control raises OnCommand event.
public onItemCommand ( $param )

onItemCreated() public method

This method is invoked right after a datagrid item is created and before added to page hierarchy.
public onItemCreated ( $param )

onItemDataBound() public method

This method is invoked for each datagrid item after it performs databinding.
public onItemDataBound ( $param )

onPageIndexChanged() public method

This method is invoked when current page is changed.
public onPageIndexChanged ( $param )

onPagerCreated() public method

This method is invoked right after a datagrid pager is created and before added to page hierarchy.
public onPagerCreated ( $param )

onSortCommand() public method

This method is invoked when a button control raises OnCommand event with sort command name.
public onSortCommand ( $param )

onUpdateCommand() public method

This method is invoked when a button control raises OnCommand event with update command name.
public onUpdateCommand ( $param )

performDataBinding() protected method

This method is invoked by {@link dataBind()}. You may override this function to provide your own way of data population.
protected performDataBinding ( $data )

render() public method

Renders the datagrid.
public render ( $writer )

renderBeginTag() public method

Renders the openning tag for the datagrid control which will render table caption if present.
public renderBeginTag ( $writer )

renderTable() protected method

Renders the tabular data.
protected renderTable ( $writer )

reset() public method

Clears up all items in the datagrid.
public reset ( )

restoreGridFromViewState() protected method

Restores datagrid content from viewstate.
protected restoreGridFromViewState ( )

saveState() public method

This method is invoked right before control state is to be saved.
public saveState ( )

setAllowSorting() public method

public setAllowSorting ( $value )

setAutoGenerateColumns() public method

public setAutoGenerateColumns ( $value )

setBackImageUrl() public method

public setBackImageUrl ( $value )

setCaption() public method

public setCaption ( $value )

setCaptionAlign() public method

public setCaptionAlign ( $value )

setEditItemIndex() public method

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 setEditItemIndex ( $value )

setEmptyTemplate() public method

public setEmptyTemplate ( $value )

setSelectedItemIndex() public method

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 setSelectedItemIndex ( $value )

setShowFooter() public method

public setShowFooter ( $value )

setShowHeader() public method

public setShowHeader ( $value )