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 (qiang.xue@gmail.com)
Inheritance: extends TBaseDataList, implements Prado\Web\UI\INamingContainer
Beispiel #1
0
 /**
  * Renders the datagrid by writing a {@link getSurroundingTag()} with the container id obtained
  * from {@link getSurroundingTagId()} which will be called by the replacement method of the client
  * script to update it's content.
  * @param THtmlWriter writer for the rendering purpose
  */
 private function renderDataGrid($writer)
 {
     $writer->addAttribute('id', $this->getSurroundingTagID());
     $writer->renderBeginTag($this->getSurroundingTag());
     parent::render($writer);
     $writer->renderEndTag();
 }