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

Revision #4 has been created by Steve Friedl on Oct 12, 2011, 1:54:27 PM with the memo:

Updated to reflect viewData being available in CListView based on info from redguy
« previous (#3)

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`.
22 1
33 followers
Viewed: 67 302 times
Version: 1.1
Category: Tips
Written by: Steve Friedl
Last updated by: Steve Friedl
Created on: Oct 9, 2011
Last updated: 6 years ago
Update Article

Revisions

View all history