Changes
Title
unchanged
Special $variables in CGridView and CListView
Category
unchanged
Tips
Yii version
unchanged
Tags
unchanged
CGridView, CListView, rendering
Content
changed
[...]
* `$this->grid` - the [CGridView] object that owns the column
* `$this->grid->owner` - the owner of the grid, usually the calling controller
Passing your own variables
--------------------------
A very common request in the `#yii` channel is how to pass additional variables to the widget that are available by the rendering code. Unfortunately, it's not so simple as it looks, so unless the variable is readily available in the controller (reachable by `$this` in `CListView` or `$this->grid->owner` in `CGridView`), you pretty muchThough this is straightforward in `CListView`, it's not so simple in `CGridView`.
In `CListView` an additional `viewData` parameter can be passed to the widget as an array with additional variables:
```php
$this->widget( 'zii.widgets.CListView', array(
'dataProvider' => $dataProvider,
'viewData' => array( 'switch' => true, 'blah' => 123 ), // YOUR OWN VARIABLES
'itemView' => '_view',
) );
```
Then, in the `_view` you can refer to `$switch` or `$blah` directly right alongside the other predefined variables.
For `CGridView` it appears that we have to extend the class
es to
do this.
Extending `CListView`provide the variables in the widget. This extended class might look like:
```php
// components/Special
ListGridView.php
Yii::import('zii.widgets.
CListgrid.CGridView');
class Special
ListGridView extends C
ListGridView {
public $extraparam;
}[...]
```php
// in your controller
$this->widget('SpecialListGridView', array(
'dataProvider' => $dataProvider,
'itemView' => '_view', // partial rendering
'extraparam' => 1234 // your special parameter
'columns' => array( ... ),
) );
```
This done, the extra parameter is available in
the `_view` as `$widget->extraparam`.
For `CGridView` it will be likewise, using `$this->grid->extraparam` from within each column to get at the parameter.
each the column as `$this->grid->extraparam`.