Skip to content

Commit

Permalink
Refactored CCheckBoxColumn
Browse files Browse the repository at this point in the history
  • Loading branch information
mdomba committed Jan 13, 2011
1 parent 709f5ff commit 0452f5a
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 16 deletions.
80 changes: 76 additions & 4 deletions framework/zii/widgets/assets/gridview/jquery.yiigridview.js
Expand Up @@ -47,13 +47,53 @@
$.fn.yiiGridView.update(id, {data: data});
});

$.fn.yiiGridView.selectCheckedRows(id);

if(settings.selectableRows > 0) {
$('#'+id+' .'+settings.tableClass+' > tbody > tr').live('click',function(){
if(settings.selectableRows == 1)
$(this).siblings().removeClass('selected');
$(this).toggleClass('selected');
$('#'+id+' .'+settings.tableClass+' > tbody > tr').live('click',function(e){
if('checkbox'!=e.target.type){
var $sbox=$('input.select-on-check',this);
if(settings.selectableRows == 1){
$(this).siblings().removeClass('selected');
$("input[name='"+$sbox.attr('name')+"']").attr('checked',false);
}
$(this).toggleClass('selected');
$sbox.attr('checked',$(this).hasClass('selected'));
if(settings.selectionChanged != undefined)
settings.selectionChanged(id);
}
});
}

$('#'+id+' .'+settings.tableClass+' > tbody > tr > td > input.select-on-check').live('click',function(){
if(settings.selectableRows ==0)
return false;

var $row=$(this).parent().parent();
if(settings.selectableRows == 1){
$row.siblings().removeClass('selected');
$("input:not(#"+this.id+")[name='"+this.name+"']").attr('checked',false);
}
if($(this).attr('checked'))
$row.addClass('selected');
else
$row.removeClass('selected');
if(settings.selectionChanged != undefined)
settings.selectionChanged(id);
return true;
});

if(settings.selectableRows > 1) {
$('#'+id+' .'+settings.tableClass+' > thead > tr > th > input.select-on-check-all').live('click',function(){
var checked=this.checked;
var name=this.name.substring(0,this.name.length-4)+'\[\]'; //.. remove '_all' and add '[]''
$("input[name='"+name+"']").each(function() {
this.checked=checked;
if(checked)
$(this).parent().parent().addClass('selected');
else
$(this).parent().parent().removeClass('selected');
});
});
}
});
Expand Down Expand Up @@ -134,6 +174,7 @@
$d=$(data),
$filtered=$d.filter(id);
$(id).replaceWith( $filtered.size() ? $filtered : $d.find(id));
$.fn.yiiGridView.selectCheckedRows(v);
});
if(settings.afterAjaxUpdate != undefined)
settings.afterAjaxUpdate(id, data);
Expand Down Expand Up @@ -175,6 +216,18 @@
}
};

/**
* Selects rows that have checkbox checked (only checkbox that is connected with selecting a row)
* @param id string the ID of the grid view container
*/
$.fn.yiiGridView.selectCheckedRows = function(id) {
var settings = $.fn.yiiGridView.settings[id];
$('#'+id+' .'+settings.tableClass+' > tbody > tr > td >input.select-on-check').each(function(){
if($(this).attr('checked'))
$(this).parent().parent().addClass('selected');
});
};

