Cgridview Custom Filed

hi.

how can i add custom columns to CGridView, i want to get data from model by ActiveDataProvider and add one or more Columns to the dataProvider Array.

i do somthing like that :




'dataProvider'=>$dataProvider,

'columns'=>array(

      'name'=>'test',

      'type'=>'raw',

      'value'=>'some value'

)



but facing error that test (in this case) is not in my table(Model)

Dear friend

You can declare virtual attribute in your model(AR).




class something extends CActiveRecord

{   

    public $test;


}



Also make it safe on search when declaring rules to make the filters to work for that attribute.

Regards.

tanx for your answer, but there is no better way ? for example i want to add image column, i had to define image attribute in my Model? i think it’s strange

Dear Friend

If you are averse in declaring virtual attributes, you can do the following.




dataProvider'=>$dataProvider,

'columns'=>array(

      'header'=>'SomeImage',

      'type'=>'raw',

      'value'=>'some value'

)



Regards.

here is my code :




    $this->widget('zii.widgets.grid.CGridView',array(

                                       'dataProvider'=>$article,'columns'=>array(

                                       'header'=>'Link', 'type'=>'raw', 'value'=>'ABC')))



but i faced this error :

Property "Article.Link" is not defined

Dear Friend

It should be declared in the following way.




<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'test-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'id',

		'name',

		'age',

		'mail',

                array(    //customized column should be declared as an array inside columns.

                 'header'=>"MyImage",

                 'type'=>"raw",

                  'value'=>"someValue"

                ),

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



therefor we can not use CGridView by simple array ?

like that :

$dataProvider = array(‘i1’=>‘a’,‘i2’=>‘b’,‘i3’=>‘c’);




    $this->widget('zii.widgets.grid.CGridView',array(

                                       'dataProvider'=>$dataProvide,'columns'=>array(

                                       'header'=>'Link', 'type'=>'raw', 'value'=>'ABC')))



why you use model seach method ad filter in this case?

Dear Friend

We can use arrays.

We have to use CArrayDataProvider.

I give an example.




<?php $arr=array(

                array("id"=>1,"fruit"=>"apple","color"=>"red"),

                array("id"=>2,"fruit"=>"orange","color"=>"orange"),

                array("id"=>3,"fruit"=>"grape","color"=>"green"),

                array("id"=>4,"fruit"=>"banana","color"=>"yellow"),              

                );

?>

<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'customer-grid',

	'dataProvider'=>new CArrayDataProvider($arr),

	'columns'=>array(

	    'id',

	    'fruit',

	    'color',		

	),

));

?>



Do not forget to put id in each element.

If your array record do not contain keyField ie) id

You can do something like this.




<?php $arr=array(

                array("fruit"=>"apple","color"=>"red"),

                array("fruit"=>"orange","color"=>"orange"),

                array("fruit"=>"grape","color"=>"green"),

                array("fruit"=>"banana","color"=>"yellow"),              

                );

$dataProvider=new CArrayDataProvider($arr);

$dataProvider->keyField=false;

?>

<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'customer-grid',

	'dataProvider'=>$dataProvider,

	'columns'=>array(

	    'fruit',

	    'color',		

	),

));

 ?>




It is advised to put keyField and also as id to avoid confusion.

Regards.