Difference between #3 and #4 of
Special $variables in CGridView and CListView

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 classes to do this.
 
 
Extending `CListView`
provide the variables in the widget. This extended class might look like: ```php // components/SpecialListGridView.php Yii::import('zii.widgets.CListgrid.CGridView'); class SpecialListGridView extends CListGridView {
public $extraparam;
}
[...]
```php
// in your controller
$this->widget('Special
ListGridView', 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`.
23 1
33 followers
Viewed: 77 287 times
Version: 1.1
Category: Tips
Written by: Steve Friedl
Last updated by: Steve Friedl
Created on: Oct 9, 2011
Last updated: 12 years ago
Update Article

Revisions

View all history