Ability to pre-populate "checked" value in ccheckboxcolumn

Unless I’m missing something huge,

There is currently no way to pre-populate the checked status of a checkbox in a CGridView CCheckBoxColumn.

checkBoxHtmlOptions does not allow it either.

Seems like an big oversight, so if I’m wrong, please someone point me in the right direction, but otherwise, the ability to send in “checked” or not should be added.

Depending on what version you’re using, 1.1.x or 1.0.x, the class documentation is very powerful. It can show you what is and what isn’t part of certain classes.

1.1.x documentation --> http://www.yiiframework.com/doc/api/

1.0.x documentation --> http://www.yiiframework.com/doc/api/1.0.12/

When you search, ajax scripting pulls up real time string statements that represent classes and their methods or attributes. In a format of ClassName.AttributeOrMethodName

You can view the source of all the classes this way.

Searching up CHtml.checkboxlist you’ll get the information about it. It also points at the bottom there’s more informat on listBox which is another function or class.

You search that up and you’ll see the information about that method. Reading into it you can see you have the ability to specify htmloptions

I’m pressuming if you pass something like




array('value1'=>array('checked'=true, ...),



That should precheck the box. Once you implement it with the CHtml::checkBoxList method, try and see if you can establish the gridview version of it. Should work pretty much the same.

Hope that helps :)

I’m familiar with the documentation. It does not work the same.

check here: http://www.yiiframework.com/doc/api/CCheckBoxColumn

I’ve implemented as follows:




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

	'id'=>'grid',

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

	'selectableRows'=>20,

	'filter'=>$model,

	'columns'=>array(

		array(

			'class'=>'CCheckBoxColumn',

			'checkBoxHtmlOptions'=>array(

				'checked'=>'checked',

			),			

		),

		array(

			'class'=>'CDataColumn',

			'name'=>'id',

		),

...



And yet no checkboxes are pre-checked.

I still haven’t used the CCheckBoxColumn…

I took a look at the source code and I can confirm that in current implementation the checkbox cannot be checked, because in the source of CCheckBoxColumn the function renderDataCellContent has a call




CHtml::checkBox($this->id.'[]',[b]false[/b],$options);



where false says that the column is unchecked

So if you need to set some checkboxes the only way is to make a custom checkBoxColumn and befor that call to test something like




if(isset($options['checked']))

   CHtml::checkBox($this->id.'[]',[b]true[/b],$options);

else

   CHtml::checkBox($this->id.'[]',[b]false[/b],$options);



with this modification your code would work.

I had some time so I created an extension to solve this problem.

Take a look at http://www.yiiframework.com/extension/mycheckboxcolumn

With this extension I added the ‘checked’ attribute to the CheckBoxColumn so your code can look like




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

        'id'=>'grid',

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

        'selectableRows'=>20,

        'filter'=>$model,

        'columns'=>array(

                array(

                        'class'=>'CCheckBoxColumn',

                        'checked'=>'true',

                ),

                array(

                        'class'=>'CDataColumn',

                        'name'=>'id',

                ),



but can be more complex, eg. the ‘checked’ value can be an string expression that evaluates current row data to decide if the checbox will be checked… just use $data as the data model…

Thanks mdomba! I think this should be incorporated, but looks like qiang disagrees…

http://code.google.com/p/yii/issues/detail?id=712&can=1&q=CCheckBoxColumn&colspec=ID%20Type%20Status%20Priority%20Milestone%20Owner%20Stars%20Summary

Hi,

don’t know if you came across a solution but if not, I’ve just created one. Simply create a new class that extends CCheckBoxColumn;





class KCheckBoxColumn extends CCheckBoxColumn

{

	/**

	 * evaluate string for initial state

	 * @var	string

	 */

	public $selected = '';

	

	/**

	 * Renders the data cell content.

	 * This method renders a checkbox in the data cell.

	 * @param integer the row number (zero-based)

	 * @param mixed the data associated with the row

	 */

	protected function renderDataCellContent($row,$data)

	{

		if($this->value!==null)

			$value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));

		else if($this->name!==null)

			$value=CHtml::value($data,$this->name);

		else

			$value=$this->grid->dataProvider->keys[$row];

		$options=$this->checkBoxHtmlOptions;

		$options['value']=$value;

		$options['id']=$this->id.'_'.$row;

		echo CHtml::checkBox($this->id.'[]',$this->evaluateExpression($this->selected,array('data'=>$data,'row'=>$row)),$options);

	}

}



and in your view file, you can create an expression for ‘selected’ e.g.




'columns'=>array(

	array(

	'class'=>'KCheckBoxColumn',

	'header'=>'',

	'value'=>'$data->id',

	'selected'=>'in_array($data->id,array('.implode(',',$somearray).'))'

)



hope that helps…

Just an update for anyone looking for a current answer to this. The checked property from mdomba’s extension has been incorporated into Yii since version 1.1.4.

If you are using an array to store the selections, you can pass the selections into the checked property by creating an expression from the array. CCheckBoxColumn will evaluate the expression and check the checkbox if the expression evaluates to true.

The expression:




$rowSelection = 'in_array($data->id,array('.($_POST['my-grid_c0'] ?

                        implode(',', $_POST['my-grid_c0']) : '').'))';

Now you can pass this into the checked property:




'columns'=>array(

             array(

               'class'=>'CCheckBoxColumn',

               'checked'=>$rowSelection,

             ),

             ...