0 follower

CCheckBoxColumn

Package zii.widgets.grid
Inheritance class CCheckBoxColumn » CGridColumn » CComponent
Since 1.1
Source Code framework/zii/widgets/grid/CCheckBoxColumn.php
CCheckBoxColumn represents a grid view column of checkboxes.

CCheckBoxColumn supports no checking (read-only), single check and multiple checking. The mode is determined according to selectableRows. When in multiple checking mode, the header cell will display an additional checkbox, clicking on which will check or uncheck all of the checkboxes in the data cells. The header cell can be customized by headerTemplate.

Additionally selecting a checkbox can select a grid view row (depending on CGridView::selectableRows value) if selectableRows is null (default).

By default, the checkboxes rendered in data cells will have the values that are the same as the key values of the data model. One may change this by setting either name or value.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
checkBoxHtmlOptions array the HTML options for the checkboxes. CCheckBoxColumn
checked string a PHP expression that will be evaluated for every data cell and whose result will determine if checkbox for each data cell is checked. CCheckBoxColumn
cssClassExpression string a PHP expression that is evaluated for every data cell and whose result is used as the CSS class name for the data cell. CGridColumn
disabled string a PHP expression that will be evaluated for every data cell and whose result will determine if checkbox for each data cell is disabled. CCheckBoxColumn
filterCellContent string Returns the filter cell content. CGridColumn
filterHtmlOptions array the HTML options for the filter cell tag. CGridColumn
footerCellContent string Returns the footer cell content. CGridColumn
footerHtmlOptions array the HTML options for the footer cell tag. CCheckBoxColumn
grid CGridView the grid view object that owns this column. CGridColumn
hasFooter boolean whether this column has a footer cell. CGridColumn
headerCellContent string Returns the header cell content. CCheckBoxColumn
headerHtmlOptions array the HTML options for the header cell tag. CCheckBoxColumn
headerTemplate string the template to be used to control the layout of the header cell. CCheckBoxColumn
htmlOptions array the HTML options for the data cell tags. CCheckBoxColumn
id string the ID of this column. CGridColumn
name string the attribute name of the data model. CCheckBoxColumn
selectableRows integer the number of rows that can be checked. CCheckBoxColumn
value string a PHP expression that will be evaluated for every data cell and whose result will be rendered in each data cell as the checkbox value. CCheckBoxColumn
visible boolean whether this column is visible. CGridColumn

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CGridColumn
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
getDataCellContent() Returns the data cell content. CCheckBoxColumn
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getFilterCellContent() Returns the filter cell content. CGridColumn
getFooterCellContent() Returns the footer cell content. CGridColumn
getHasFooter() Returns whether this column has a footer cell. This is determined based on whether footer is set. CGridColumn
getHeaderCellContent() Returns the header cell content. CCheckBoxColumn
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
init() Initializes the column. CCheckBoxColumn
raiseEvent() Raises an event. CComponent
renderDataCell() Renders a data cell. CGridColumn
renderFilterCell() Renders the filter cell. CGridColumn
renderFooterCell() Renders the footer cell. CGridColumn
renderHeaderCell() Renders the header cell. CGridColumn

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
renderDataCellContent() Renders the data cell content. CGridColumn
renderFilterCellContent() Renders the filter cell content. CGridColumn
renderFooterCellContent() Renders the footer cell content. CGridColumn
renderHeaderCellContent() Renders the header cell content. CGridColumn

Property Details

checkBoxHtmlOptions property
public array $checkBoxHtmlOptions;

the HTML options for the checkboxes.

checked property (available since v1.1.4)
public string $checked;

a PHP expression that will be evaluated for every data cell and whose result will determine if checkbox for each data cell is checked. In this expression, you can use the following variables:

  • $row the row number (zero-based).
  • $data the value provided by grid view object for the row.
  • $this the column object.
Type of the $data depends on data provider which is passed to the grid view object. In case of CActiveDataProvider, $data will have object type and its values are accessed like $data->property. In case of CArrayDataProvider or CSqlDataProvider, it will have array type and its values must be accessed like $data['property'].

The PHP expression will be evaluated using evaluateExpression.

A PHP expression can be any PHP code that has a value. To learn more about what an expression is, please refer to the php manual.
disabled property (available since v1.1.13)
public string $disabled;

a PHP expression that will be evaluated for every data cell and whose result will determine if checkbox for each data cell is disabled. In this expression, you can use the following variables:

  • $row the row number (zero-based).
  • $data the value provided by grid view object for the row.
  • $this the column object.
Type of the $data depends on data provider which is passed to the grid view object. In case of CActiveDataProvider, $data will have object type and its values are accessed like $data->property. In case of CArrayDataProvider or CSqlDataProvider, it will have array type and its values must be accessed like $data['property'].

The PHP expression will be evaluated using evaluateExpression.

A PHP expression can be any PHP code that has a value. To learn more about what an expression is, please refer to the php manual.

Note that expression result will overwrite value set with checkBoxHtmlOptions['disabled'].
footerHtmlOptions property
public array $footerHtmlOptions;

the HTML options for the footer cell tag.

headerCellContent property read-only (available since v1.1.16)
public string getHeaderCellContent()

Returns the header cell content. This method will render a checkbox in the header when selectableRows is greater than 1 or in case selectableRows is null when CGridView::selectableRows is greater than 1.