/**
* Returns the key values of the currently selected rows.
* @param id string the ID of the grid view container
Expand All @@ -191,4 +244,23 @@
return selection;
};

/**
* Returns the key values of the currently checked rows.
* @param id string the ID of the grid view container
* @param column_id string the ID of the column
* @return array the key values of the currently selected rows.
*/
$.fn.yiiGridView.getChecked = function(id,column_id) {
var settings = $.fn.yiiGridView.settings[id];
var keys = $('#'+id+' > div.keys > span');
if(column_id.substring(column_id.length-2)!='\[\]')
column_id=column_id+'\[\]';
var checked = [];
$('#'+id+' .'+settings.tableClass+' > tbody > tr > td > input[name="'+column_id+'"]').each(function(i){
if($(this).attr('checked'))
checked.push(keys.eq(i).text());
});
return checked;
};

})(jQuery);
67 changes: 55 additions & 12 deletions framework/zii/widgets/grid/CCheckBoxColumn.php
Expand Up @@ -65,6 +65,21 @@ class CCheckBoxColumn extends CGridColumn
* @var array the HTML options for the checkboxes.
*/
public $checkBoxHtmlOptions=array();
/**
* @var integer the number of rows that can be checked.
* Possible values:
* <ul>
* <li>0 - the state of the checkbox cannot be changed (read-only mode)</li>
* <li>1 - only one row can be checked. Checking a checkbox has nothing to do with selecting the row</li>
* <li>2 or more - multiple checkboxes can be checked. Checking a checkbox has nothing to do with selecting the row</li>
* <li>null - {@link CGridView::selectableRows} is used to control how many checkboxes can be checked.
* Cheking a checkbox will also select the row.</li>
* </ul>
* You may also call the JavaScript function <code>$.fn.yiiGridView.getChecked(containerID,columnID)</code>
* to retrieve the key values of the checked rows.
* @since 1.1.6
*/
public $selectableRows=null;

/**
* Initializes the column.
Expand All @@ -82,31 +97,59 @@ public function init()
$this->checkBoxHtmlOptions['name']=$name;
}
$name=strtr($name,array('['=>"\\[",']'=>"\\]"));
if($this->grid->selectableRows==1)
$one="\n\tjQuery(\"input:not(#\"+$(this).attr('id')+\")[name='$name']\").attr('checked',false);";

if($this->selectableRows===null)
{
if(isset($this->checkBoxHtmlOptions['class']))
$this->checkBoxHtmlOptions['class'].=' select-on-check';
else
$this->checkBoxHtmlOptions['class']='select-on-check';
return;
}

$cball=$cbcode='';
if($this->selectableRows==0)
{
//.. read only
$cbcode="return false;";
}
elseif($this->selectableRows==1)
{
//.. only one can be checked, uncheck all other
$cbcode="$(\"input:not(#\"+$(this).attr('id')+\")[name='$name']\").attr('checked',false);";
}
else
$one='';
$js=<<<EOD
jQuery('#{$this->id}_all').live('click',function() {
{
//.. process check/uncheck all
$cball=<<<CBALL
$('#{$this->id}_all').live('click',function() {
var checked=this.checked;
jQuery("input[name='$name']").each(function() {
this.checked=checked;
});
$("input[name='$name']").each(function() {this.checked=checked;});
});
jQuery("input[name='$name']").live('click', function() {
jQuery('#{$this->id}_all').attr('checked', jQuery("input[name='$name']").length==jQuery("input[name='$name']:checked").length);{$one}
CBALL;
$cbcode="$('#{$this->id}_all').attr('checked', $(\"input[name='$name']\").length==$(\"input[name='$name']:checked\").length);";
}

$js=$cball;
$js.=<<<EOD
$("input[name='$name']").live('click', function() {
$cbcode
});
EOD;
Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$this->id,$js);
}

/**
* Renders the header cell content.
* This method will render a checkbox in the header when {@link CGridView::selectableRows} is greater than 1.
* This method will render a checkbox in the header when {@link selectableRows} is greater than 1
* or in case {@link selectableRows} is null when {@link CGridView::selectableRows} is greater than 1.
*/
protected function renderHeaderCellContent()
{
if($this->grid->selectableRows>1)
if($this->selectableRows===null && $this->grid->selectableRows>1)
echo CHtml::checkBox($this->id.'_all',false,array('class'=>'select-on-check-all'));
else if($this->selectableRows>1)
echo CHtml::checkBox($this->id.'_all',false);
else
parent::renderHeaderCellContent();
Expand Down

0 comments on commit 0452f5a

Please sign in to comment.