PHP Class yii\grid\GridView

It provides features like sorting, paging and also filtering the data.
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\widgets\BaseListView
Exibir arquivo Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$afterRow an anonymous function that is called once AFTER rendering each data model. It should have the similar signature as [[rowOptions]]. The return result of the function will be rendered directly.
$beforeRow an anonymous function that is called once BEFORE rendering each data model. It should have the similar signature as [[rowOptions]]. The return result of the function will be rendered directly.
$caption the caption of the grid table
$captionOptions the HTML attributes for the caption element.
$columns grid column configuration. Each array element represents the configuration for one particular grid column. For example, php [ ['class' => SerialColumn::className()], [ 'class' => DataColumn::className(), // this line is optional 'attribute' => 'name', 'format' => 'text', 'label' => 'Name', ], ['class' => CheckboxColumn::className()], ] If a column is of class DataColumn, the "class" element can be omitted. As a shortcut format, a string may be used to specify the configuration of a data column which only contains [[DataColumn::attribute|attribute]], [[DataColumn::format|format]], and/or [[DataColumn::label|label]] options: "attribute:format:label". For example, the above "name" column can also be specified as: "name:text:Name". Both "format" and "label" are optional. They will take default values if absent. Using the shortcut format the configuration for columns in simple cases would look like this: php [ 'id', 'amount:currency:Total Amount', 'created_at:datetime', ] When using a [[dataProvider]] with active records, you can also display values from related records, e.g. the name attribute of the author relation: php shortcut syntax 'author.name', full syntax [ 'attribute' => 'author.name', ... ]
$dataColumnClass the default data column class if the class name is not explicitly specified when configuring a data column. Defaults to 'yii\grid\DataColumn'.
$emptyCell the HTML display when the content of a cell is empty. This property is used to render cells that have no defined content, e.g. empty footer or filter cells. Note that this is not used by the DataColumn if a data item is null. In that case the [[\yii\i18n\Formatter::nullDisplay|nullDisplay]] property of the [[formatter]] will be used to indicate an empty data value.
$filterErrorOptions the options for rendering every filter error message. This is mainly used by [[Html::error()]] when rendering an error message next to every filter input field.
$filterErrorSummaryOptions the options for rendering the filter error summary. Please refer to [[Html::errorSummary()]] for more details about how to specify the options.
$filterModel the model 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. Note that in order to show an input field for filtering, a column must have its [[DataColumn::attribute]] property set and the attribute should be active in the current scenario of $filterModel or have [[DataColumn::filter]] set as the HTML code for the input field. When this property is not set (null) the filtering feature is disabled.
$filterPosition whether the filters should be displayed in the grid view. Valid values include: - [[FILTER_POS_HEADER]]: the filters will be displayed on top of each column's header cell. - [[FILTER_POS_BODY]]: the filters will be displayed right below each column's header cell. - [[FILTER_POS_FOOTER]]: the filters will be displayed below each column's footer cell.
$filterRowOptions the HTML attributes for the filter row element.
$filterSelector additional jQuery selector for selecting filter input fields
$filterUrl the URL for returning the filtering result. [[Url::to()]] will be called to normalize the URL. If not set, the current controller action will be used. When the user makes change to any filter input, the current filtering inputs will be appended as GET parameters to this URL.
$footerRowOptions the HTML attributes for the table footer row.
$formatter the formatter used to format model attribute values into displayable texts. This can be either an instance of [[Formatter]] or an configuration array for creating the [[Formatter]] instance. If this property is not set, the "formatter" application component will be used.
$headerRowOptions the HTML attributes for the table header row.
$layout the layout that determines how different sections of the list view should be organized. The following tokens will be replaced with the corresponding section contents: - {summary}: the summary section. See [[renderSummary()]]. - {errors}: the filter model error summary. See GridView::renderErrors. - {items}: the list items. See GridView::renderItems. - {sorter}: the sorter. See [[renderSorter()]]. - {pager}: the pager. See [[renderPager()]].
$options the HTML attributes for the container tag of the grid view. The "tag" element specifies the tag name of the container element and defaults to "div".
$rowOptions the HTML attributes for the table body rows. This can be either an array specifying the common HTML attributes for all body rows, or an anonymous function that returns an array of the HTML attributes. The anonymous function will be called once for every data model returned by [[dataProvider]]. It should have the following signature: php function ($model, $key, $index, $grid) - $model: the current data model being rendered - $key: the key value associated with the current data model - $index: the zero-based index of the data model in the model array returned by [[dataProvider]] - $grid: the GridView object
$showFooter whether to show the footer section of the grid table.
$showHeader whether to show the header section of the grid table.
$showOnEmpty whether to show the grid view if [[dataProvider]] returns no data.
$tableOptions the HTML attributes for the grid table element.

Public Methods

Method Description
init ( ) Initializes the grid view.
renderCaption ( ) : boolean | string Renders the caption element.
renderColumnGroup ( ) : boolean | string Renders the column group HTML.
renderErrors ( ) : string Renders validator errors of filter model.
renderFilters ( ) : string Renders the filter.
renderItems ( ) Renders the data models for the grid view.
renderSection ( $name )
renderTableBody ( ) : string Renders the table body.
renderTableFooter ( ) : string Renders the table footer.
renderTableHeader ( ) : string Renders the table header.
renderTableRow ( mixed $model, mixed $key, integer $index ) : string Renders a table row with the given data model and key.
run ( ) Runs the widget.

Protected Methods

Method Description
createDataColumn ( string $text ) : DataColumn Creates a DataColumn object based on a string in the format of "attribute:format:label".
getClientOptions ( ) : array Returns the options for the grid view JS widget.
guessColumns ( ) This function tries to guess the columns to show from the given data if [[columns]] are not explicitly specified.
initColumns ( ) Creates column objects and initializes them.

Method Details

createDataColumn() protected method

Creates a DataColumn object based on a string in the format of "attribute:format:label".
protected createDataColumn ( string $text ) : DataColumn
$text string the column specification string
return DataColumn the column instance

getClientOptions() protected method

Returns the options for the grid view JS widget.
protected getClientOptions ( ) : array
return array the options

guessColumns() protected method

This function tries to guess the columns to show from the given data if [[columns]] are not explicitly specified.
protected guessColumns ( )

init() public method

This method will initialize required property values and instantiate [[columns]] objects.
public init ( )

initColumns() protected method

Creates column objects and initializes them.
protected initColumns ( )

renderCaption() public method

Renders the caption element.
public renderCaption ( ) : boolean | string
return boolean | string the rendered caption element or `false` if no caption element should be rendered.

renderColumnGroup() public method

Renders the column group HTML.
public renderColumnGroup ( ) : boolean | string
return boolean | string the column group HTML or `false` if no column group should be rendered.

renderErrors() public method

Renders validator errors of filter model.
public renderErrors ( ) : string
return string the rendering result.

renderFilters() public method

Renders the filter.
public renderFilters ( ) : string
return string the rendering result.

renderItems() public method

Renders the data models for the grid view.
public renderItems ( )

renderSection() public method

public renderSection ( $name )

renderTableBody() public method

Renders the table body.
public renderTableBody ( ) : string
return string the rendering result.

renderTableFooter() public method

Renders the table footer.
public renderTableFooter ( ) : string
return string the rendering result.

renderTableHeader() public method

Renders the table header.
public renderTableHeader ( ) : string
return string the rendering result.

renderTableRow() public method

Renders a table row with the given data model and key.
public renderTableRow ( mixed $model, mixed $key, integer $index ) : string
$model mixed the data model to be rendered
$key mixed the key associated with the data model
$index integer the zero-based index of the data model among the model array returned by [[dataProvider]].
return string the rendering result

run() public method

Runs the widget.
public run ( )

Property Details

$afterRow public_oe property

an anonymous function that is called once AFTER rendering each data model. It should have the similar signature as [[rowOptions]]. The return result of the function will be rendered directly.
public $afterRow

$beforeRow public_oe property

an anonymous function that is called once BEFORE rendering each data model. It should have the similar signature as [[rowOptions]]. The return result of the function will be rendered directly.
public $beforeRow

$caption public_oe property

the caption of the grid table
See also: captionOptions
public $caption

$captionOptions public_oe property

the HTML attributes for the caption element.
See also: yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
See also: caption
public $captionOptions

$columns public_oe property

grid column configuration. Each array element represents the configuration for one particular grid column. For example, php [ ['class' => SerialColumn::className()], [ 'class' => DataColumn::className(), // this line is optional 'attribute' => 'name', 'format' => 'text', 'label' => 'Name', ], ['class' => CheckboxColumn::className()], ] If a column is of class DataColumn, the "class" element can be omitted. As a shortcut format, a string may be used to specify the configuration of a data column which only contains [[DataColumn::attribute|attribute]], [[DataColumn::format|format]], and/or [[DataColumn::label|label]] options: "attribute:format:label". For example, the above "name" column can also be specified as: "name:text:Name". Both "format" and "label" are optional. They will take default values if absent. Using the shortcut format the configuration for columns in simple cases would look like this: php [ 'id', 'amount:currency:Total Amount', 'created_at:datetime', ] When using a [[dataProvider]] with active records, you can also display values from related records, e.g. the name attribute of the author relation: php shortcut syntax 'author.name', full syntax [ 'attribute' => 'author.name', ... ]
public $columns

$dataColumnClass public_oe property

the default data column class if the class name is not explicitly specified when configuring a data column. Defaults to 'yii\grid\DataColumn'.
public $dataColumnClass

$emptyCell public_oe property

the HTML display when the content of a cell is empty. This property is used to render cells that have no defined content, e.g. empty footer or filter cells. Note that this is not used by the DataColumn if a data item is null. In that case the [[\yii\i18n\Formatter::nullDisplay|nullDisplay]] property of the [[formatter]] will be used to indicate an empty data value.
public $emptyCell

$filterErrorOptions public_oe property

the options for rendering every filter error message. This is mainly used by [[Html::error()]] when rendering an error message next to every filter input field.
public $filterErrorOptions

$filterErrorSummaryOptions public_oe property

the options for rendering the filter error summary. Please refer to [[Html::errorSummary()]] for more details about how to specify the options.
See also: renderErrors()
public $filterErrorSummaryOptions

$filterModel public_oe property

the model 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. Note that in order to show an input field for filtering, a column must have its [[DataColumn::attribute]] property set and the attribute should be active in the current scenario of $filterModel or have [[DataColumn::filter]] set as the HTML code for the input field. When this property is not set (null) the filtering feature is disabled.
public $filterModel

$filterPosition public_oe property

whether the filters should be displayed in the grid view. Valid values include: - [[FILTER_POS_HEADER]]: the filters will be displayed on top of each column's header cell. - [[FILTER_POS_BODY]]: the filters will be displayed right below each column's header cell. - [[FILTER_POS_FOOTER]]: the filters will be displayed below each column's footer cell.
public $filterPosition

$filterRowOptions public_oe property

the HTML attributes for the filter row element.
See also: yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
public $filterRowOptions

$filterSelector public_oe property

additional jQuery selector for selecting filter input fields
public $filterSelector

$filterUrl public_oe property

the URL for returning the filtering result. [[Url::to()]] will be called to normalize the URL. If not set, the current controller action will be used. When the user makes change to any filter input, the current filtering inputs will be appended as GET parameters to this URL.
public $filterUrl

$footerRowOptions public_oe property

the HTML attributes for the table footer row.
See also: yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
public $footerRowOptions

$formatter public_oe property

the formatter used to format model attribute values into displayable texts. This can be either an instance of [[Formatter]] or an configuration array for creating the [[Formatter]] instance. If this property is not set, the "formatter" application component will be used.
public $formatter

$headerRowOptions public_oe property

the HTML attributes for the table header row.
See also: yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
public $headerRowOptions

$layout public_oe property

the layout that determines how different sections of the list view should be organized. The following tokens will be replaced with the corresponding section contents: - {summary}: the summary section. See [[renderSummary()]]. - {errors}: the filter model error summary. See GridView::renderErrors. - {items}: the list items. See GridView::renderItems. - {sorter}: the sorter. See [[renderSorter()]]. - {pager}: the pager. See [[renderPager()]].
public $layout

$options public_oe property

the HTML attributes for the container tag of the grid view. The "tag" element specifies the tag name of the container element and defaults to "div".
See also: yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
public $options

$rowOptions public_oe property

the HTML attributes for the table body rows. This can be either an array specifying the common HTML attributes for all body rows, or an anonymous function that returns an array of the HTML attributes. The anonymous function will be called once for every data model returned by [[dataProvider]]. It should have the following signature: php function ($model, $key, $index, $grid) - $model: the current data model being rendered - $key: the key value associated with the current data model - $index: the zero-based index of the data model in the model array returned by [[dataProvider]] - $grid: the GridView object
See also: yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
public $rowOptions

$showFooter public_oe property

whether to show the footer section of the grid table.
public $showFooter

$showHeader public_oe property

whether to show the header section of the grid table.
public $showHeader

$showOnEmpty public_oe property

whether to show the grid view if [[dataProvider]] returns no data.
public $showOnEmpty

$tableOptions public_oe property

the HTML attributes for the grid table element.
See also: yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
public $tableOptions