CCheckBoxColumn header checkbox doesn't properly update checkboxes?

I’m trying to use a CCheckBoxColumn in a gridview which allows multiple selections (Yii 1.1.2).

Checking individual checkboxes in the column will update that row and add the “selected” class to it’s css as expected.

The selected rows can then be viewed using


$.fn.yiiGridView.getSelection('container-id')

However checking the top checkbox in the header of the table doesn’t work as expected - it does successfully check all the boxes however it doesn’t update the individual rows and add the “selected” class. You can infact then uncheck a row and it will toggle that rows class to be selected even though the row itself is un-checked.

Specifically, the expected behaviour is that if you load the page and tick the checkbox in the header the following should happen:

  • All checkboxes in the CCheckBoxColumn should be checked.

  • All rows should be updated to have the "selected" class added.

  • Running $.fn.yiiGridView.getSelection(‘container-id’) should return all the checked rows.

A condensed copy of my code is below:




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

  'id'=>'container-id',

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

  'filter'=>$model,

  'selectableRows'=>2,

  'columns'=>array(

    array(

      'class'=>'CCheckBoxColumn',

    ),

    'id',

    'name',


    ...


));



I had the same problem, so I fixed it and added an extension. Try this out here:

http://www.yiiframework.com/extension/fixed-checkbox-column/

This really should be fixed in Yii directly ??!

That extension does not work in Yii 1.1.5, but IMO CCheckBoxColumn should behave properly.

Any comments before I file that bug? :)

It’s filed already:

Issue 1394

It didn’t make it into Yii 1.1.5, so let’s hope for Yii 1.1.6. :P

It sure will be… I’m working on it…

Currently this is just because of a jQuery (let’s say) bug… that the row gets selected when clicking on the checkbox… because the TR.click event is fired even if a checkbox is clicked…

To understand what I mean… try to click in the checkbox column but not on the checkbox… click on the small space between the checkbox and the column border… you will get the row selected… so as it is currently this is not even a functionality programmed on the checkbox…

Even more on this… if the checkbox by default is selected… then unselecting that checkbox… the row will get selected… always because of the same bug…

And the getSelection() function returns selected rows… not checked rows… so I’m planning to add the getChecked() function…

I see.

Yes, it’s strange.

I can mark rows as selected by clicking anywhere in the row, so the checkboxes doesn’t need to be there. :)

I just removed the checkbox column, and now it works much better: when you click a row, it toggles selected.

Will put the checkboxes back in when this is fixed.

Thanks! :)

I made some modifications/enhancement to the CCheckBoxColumn…

Can you test it, if you have time?

Can you link me to the new file? I can test the crap out of it this weekend.

Here they are - http://code.google.com/p/yii/source/detail?r=2848

Just remember to delete the assets folder…

The solution for this is pretty simple:

go to the file CCheckBoxColumn.php inside the folder framework\zii\widgets\grid

And then go to init function.

You are going to see that the function for the “select all” checkbox only CHECKS all the checkboxes, but it doesn’t add the class SELECTED. The getSelected function seeks for the checkboxes that have the class “selected”, so that javascript function had to be corrected. The solution, then, is to add this lines to that function (I’m talking about line 130 in version 1.1.6:

	if(checked) { //CMT added 2010-07-09


		$(this).closest('tr').addClass('selected'); //CMT added 2010-07-09


	} else { //CMT added 2010-07-09


		$(this).closest('tr').removeClass('selected'); //CMT added 2010-07-09


	} //CMT added 2010-07-09


}); 

)

This wonderful solution is from Issue 1394, in code.google.com

Another method is to change the getSelected function so it can check for the “checked” property instead of the “selected” class, but I haven’t found where to change this yet.

I hope this saves a lot of time, because it seems it isn’t still added for new Yii versions.