Yii Framework Forum: Ability to pre-populate "checked" value in ccheckboxcolumn - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Ability to pre-populate "checked" value in ccheckboxcolumn It is impossible to pre-pop the checked status in grid view... Rate Topic: ***** 1 Votes

#1 User is offline   davemyers 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 08-January 10

Posted 14 April 2010 - 01:14 PM

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

#2 User is offline   whoopass 

  • Advanced Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 119
  • Joined: 27-May 09
  • Location:Ajax, Ontario, Canada

Posted 14 April 2010 - 01:43 PM

View Postdavemyers, on 14 April 2010 - 01:14 PM, said:

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

Quote

$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few specialattributes are also recognized. See clientChange and tag for more details.In addition, the following options are also supported specifically for list box:
  • prompt: string, specifies the prompt text shown as the first list option. Its value is empty.
  • empty: string, specifies the text corresponding to empty selection. Its value is empty.Starting from version 1.0.10, the 'empty' option can also be an array of value-label pairs.Each pair will be used to render a list option at the beginning.
  • options: array, specifies additional attributes for each OPTION tag. The array keys must be the option values, and the array values are the extra OPTION tag attributes in the name-value pairs. For example, array(
    'value1'=>array('disabled'=>true, 'label'=>'value 1'),
    'value2'=>array('label'=>'value 2'),
    );

    This option has been available since version 1.0.3.



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 :)
0

#3 User is offline   davemyers 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 08-January 10

Posted 14 April 2010 - 02:01 PM

View Postwhoopass, on 14 April 2010 - 01:43 PM, said:

Should work pretty much the same.


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

check here: http://www.yiiframew...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.
0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,358
  • Joined: 12-October 09
  • Location:Croatia

Posted 15 April 2010 - 02:32 AM

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.
Find more about me.... btw. Do you know your WAN IP?
1

#5 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,358
  • Joined: 12-October 09
  • Location:Croatia

Posted 15 April 2010 - 02:59 AM

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

Take a look at http://www.yiiframew...ycheckboxcolumn

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...
Find more about me.... btw. Do you know your WAN IP?
0

#6 User is offline   davemyers 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 08-January 10

Posted 15 April 2010 - 09:44 AM

View Postmdomba, on 15 April 2010 - 02:59 AM, said:

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

Take a look at http://www.yiiframew...ycheckboxcolumn


Thanks mdomba! I think this should be incorporated, but looks like qiang disagrees...
http://code.google.c...Stars%20Summary
0

#7 User is offline   _wk_ 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 152
  • Joined: 22-January 10

Posted 14 July 2010 - 10:38 AM

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

#8 User is offline   Pep 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 06-August 11

Posted 14 April 2012 - 08:22 AM

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,
             ),
             ...

2

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users