You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
You usually take a model instance passed into your view to provide data to a CGridView in this way:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search() ,
));
Very often, you will want to customize a single column, while keeping the rest untouched. There are many ways to accomplish this, but I find the following to be the easiest one:
$columns = array_keys($model->metaData->columns);
$columns['column_name'] = array(
'name' => 'column_name',
'value' => ...
);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search() ,
'columns' => $columns
));
I have used the metaData ActiveRecord's property to get all the model's attribute names and then customized a single attribute (i.e. column_name), keeping the rest to the defaults, directly in the view, without having to perform loops or implement methods in other classes.
can see only niche use case for this.
I've always used:
$this->widget('zii.widgets.grid.CGridView', array( 'dataProvider' => $model->search() , 'columns' => array( 'col1', 'col2', array( 'name' => 'col3', 'value' => '...', ), ), ));
I'd use your solution when i want it to be dynamic to model change. However you usually have also ID columns etc which you dont want to display.
@imre
As you say, this is handy for models under change, like when the application is in development. Also, I have classes with many attributes, and I don't like to copy and paste the attributes from one place to another. Indeed, it can clutter the code when you write all the attributes in the widget params.
Thanks for commenting! Cheers
this doesn't seam to work for CSqlDataProvider...
I tried doing this for a CSqlDataProvider, and always gives me Trying to get property of non-object
Hello Friends I am new in Yii Please Help me on cgrid view
I am trying to display text instead of numeric numbers
<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'admin-user-grid', 'dataProvider'=>$model->search(), 'filter'=>$model, 'columns'=>array( 'fname', 'lname', 'username', 'password', 'email', array('name'=>'status_id','value'=>CHtml::encode($model->getStatusText())), array('name'=>'pkg_id','value'=>CHtml::encode($model->getPackageText())), 'active_date', 'deactive_date', array( 'class'=>'CButtonColumn', ), ), )); ?>
getPackageText() and getStatusText() functions are defined in model class. I am able to display numeric number which is stored in database but I want to display text for better user experience.If I am try to call these functions from cgridview its displaying blank instead of text. Please help me to achieve my requirement and suggest better solution.
my model class functions.
public function getPackageText() { $user=Package::model()->findByAttributes(array('pkgid'=>$this->pkg_id)); return $user['pkg_name']; } public function getStatusText() { $user=Status::model()->findByAttributes(array('statusid'=>$this->status_id)); return $user['status_name']; }
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.