Skip to content
PHP Ecosystem
Explore

CheckboxColumn

API reference › yii › grid › CheckboxColumn

classyii\grid\CheckboxColumn
ExtendsColumn » yii\base\BaseObject
Implementsyii\base\Configurable
Since2.0
Sourceframework/grid/CheckboxColumn.php

CheckboxColumn displays a column of checkboxes in a grid view.

To add a CheckboxColumn to the GridView, add it to the columns configuration as follows:

'columns' => [
// ...
[
'class' => 'yii\grid\CheckboxColumn',
// you may configure additional properties here
],
]

Users may click on the checkboxes to select rows of the grid. The selected rows may be obtained by calling the following JavaScript code:

var keys = $('#grid').yiiGridView('getSelectedRows');
// keys is an array consisting of the keys associated with the selected rows

For more details and usage information on CheckboxColumn, see the guide article on data widgets.

PropertyTypeDescriptionDefined by
$checkboxOptionsarray|ClosureThe HTML attributes for checkboxes. Default: [].CheckboxColumn
$clientScriptarray|yii\web\client\ClientScriptInterface|nullClient script class to use for client-side validation. Default: null.CheckboxColumn
$contentcallableThis is a callable that will be used to generate the content of each cell. Default: null.Column
$contentOptionsarray|ClosureThe HTML attributes for the data cell tag. Default: [].Column
$cssClassstringThe css class that will be used to find the checkboxes. Default: null. Available since 2.0.9.CheckboxColumn
$filterOptionsarrayThe HTML attributes for the filter cell tag. Default: [].Column
$footerstringThe footer cell content. Default: null.Column
$footerOptionsarrayThe HTML attributes for the footer cell tag. Default: [].Column
$gridGridViewThe grid view object that owns this column. Default: null.Column
$headerstring|nullThe header cell content. Default: null.Column
$headerOptionsarrayThe HTML attributes for the header cell tag. Default: [].Column
$multipleboolWhether it is possible to select multiple rows. Default: true.CheckboxColumn
$namestringThe name of the input checkbox input fields. Default: 'selection'.CheckboxColumn
$optionsarrayThe HTML attributes for the column group tag. Default: [].Column
$visibleboolWhether this column is visible. Default: true.Column
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\BaseObject
__get()Returns the value of an object property.yii\base\BaseObject
__isset()Checks if a property is set, i.e. defined and not null.yii\base\BaseObject
__set()Sets value of an object property.yii\base\BaseObject
__unset()Sets an object property to null.yii\base\BaseObject
canGetProperty()Returns a value indicating whether a property can be read.yii\base\BaseObject
canSetProperty()Returns a value indicating whether a property can be set.yii\base\BaseObject
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
hasMethod()Returns a value indicating whether a method is defined.yii\base\BaseObject
hasProperty()Returns a value indicating whether a property is defined.yii\base\BaseObject
init()Initializes the object.CheckboxColumn
registerClientScript()Registers the needed JavaScript.CheckboxColumn
renderDataCell()Renders a data cell.Column
renderFilterCell()Renders the filter cell.Column
renderFooterCell()Renders the footer cell.Column
renderHeaderCell()Renders the header cell.Column
getHeaderCellLabel() protectedReturns header cell label.Column
getHeaderCheckBoxName() protectedReturns header checkbox name.CheckboxColumn
renderDataCellContent() protectedRenders the data cell content.CheckboxColumn
renderFilterCellContent() protectedRenders the filter cell content.Column
renderFooterCellContent() protectedRenders the footer cell content.Column
renderHeaderCellContent() protectedRenders the header cell content.CheckboxColumn
public array|Closure $checkboxOptions = []

The HTML attributes for checkboxes.

This can either be an array of attributes or an anonymous function (Closure) that returns such an array. The signature of the function should be the following: function ($model, $key, $index, $column). Where $model, $key, and $index refer to the model, key and index of the row currently being rendered and $column is a reference to the CheckboxColumn object. A function may be used to assign different attributes to different rows based on the data in that row. Specifically if you want to set a different value for the checkbox you can use this option in the following way (in this example using the name attribute of the model):

'checkboxOptions' => function ($model, $key, $index, $column) {
return ['value' => $model->name];
}
public bool $multiple = true

Whether it is possible to select multiple rows.

Defaults to true.

public string $name = 'selection'

The name of the input checkbox input fields.

This will be appended with [] to ensure it is an array.

public function init(): mixed

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

Returns

mixed

Throws

public function registerClientScript(): mixed

Registers the needed JavaScript.

Returns

mixed

Since

2.0.8

protected function getHeaderCheckBoxName(): string

Returns header checkbox name.

Returns

string: Header checkbox name

Since

2.0.8

protected function renderDataCellContent(mixed $model, mixed $key, mixed $index): string

Renders the data cell content.

Parameters

ParameterTypeDescription
$modelmixedThe data model
$keymixedThe key associated with the data model
$indexmixedThe zero-based index of the data model among the models array returned by GridView::$dataProvider.

Returns

string: The rendering result

protected function renderHeaderCellContent(): string

Renders the header cell content.

The default implementation simply renders $header. This method may be overridden to customize the rendering of the header cell.

Returns

string: The rendering result