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.
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.
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
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…
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.
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.