CGridView
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.
Property Details
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.
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).
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.
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).
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
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.
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.
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.
public string $filterCssClass;
the CSS class name for the table row element containing all filter input fields. Defaults to 'filters'.
public string $filterPosition;
whether the filters should be displayed in the grid view. Valid values include:
- header: the filters will be displayed on top of each column's header cell.
- body: the filters will be displayed right below each column's header cell.
- footer: the filters will be displayed below each column's footer cell.
the formatter instance. Defaults to the 'format' application component.
whether the table should render a footer.
This is true if any of the columns has a true CGridColumn::hasFooter value.
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.
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.
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').
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.
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.
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.
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()
|
|
| $text |
string |
the column specification string |
| {return} |
CDataColumn |
the column instance |
Creates a CDataColumn based on a shortcut column specification string.
|
|
| {return} |
CFormatter |
the formatter instance. Defaults to the 'format' application component. |
|
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. |
Initializes the grid view.
This method will initialize required property values and instantiate columns objects.
initColumns()
|
protected void initColumns()
|
Creates column objects and initializes them.
|
public void registerClientScript()
|
Registers necessary client scripts.
|
public void renderFilter()
|
Renders the filter.
|
public void renderItems()
|
Renders the data items for the grid view.
renderTableBody()
|
public void renderTableBody()
|
Renders the table body.
|
public void renderTableFooter()
|
Renders the table footer.
|
public void renderTableHeader()
|
Renders the table header.
|
public void renderTableRow(integer $row)
|
| $row |
integer |
the row number (zero-based). |
Renders a table body row.
Can you tell me what variable is $data????
I tried an example similiar to that one and this was the error returned:
"PHP Error
Description
Undefined variable: data"