# CheckboxColumn

> CheckboxColumn displays a column of checkboxes in a grid view.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [grid](/docs/api/yii/grid/index.md) › CheckboxColumn

| class | `yii\grid\CheckboxColumn` |
| --- | --- |
| Extends | [`Column`](/docs/api/yii/grid/column/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/grid/CheckboxColumn.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/grid/CheckboxColumn.php#L46) |

CheckboxColumn displays a column of checkboxes in a grid view.

To add a CheckboxColumn to the [`GridView`](/docs/api/yii/grid/gridview/index.md), add it to the [columns](/docs/api/yii/grid/gridview/index.md#property-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](/docs/guide/output-data-widgets/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$checkboxOptions`](#property-checkboxOptions) | `array\|`[`Closure`](https://www.php.net/manual/en/class.closure.php) | The HTML attributes for checkboxes. Default: `[]`. | `CheckboxColumn` |
| <span id="property-clientScript"></span>`$clientScript` | `array\|`[`yii\web\client\ClientScriptInterface`](/docs/api/yii/web/client/clientscriptinterface/index.md)`\|null` | Client script class to use for client-side validation. Default: `null`. | `CheckboxColumn` |
| <span id="property-content"></span>[`$content`](/docs/api/yii/grid/column/index.md#property-content) | `callable` | This is a callable that will be used to generate the content of each cell. Default: `null`. | [`Column`](/docs/api/yii/grid/column/index.md) |
| <span id="property-contentOptions"></span>[`$contentOptions`](/docs/api/yii/grid/column/index.md#property-contentOptions) | `array\|`[`Closure`](https://www.php.net/manual/en/class.closure.php) | The HTML attributes for the data cell tag. Default: `[]`. | [`Column`](/docs/api/yii/grid/column/index.md) |
| <span id="property-cssClass"></span>`$cssClass` | `string` | The css class that will be used to find the checkboxes. Default: `null`. Available since 2.0.9. | `CheckboxColumn` |
| <span id="property-filterOptions"></span>[`$filterOptions`](/docs/api/yii/grid/column/index.md#property-filterOptions) | `array` | The HTML attributes for the filter cell tag. Default: `[]`. | [`Column`](/docs/api/yii/grid/column/index.md) |
| <span id="property-footer"></span>[`$footer`](/docs/api/yii/grid/column/index.md#property-footer) | `string` | The footer cell content. Default: `null`. | [`Column`](/docs/api/yii/grid/column/index.md) |
| <span id="property-footerOptions"></span>[`$footerOptions`](/docs/api/yii/grid/column/index.md#property-footerOptions) | `array` | The HTML attributes for the footer cell tag. Default: `[]`. | [`Column`](/docs/api/yii/grid/column/index.md) |
| <span id="property-grid"></span>[`$grid`](/docs/api/yii/grid/column/index.md#property-grid) | [`GridView`](/docs/api/yii/grid/gridview/index.md) | The grid view object that owns this column. Default: `null`. | [`Column`](/docs/api/yii/grid/column/index.md) |
| <span id="property-header"></span>[`$header`](/docs/api/yii/grid/column/index.md#property-header) | `string\|null` | The header cell content. Default: `null`. | [`Column`](/docs/api/yii/grid/column/index.md) |
| <span id="property-headerOptions"></span>[`$headerOptions`](/docs/api/yii/grid/column/index.md#property-headerOptions) | `array` | The HTML attributes for the header cell tag. Default: `[]`. | [`Column`](/docs/api/yii/grid/column/index.md) |
| [`$multiple`](#property-multiple) | `bool` | Whether it is possible to select multiple rows. Default: `true`. | `CheckboxColumn` |
| [`$name`](#property-name) | `string` | The name of the input checkbox input fields. Default: `'selection'`. | `CheckboxColumn` |
| <span id="property-options"></span>[`$options`](/docs/api/yii/grid/column/index.md#property-options) | `array` | The HTML attributes for the column group tag. Default: `[]`. | [`Column`](/docs/api/yii/grid/column/index.md) |
| <span id="property-visible"></span>[`$visible`](/docs/api/yii/grid/column/index.md#property-visible) | `bool` | Whether this column is visible. Default: `true`. | [`Column`](/docs/api/yii/grid/column/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`init()`](#method-init) | Initializes the object. | `CheckboxColumn` |
| [`registerClientScript()`](#method-registerClientScript) | Registers the needed JavaScript. | `CheckboxColumn` |
| <span id="method-renderDataCell"></span>[`renderDataCell()`](/docs/api/yii/grid/column/index.md#method-renderDataCell) | Renders a data cell. | [`Column`](/docs/api/yii/grid/column/index.md) |
| <span id="method-renderFilterCell"></span>[`renderFilterCell()`](/docs/api/yii/grid/column/index.md#method-renderFilterCell) | Renders the filter cell. | [`Column`](/docs/api/yii/grid/column/index.md) |
| <span id="method-renderFooterCell"></span>[`renderFooterCell()`](/docs/api/yii/grid/column/index.md#method-renderFooterCell) | Renders the footer cell. | [`Column`](/docs/api/yii/grid/column/index.md) |
| <span id="method-renderHeaderCell"></span>[`renderHeaderCell()`](/docs/api/yii/grid/column/index.md#method-renderHeaderCell) | Renders the header cell. | [`Column`](/docs/api/yii/grid/column/index.md) |
| <span id="method-getHeaderCellLabel"></span>[`getHeaderCellLabel()`](/docs/api/yii/grid/column/index.md#method-getHeaderCellLabel) protected | Returns header cell label. | [`Column`](/docs/api/yii/grid/column/index.md) |
| [`getHeaderCheckBoxName()`](#method-getHeaderCheckBoxName) protected | Returns header checkbox name. | `CheckboxColumn` |
| [`renderDataCellContent()`](#method-renderDataCellContent) protected | Renders the data cell content. | `CheckboxColumn` |
| <span id="method-renderFilterCellContent"></span>[`renderFilterCellContent()`](/docs/api/yii/grid/column/index.md#method-renderFilterCellContent) protected | Renders the filter cell content. | [`Column`](/docs/api/yii/grid/column/index.md) |
| <span id="method-renderFooterCellContent"></span>[`renderFooterCellContent()`](/docs/api/yii/grid/column/index.md#method-renderFooterCellContent) protected | Renders the footer cell content. | [`Column`](/docs/api/yii/grid/column/index.md) |
| [`renderHeaderCellContent()`](#method-renderHeaderCellContent) protected | Renders the header cell content. | `CheckboxColumn` |

## Property details

### $checkboxOptions <span id="property-checkboxOptions"></span>

```php
public array|Closure $checkboxOptions = []
```

The HTML attributes for checkboxes.

This can either be an array of
attributes or an anonymous function ([`Closure`](https://www.php.net/manual/en/class.closure.php)) 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`](/docs/api/yii/grid/checkboxcolumn/index.md) 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];
}
```

### $multiple <span id="property-multiple"></span>

```php
public bool $multiple = true
```

Whether it is possible to select multiple rows.

Defaults to `true`.

### $name <span id="property-name"></span>

```php
public string $name = 'selection'
```

The name of the input checkbox input fields.

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

## Method details

### init() <span id="method-init"></span>

```php
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**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if [`$name`](/docs/api/yii/grid/checkboxcolumn/index.md#property-name) is not set.

### registerClientScript() <span id="method-registerClientScript"></span>

```php
public function registerClientScript(): mixed
```

Registers the needed JavaScript.

**Returns**

`mixed`

**Since**

2.0.8

### getHeaderCheckBoxName() <span id="method-getHeaderCheckBoxName"></span>

```php
protected function getHeaderCheckBoxName(): string
```

Returns header checkbox name.

**Returns**

`string`: Header checkbox name

**Since**

2.0.8

### renderDataCellContent() <span id="method-renderDataCellContent"></span>

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

Renders the data cell content.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | `mixed` | The data model |
| `$key` | `mixed` | The key associated with the data model |
| `$index` | `mixed` | The zero-based index of the data model among the models array returned by [`GridView::$dataProvider`](/docs/api/yii/widgets/baselistview/index.md#property-dataProvider). |

**Returns**

`string`: The rendering result

### renderHeaderCellContent() <span id="method-renderHeaderCellContent"></span>

```php
protected function renderHeaderCellContent(): string
```

Renders the header cell content.

The default implementation simply renders [`$header`](/docs/api/yii/grid/column/index.md#property-header).
This method may be overridden to customize the rendering of the header cell.

**Returns**

`string`: The rendering result