Yii 1.1: Adding 'disabled' to CCheckBoxColumn and support for yiigridview.js

7 followers

I recently had to have a checkbox column in my grid view that supported the 'disabled' attribute. This was easy enough to do by extending the CCheckBoxColumn. However, I also wanted the checkbox to be selected when clicking on the table's row (supported via CGridView's selectableRows option). I discovered I had to tweak yiigridview.js so that it would not select disabled rows when clicking on it or on the 'select all' checkbox.

ECheckBoxColumn.php

Save this file to protected/extensions/. This was taken directly from http://www.yiiframework.com/forum/index.php/topic/20495-disable-checkbox-in-ccheckboxcolumn-based-on-attribute-value/

<?php
 
class ECheckBoxColumn extends CCheckBoxColumn
{
        public $disabled;
        protected function renderDataCellContent($row,$data)
        {
                if($this->disabled!==null)
                        $this->checkBoxHtmlOptions['disabled']=$this->evaluateExpression($this->disabled,array('data'=>$data,'row'=>$row));
                parent::renderDataCellContent($row,$data);
        }
?>

jquery.yiigridview.js

Copy this file with related files (css, img, etc) to /protected/assets/gridview/ or wherever you want it (if different, be sure to change it in the usage scenario). I'm just going to link the whole file here: http://pastebin.com/b5t9NdmZ

Usage

$this->widget('bootstrap.widgets.TbGridView', array(
    'baseScriptUrl' => Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('app.assets')).'/gridview',
    'dataProvider'=>$dataProvider,
    'selectableRows'=>2,
    'rowCssClassExpression' => '(expresion) ? null : "disabled"',
    'columns'=>array(
            array(
            'class'=>'ext.ECheckBoxColumn',
            'disabled'=>'(true/false exression (same as rowCssClassExpression)',
        ),
    ),
));

Limitations

'disabled' class is hard coded into the modified JS code. For me to add a custom 'disabled' class, I would have to extend CGridView, and to me it just wasn't worth adding a small property for it to be passed into yiigridview.js.

Total 3 comments

#10666 report it
sucotronic at 2012/11/13 02:51am
of course

You can check here that only the enabled chekboxes are selected.

#10665 report it
Xgamer99 at 2012/11/12 11:36pm
great!

wonderful! Does your commit also change the gridview javascript file to support it when selecting rows? they go hand in hand

#10657 report it
sucotronic at 2012/11/12 05:30am
Added in main trunk some time ago

I commited a pull request some time ago with this feature, and it will be available in next release :)

Leave a comment

Please to leave your comment.

Write new article