Cgridview With Ccheckboxcolumn Value Checked

Hey guys …

i have problem when using CCheckBoxColumn in CGridView …

i want check item if a variable is in array … it work when i code like this

<?php

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

    'id'=&gt;'content-grid',


    'dataProvider'=&gt;&#036;model-&gt;search(),


    'columns'=&gt;array(


         array(


        'id'=&gt;'idx',


        'class'=&gt;'CCheckBoxColumn',


        'selectableRows' =&gt; '50',


        'checked'=&gt;'in_array(&#036;data-&gt;id,array(22,23))' , 


    		 		


    ),


        'nama',


    	


    ),

));

?>

but when i use array variabel $arr = array(22,23) … it’s failed … my coding below is not working

<?php

$arr = array(22,23);

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

    'id'=&gt;'content-grid',


    'dataProvider'=&gt;&#036;model-&gt;search(),


    'columns'=&gt;array(


         array(


        'id'=&gt;'idx',


        'class'=&gt;'CCheckBoxColumn',


        'selectableRows' =&gt; '50',


        'checked'=&gt;'in_array(&#036;data-&gt;id,&#036;arr)' , 


    		 		


    ),


        'nama',


    	


    ),

));

?>

can someone help me … thanks

Note:

You can use like that:

in Current Controller create function ex: isChecked

public function isChecked($id){

&#036;arr = array(22,23);


return in_array(&#036;id ,&#036;arr);

}

In view:

<?php

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

    'id'=&gt;'content-grid',


    'dataProvider'=&gt;&#036;model-&gt;search(),


    'columns'=&gt;array(


		array(


        'id'=&gt;'idx',


        'class'=&gt;'CCheckBoxColumn',


        'selectableRows' =&gt; '50',


        'checked'=&gt;'Yii::app()-&gt;controller-&gt;isChecked(&#036;data-&gt;id)' , 


             		


    ),


        'nama',


        


    ),

));

?>

Hi rojulman, welcome to the forum.

You can use only $row, $data and $this in the expression for ‘checked’. Local variables like $arr are not allowed in the expression.

http://www.yiiframework.com/doc/api/1.1/CCheckBoxColumn#checked-detail

So you have to use some means you can access from the expression.

While oxigen’s solution uses the controller’s public function, you may also simply declare a public property in the controller, and use it for the chosen ids.




$this->arr = array(22,23); // 'arr' is a public property in the controller

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

	'id'=>'content-grid',

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

	'columns'=>array(

		array(

			'id'=>'idx',

			'class'=>'CCheckBoxColumn',

			'selectableRows' => '50',

			'checked' => 'in_array($data->id, $this->grid->controller->arr)' , 

		),

		'nama',

	),

)); 



In the above, $this refers to the column, $this->grid is the grid, and $this->grid->controller is the controller.

Or, if you are using PHP 5.3.x, then you can use an anonymous function instead.




$arr = array(22,23);

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

	'id'=>'content-grid',

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

	'columns'=>array(

		array(

			'id'=>'idx',

			'class'=>'CCheckBoxColumn',

			'selectableRows' => '50',

			'checked' => function($data) use($arr) {

				return in_array($data->id, $arr); 

			}, 

		),

		'nama',

	),

)); 



ok guys thank you very much … it’s work like a charm … and solved my problem … :D