how can we add custom field(column) in CGridView dynamically

Hi guys… long time no seee!

well to the point, how can we add custom field(column) in CGridView dynamically ?

example :

we have table with more than 40 fields,

if we show all this fields in admin.php, the page horizontal scroolbar is to long in screen.

The guestion is, how can we custom which field will be shown using checklist(checked),

so we can dynamically add or remove some fields to show?

anybody have done this?

i think this is good for new feature in yii.

thanks all, i’m counting on you.

all your reply i’m very pleased. :)

You can prepare the columns array dynamically, someting like:




$columns=array();

foreach ($fields as $field)

{

   if ($GET['fields'][$field])

       $columns[]=$field;

}


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

	'id'=>'town-grid',

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

	'filter'=>$model,

	'columns'=>$columns

));



Or, even better, prepare $columns in the controller and then passing it to the view.

#zaccaria : thanks for the tips.

its so simple after all. base on $_GET parameter.

i personally imagine it using ajax request. so only cgridview will reload. :)

just like asmselect.

btw i will try it.

Hi,

In my case I’ve pulled the $columns from the database & I’ve my array from $row = $dataReader->readAll(); but I’m getting errors: Property “CDataColumn.Male” is not defined. while Male is the column in my array. Here is the code:




// Inside my model, I've a method which retrieve the rows which will be used as column in my view.

	public function getfamily_size()

	{

            $criteria=new CDbCriteria;

            $connection=Yii::app()->db;

            $sql= "SELECT vc_name FROM sp_vc_dd_tbl WHERE vc_type = 'family_size'";

            $command=$connection->createCommand($sql);

            $dataReader = $command->query();

            // using foreach to traverse through every row of data

            //$row = $dataReader->readAll();

            

            foreach($dataReader as $row)

            {

                $family_name['Name'][$row['vc_name']] = $row['vc_name'];

                //$family_name[$row['vc_name']]['Type'] = 'raw';

                //$family_name[$row['vc_name']]['Label'] = $row['vc_name'];

                //$family_name[$row['vc_name']]['Value'] = $row['vc_name'];

                //$family_size[] = $row['vc_name'];

               // echo $key." -- ".$val."<br>";

                //$family_name[]['Name'] = $row['vc_name'];

            }

            return $family_name;//array_merge($family_name, $family_value);

        }


// My gridview

$columns = $model->getfamily_size();

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

	'id'=>'sp-basic-info-tbl-grid',

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

	'filter'=>$model,

	'columns'=>$columns,

));



Okay guys… I’m done with the dynamic column thing. Here is the solution




// Model

   public function getfamily_size()

   {

       $criteria=new CDbCriteria;

       $connection=Yii::app()->db;

       $sql= "SELECT vc_name FROM sp_vc_dd_tbl WHERE vc_type = 'family_size'";

       $command=$connection->createCommand($sql);

       $dataReader = $command->query();

       // using foreach to traverse through every row of data

       //$row = $dataReader->readAll();

       foreach($dataReader as $row)

       {

         $family_size[$row['vc_name']] = $row['vc_name'];

       }            

       return $family_size;

   }


// View


$columns = $model->getfamily_size();

$array1 = array("id"=>"id", "benf_name"=>"benf_name", "benf_f_name"=>"benf_f_name", "gender"=>"gender", "cnic"=>"cnic");

$columns = array_merge($array1, $columns);

//print_r($columns);


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

	'id'=>'sp-basic-info-tbl-grid',

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

	//'filter'=>$model,

	'columns'=>$columns,



This gives exactly what I was looking for… but now I’m stuck with the values of the dynamic columns. I can’t figure it out that how I can retrieve their values????