CGridView

Package zii.widgets.grid
Inheritance class CGridView » CBaseListView » CWidget » CBaseController » CComponent
Since 1.1
Version $Id: CGridView.php 190 2010-06-17 16:08:28Z qiang.xue $
CGridView displays a list of data items in terms of a table.

Each row of the table represents the data of a single data item, and a column usually represents an attribute of the item (some columns may correspond to complex expression of attributes or static text).

CGridView supports both sorting and pagination of the data items. The sorting and pagination can be done in AJAX mode or normal page request. A benefit of using CGridView is that when the user browser disables JavaScript, the sorting and pagination automatically degenerate to normal page requests and are still functioning as expected.

CGridView should be used together with a data provider, preferrably a CActiveDataProvider.

The minimal code needed to use CGridView is as follows:

$dataProvider=new CActiveDataProvider('Post');

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
));


The above code first creates a data provider for the Post ActiveRecord class. It then uses CGridView to display every attribute in every Post instance. The displayed table is equiped with sorting and pagination functionality.

In order to selectively display attributes with different formats, we may configure the CGridView::columns property. For example, we may specify only the title and create_time attributes to be displayed, and the create_time should be properly formatted to show as a time. We may also display the attributes of the related objects using the dot-syntax as shown below:

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        'title',          // display the 'title' attribute
        'category.name',  // display the 'name' attribute of the 'category' relation
        'content:html',   // display the 'content' attribute as purified HTML
        array(            // display 'create_time' using an expression
            'name'=>'create_time',
            'value'=>'date("M j, Y", $data->create_time)',
        ),
        array(            // display 'author.username' using an expression
            'name'=>'authorName',
            'value'=>'$data->author->username',
        ),
        array(            // display a column with "view", "update" and "delete" buttons
            'class'=>'CButtonColumn',
        ),
    ),
));