headerHtmlOptions property
public array $headerHtmlOptions;

the HTML options for the header cell tag.

headerTemplate property (available since v1.1.11)
public string $headerTemplate;

the template to be used to control the layout of the header cell. The token "{item}" is recognized and it will be replaced with a "check all" checkbox. By default if in multiple checking mode, the header cell will display an additional checkbox, clicking on which will check or uncheck all of the checkboxes in the data cells. See selectableRows for more details.

htmlOptions property
public array $htmlOptions;

the HTML options for the data cell tags.

name property
public string $name;

the attribute name of the data model. The corresponding attribute value will be rendered in each data cell as the checkbox value. Note that if value is specified, this property will be ignored.

See Also

selectableRows property (available since v1.1.6)
public integer $selectableRows;

the number of rows that can be checked. Possible values:

  • 0 - the state of the checkbox cannot be changed (read-only mode)
  • 1 - only one row can be checked. Checking a checkbox has nothing to do with selecting the row
  • 2 or more - multiple checkboxes can be checked. Checking a checkbox has nothing to do with selecting the row
  • null - CGridView::selectableRows is used to control how many checkboxes can be checked. Checking a checkbox will also select the row.
You may also call the JavaScript function $(gridID).yiiGridView('getChecked', columnID) to retrieve the key values of the checked rows.
value property
public string $value;

a PHP expression that will be evaluated for every data cell and whose result will be rendered in each data cell as the checkbox value. In this expression, you can use the following variables:

  • $row the row number (zero-based).
  • $data the value provided by grid view object for the row.
  • $this the column object.
Type of the $data depends on data provider which is passed to the grid view object. In case of CActiveDataProvider, $data will have object type and its values are accessed like $data->property. In case of CArrayDataProvider or CSqlDataProvider, it will have array type and its values must be accessed like $data['property'].

The PHP expression will be evaluated using evaluateExpression.

A PHP expression can be any PHP code that has a value. To learn more about what an expression is, please refer to the php manual.

Method Details

getDataCellContent() method (available since v1.1.16)
public string getDataCellContent(integer $row)
$row integer the row number (zero-based)
{return} string the data cell content.
Source Code: framework/zii/widgets/grid/CCheckBoxColumn.php#238 (show)
public function getDataCellContent($row)
{
    
$data=$this->grid->dataProvider->data[$row];
    if(
$this->value!==null)
        
$value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
    elseif(
$this->name!==null)
        
$value=CHtml::value($data,$this->name);
    else
        
$value=$this->grid->dataProvider->keys[$row];

    
$checked false;
    if(
$this->checked!==null)
        
$checked=$this->evaluateExpression($this->checked,array('data'=>$data,'row'=>$row));

    
$options=$this->checkBoxHtmlOptions;
    if(
$this->disabled!==null)
        
$options['disabled']=$this->evaluateExpression($this->disabled,array('data'=>$data,'row'=>$row));

    
$name=$options['name'];
    unset(
$options['name']);
    
$options['value']=$value;
    
$options['id']=$this->id.'_'.$row;
    return 
CHtml::checkBox($name,$checked,$options);
}

Returns the data cell content. This method renders a checkbox in the data cell.

getHeaderCellContent() method (available since v1.1.16)
public string getHeaderCellContent()
{return} string the header cell content.
Source Code: framework/zii/widgets/grid/CCheckBoxColumn.php#214 (show)
public function getHeaderCellContent()
{
    if(
trim($this->headerTemplate)==='')
        return 
$this->grid->blankDisplay;

    if(
$this->selectableRows===null && $this->grid->selectableRows>1)
        
$item=CHtml::checkBox($this->id.'_all',false,array('class'=>'select-on-check-all'));
    elseif(
$this->selectableRows>1)
        
$item=CHtml::checkBox($this->id.'_all',false);
    else
        
$item=parent::getHeaderCellContent();

    return 
strtr($this->headerTemplate,array(
        
'{item}'=>$item,
    ));
}

Returns the header cell content. This method will render a checkbox in the header when selectableRows is greater than 1 or in case selectableRows is null when CGridView::selectableRows is greater than 1.

init() method
public void init()
Source Code: framework/zii/widgets/grid/CCheckBoxColumn.php#149 (show)
public function init()
{
    if(isset(
$this->checkBoxHtmlOptions['name']))
        
$name=$this->checkBoxHtmlOptions['name'];
    else
    {
        
$name=$this->id;
        if(
substr($name,-2)!=='[]')
            
$name.='[]';
        
$this->checkBoxHtmlOptions['name']=$name;
    }
    
$name=strtr($name,array('['=>"\\[",']'=>"\\]"));

    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="jQuery(\"input:not(#\"+this.id+\")[name='$name']\").prop('checked',false);";
    }
    elseif(
strpos($this->headerTemplate,'{item}')!==false)
    {
        
//.. process check/uncheck all
        
$cball=<<<CBALL
jQuery(document).on('click','#{$this->id}_all',function() {
var checked=this.checked;
jQuery("input[name='
$name']:enabled").each(function() {this.checked=checked;});
});

CBALL;
        
$cbcode="jQuery('#{$this->id}_all').prop('checked', jQuery(\"input[name='$name']\").length==jQuery(\"input[name='$name']:checked\").length);";
    }

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

Initializes the column. This method registers necessary client script for the checkbox column.