Please refer to columns for more details about how to configure this property.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
actionPrefix string the prefix to the IDs of the actions. CWidget
afterAjaxUpdate string a javascript function that will be invoked after a successful AJAX response is received. CGridView
ajaxUpdate mixed the ID of the container whose content may be updated with an AJAX response. CGridView
ajaxVar string the name of the GET variable that indicates the request is an AJAX request triggered by this widget. CGridView
baseScriptUrl string the base script URL for all grid view resources (e. CGridView
beforeAjaxUpdate string a javascript function that will be invoked before an AJAX update occurs. CGridView
columns array grid column configuration. CGridView
controller CController the controller that this widget belongs to. CWidget
cssFile string the URL of the CSS file used by this grid view. CGridView
dataProvider IDataProvider the data provider for the view. CBaseListView
emptyText string the message to be displayed when dataProvider does not have any data. CBaseListView
enablePagination boolean whether to enable pagination. CBaseListView
enableSorting boolean whether to enable sorting. CBaseListView
filter CModel the model instance that keeps the user-entered filter data. CGridView
filterCssClass string the CSS class name for the table row element containing all filter input fields. CGridView
filterPosition string whether the filters should be displayed in the grid view. CGridView
formatter CFormatter the formatter instance. CGridView
hasFooter boolean whether the table should render a footer. CGridView
hideHeader boolean whether to hide the header cells of the grid. CGridView
htmlOptions array the HTML options for the view container tag. CBaseListView
id string id of the widget. CWidget
itemsCssClass string the CSS class name for the container of all data item display. CBaseListView
loadingCssClass string the CSS class name that will be assigned to the widget container element when the widget is updating its content via AJAX. CBaseListView
nullDisplay string the text to be displayed in a data cell when a data value is null. CGridView
owner CBaseController owner/creator of this widget. CWidget
pager array the configuration for the pager. CBaseListView
pagerCssClass string the CSS class name for the pager container. CBaseListView
rowCssClass array the CSS class names for the table body rows. CGridView
rowCssClassExpression string a PHP expression that is evaluated for every table body row and whose result is used as the CSS class name for the row. CGridView
selectableRows integer the number of table body rows that can be selected. CGridView
selectionChanged string a javascript function that will be invoked after the row selection is changed. CGridView
showTableOnEmpty boolean whether to display the table even when there is no data. CGridView
skin mixed the name of the skin to be used by this widget. CWidget
summaryCssClass string the CSS class name for the summary text container. CBaseListView
summaryText string the summary text template for the view. CBaseListView
tagName string the tag name for the view container. CBaseListView
template string the template to be used to control the layout of various sections in the view. CBaseListView
viewPath string Returns the directory containing the view files for this widget. CWidget

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CWidget
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
actions() Returns a list of actions that are used by this widget. CWidget
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
beginCache() Begins fragment caching. CBaseController
beginClip() Begins recording a clip. CBaseController
beginContent() Begins the rendering of content that is to be decorated by the specified view. CBaseController
beginWidget() Creates a widget and executes it. CBaseController
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
createWidget() Creates a widget and initializes it. CBaseController
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
endCache() Ends fragment caching. CBaseController
endClip() Ends recording a clip. CBaseController
endContent() Ends the rendering of content. CBaseController
endWidget() Ends the execution of the named widget. CBaseController
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
getController() CWidget
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getFormatter() CGridView
getHasFooter() CGridView
getId() CWidget
getOwner() CWidget
getViewFile() Looks for the view script file according to the view name. CWidget
getViewPath() Returns the directory containing the view files for this widget. CWidget
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
init() Initializes the grid view. CGridView
raiseEvent() Raises an event. CComponent
registerClientScript() Registers necessary client scripts. CGridView
render() Renders a view. CWidget
renderContent() Renders the main content of the view. CBaseListView
renderEmptyText() Renders the empty message when there is no data. CBaseListView
renderFile() Renders a view file. CBaseController
renderFilter() Renders the filter. CGridView
renderInternal() Renders a view file. CBaseController
renderItems() Renders the data items for the grid view. CGridView
renderKeys() Renders the key values of the data in a hidden tag. CBaseListView
renderPager() Renders the pager. CBaseListView
renderSummary() Renders the summary text. CBaseListView
renderTableBody() Renders the table body. CGridView
renderTableFooter() Renders the table footer. CGridView
renderTableHeader() Renders the table header. CGridView
renderTableRow() Renders a table body row. CGridView
run() Renders the view. CBaseListView
setFormatter() CGridView
setId() CWidget
widget() Creates a widget and executes it. CBaseController

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
createDataColumn() Creates a CDataColumn based on a shortcut column specification string. CGridView
initColumns() Creates column objects and initializes them. CGridView
renderSection() Renders a section. CBaseListView

Property Details

afterAjaxUpdate property
public string $afterAjaxUpdate;

a javascript function that will be invoked after a successful AJAX response is received. The function signature is function(id, data) where 'id' refers to the ID of the grid view 'data' the received ajax response data.

ajaxUpdate property
public mixed $ajaxUpdate;

the ID of the container whose content may be updated with an AJAX response. Defaults to null, meaning the container for this grid view instance. If it is set false, it means sorting and pagination will be performed in normal page requests instead of AJAX requests. If the sorting and pagination should trigger the update of multiple containers' content in AJAX fashion, these container IDs may be listed here (separated with comma).

ajaxVar property
public string $ajaxVar;

the name of the GET variable that indicates the request is an AJAX request triggered by this widget. Defaults to 'ajax'. This is effective only when ajaxUpdate is not false.

baseScriptUrl property
public string $baseScriptUrl;

the base script URL for all grid view resources (e.g. javascript, CSS file, images). Defaults to null, meaning using the integrated grid view resources (which are published as assets).

beforeAjaxUpdate property
public string $beforeAjaxUpdate;

a javascript function that will be invoked before an AJAX update occurs. The function signature is function(id) where 'id' refers to the ID of the grid view.

columns property
public array $columns;

grid column configuration. Each array element represents the configuration for one particular grid column which can be either a string or an array.

When a column is specified as a string, it should be in the format of "name:type:header", where "type" and "header" are optional. A CDataColumn instance will be created in this case, whose CDataColumn::name, CDataColumn::type and CDataColumn::header properties will be initialized accordingly.

When a column is specified as an array, it will be used to create a grid column instance, where the 'class' element specifies the column class name (defaults to CDataColumn if absent). Currently, these official column classes are provided: CDataColumn, CLinkColumn, CButtonColumn and CCheckBoxColumn.

cssFile property
public string $cssFile;

the URL of the CSS file used by this grid view. Defaults to null, meaning using the integrated CSS file. If this is set false, you are responsible to explicitly include the necessary CSS file in your page.

filter property (available since v1.1.1)
public CModel $filter;

the model instance that keeps the user-entered filter data. When this property is set, the grid view will enable column-based filtering. Each data column by default will display a text field at the top that users can fill in to filter the data.

filterCssClass property (available since v1.1.1)
public string $filterCssClass;

the CSS class name for the table row element containing all filter input fields. Defaults to 'filters'.

See Also

filterPosition property (available since v1.1.1)
public string $filterPosition;

whether the filters should be displayed in the grid view. Valid values include:

See Also

formatter property
public CFormatter getFormatter()
public void setFormatter(CFormatter $value)

the formatter instance. Defaults to the 'format' application component.

hasFooter property read-only
public boolean getHasFooter()

whether the table should render a footer. This is true if any of the columns has a true CGridColumn::hasFooter value.

hideHeader property (available since v1.1.1)
public boolean $hideHeader;

whether to hide the header cells of the grid. When this is true, header cells will not be rendered, which means the grid cannot be sorted anymore since the sort links are located in the header. Defaults to false.

nullDisplay property
public string $nullDisplay;

the text to be displayed in a data cell when a data value is null. This property will NOT be HTML-encoded when rendering. Defaults to an HTML blank.

rowCssClass property
public array $rowCssClass;

the CSS class names for the table body rows. If multiple CSS class names are given, they will be assigned to the rows sequentially and repeatedly. This property is ignored if rowCssClassExpression is set. Defaults to array('odd', 'even').

rowCssClassExpression property
public string $rowCssClassExpression;

a PHP expression that is evaluated for every table body row and whose result is used as the CSS class name for the row. In this expression, the variable $row stands for the row number (zero-based), $data is the data model associated with the row, and $this is the grid object.

See Also

selectableRows property
public integer $selectableRows;

the number of table body rows that can be selected. If 0, it means rows cannot be selected. If 1, only one row can be selected. If 2 or any other number, it means multiple rows can be selected. A selected row will have a CSS class named 'selected'. You may also call the JavaScript function $.fn.yiiGridView.getSelection(containerID) to retrieve the key values of the selected rows.

selectionChanged property
public string $selectionChanged;

a javascript function that will be invoked after the row selection is changed. The function signature is function(id) where 'id' refers to the ID of the grid view. In this function, you may use $.fn.yiiGridView.getSelection(id) to get the key values of the currently selected rows.

See Also

showTableOnEmpty property
public boolean $showTableOnEmpty;

whether to display the table even when there is no data. Defaults to true. The emptyText will be displayed to indicate there is no data.

Method Details

createDataColumn() method
protected CDataColumn createDataColumn(string $text)
$text string the column specification string
{return} CDataColumn the column instance

Creates a CDataColumn based on a shortcut column specification string.

getFormatter() method
public CFormatter getFormatter()
{return} CFormatter the formatter instance. Defaults to the 'format' application component.

getHasFooter() method
public boolean getHasFooter()
{return} boolean whether the table should render a footer. This is true if any of the columns has a true CGridColumn::hasFooter value.

init() method
public void init()

Initializes the grid view. This method will initialize required property values and instantiate columns objects.

initColumns() method
protected void initColumns()

Creates column objects and initializes them.

registerClientScript() method
public void registerClientScript()

Registers necessary client scripts.

renderFilter() method (available since v1.1.1)
public void renderFilter()

Renders the filter.

renderItems() method
public void renderItems()

Renders the data items for the grid view.

renderTableBody() method
public void renderTableBody()

Renders the table body.

renderTableFooter() method
public void renderTableFooter()

Renders the table footer.

renderTableHeader() method
public void renderTableHeader()

Renders the table header.

renderTableRow() method
public void renderTableRow(integer $row)
$row integer the row number (zero-based).

Renders a table body row.

setFormatter() method
public void setFormatter(CFormatter $value)
$value CFormatter the formatter instance