# DataColumn

> DataColumn is the default column type for the yii\grid\GridView widget.

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

| class | `yii\grid\DataColumn` |
| --- | --- |
| 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/DataColumn.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/grid/DataColumn.php#L40) |

1 subclass:

- [`yii\debug\widgets\DebugDataColumn`](/docs/extensions/debug/reference/widgets/debugdatacolumn/index.md)

DataColumn is the default column type for the [`GridView`](/docs/api/yii/grid/gridview/index.md) widget.

It is used to show data columns and allows [sorting](/docs/api/yii/grid/datacolumn/index.md#property-enableSorting) and [filtering](/docs/api/yii/grid/datacolumn/index.md#property-filter) them.

A simple data column definition refers to an attribute in the data model of the
GridView's data provider. The name of the attribute is specified by [`$attribute`](/docs/api/yii/grid/datacolumn/index.md#property-attribute).

By setting [`$value`](/docs/api/yii/grid/datacolumn/index.md#property-value) and [`$label`](/docs/api/yii/grid/datacolumn/index.md#property-label), the header and cell content can be customized.

A data column differentiates between the [data cell value](/docs/api/yii/grid/datacolumn/index.md#method-getDataCellValue) and the
[data cell content](/docs/api/yii/grid/datacolumn/index.md#method-renderDataCellContent). The cell value is an un-formatted value that
may be used for calculation, while the actual cell content is a [formatted](/docs/api/yii/grid/datacolumn/index.md#property-format) version of that
value which may contain HTML markup.

For more details and usage information on DataColumn, see the [guide article on data widgets](/docs/guide/output-data-widgets/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$attribute`](#property-attribute) | `string` | The attribute name associated with this column. Default: `null`. | `DataColumn` |
| <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) |
| [`$enableSorting`](#property-enableSorting) | `bool` | Whether to allow sorting by this column. Default: `true`. | `DataColumn` |
| <span id="property-encodeLabel"></span>`$encodeLabel` | `bool` | Whether the header label should be HTML-encoded. Default: `true`. Available since 2.0.1. | `DataColumn` |
| [`$filter`](#property-filter) | `string\|array\|null\|false` | The HTML code representing a filter input (e.g. a text field, a dropdown list) that is used for this data column. Default: `null`. | `DataColumn` |
| [`$filterAttribute`](#property-filterAttribute) | `string\|null` | The attribute name of the [`GridView::$filterModel`](/docs/api/yii/grid/gridview/index.md#property-filterModel) associated with this column. Default: `null`. Available since 2.0.41. | `DataColumn` |
| [`$filterInputOptions`](#property-filterInputOptions) | `array` | The HTML attributes for the filter input fields. Default: `['id' => null]`. | `DataColumn` |
| <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) |
| [`$format`](#property-format) | `string\|array\|`[`Closure`](https://www.php.net/manual/en/class.closure.php) | In which format should the value of each data model be displayed as (e.g. `"raw"`, `"text"`, `"html"`, `['date', 'php:Y-m-d']`). Default: `'text'`. | `DataColumn` |
| <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) |
| [`$label`](#property-label) | `string\|null` | Label to be displayed in the [header cell](/docs/api/yii/grid/column/index.md#property-header) and also to be used as the sorting link label when sorting is enabled for this column. Default: `null`. | `DataColumn` |
| <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-sortLinkOptions"></span>`$sortLinkOptions` | `array` | The HTML attributes for the link tag in the header cell generated by [`Sort::link()`](/docs/api/yii/data/sort/index.md#method-link) when sorting is enabled for this column. Default: `[]`. | `DataColumn` |
| [`$value`](#property-value) | `string\|`[`Closure`](https://www.php.net/manual/en/class.closure.php)`\|null` | An anonymous function or a string that is used to determine the value to display in the current column. Default: `null`. | `DataColumn` |
| <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) |
| [`getDataCellValue()`](#method-getDataCellValue) | Returns the data cell value. | `DataColumn` |
| <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. | `DataColumn` |
| <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) |
| [`getHeaderCellLabel()`](#method-getHeaderCellLabel) protected | Returns header cell label. | `DataColumn` |
| [`renderDataCellContent()`](#method-renderDataCellContent) protected | Renders the data cell content. | `DataColumn` |
| [`renderFilterCellContent()`](#method-renderFilterCellContent) protected | Renders the filter cell content. | `DataColumn` |
| <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. | `DataColumn` |

## Property details

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

```php
public string $attribute = null
```

The attribute name associated with this column.

When neither [`$content`](/docs/api/yii/grid/column/index.md#property-content) nor [`$value`](/docs/api/yii/grid/datacolumn/index.md#property-value)
is specified, the value of the specified attribute will be retrieved from each data model and displayed.

Also, if [`$label`](/docs/api/yii/grid/datacolumn/index.md#property-label) is not specified, the label associated with the attribute will be displayed.

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

```php
public bool $enableSorting = true
```

Whether to allow sorting by this column.

If true and [`$attribute`](/docs/api/yii/grid/datacolumn/index.md#property-attribute) is found in
the sort definition of [`GridView::$dataProvider`](/docs/api/yii/widgets/baselistview/index.md#property-dataProvider), then the header cell of this column
will contain a link that may trigger the sorting when being clicked.

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

```php
public string|array|null|false $filter = null
```

The HTML code representing a filter input (e.g. a text field, a dropdown list) that is used for this data column.

This property is effective only when [`GridView::$filterModel`](/docs/api/yii/grid/gridview/index.md#property-filterModel) is set.

- If this property is not set, a text field will be generated as the filter input with attributes defined
  with [`$filterInputOptions`](/docs/api/yii/grid/datacolumn/index.md#property-filterInputOptions). See [`BaseHtml::activeInput()`](/docs/api/yii/helpers/basehtml/index.md#method-activeInput) for details on how an active
  input tag is generated.
- If this property is an array, a dropdown list will be generated that uses this property value as
  the list options.
- If you don't want a filter for this data column, set this value to be false.

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

```php
public string|null $filterAttribute = null
```

The attribute name of the [`GridView::$filterModel`](/docs/api/yii/grid/gridview/index.md#property-filterModel) associated with this column.

If not set,
will have the same value as [`$attribute`](/docs/api/yii/grid/datacolumn/index.md#property-attribute).

**Since**

2.0.41

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

```php
public array $filterInputOptions = ['id' => null]
```

The HTML attributes for the filter input fields.

This property is used in combination with
the [`$filter`](/docs/api/yii/grid/datacolumn/index.md#property-filter) property. When [`$filter`](/docs/api/yii/grid/datacolumn/index.md#property-filter) is not set or is an array, this property will be used to
render the HTML attributes for the generated filter input fields.

Empty `id` in the default value ensures that id would not be obtained from the model attribute thus
providing better performance.

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

```php
public string|array|Closure $format = 'text'
```

In which format should the value of each data model be displayed as (e.g. `"raw"`, `"text"`, `"html"`, `['date', 'php:Y-m-d']`).

Supported formats are determined by the [formatter](/docs/api/yii/grid/gridview/index.md#property-formatter) used by
the [`GridView`](/docs/api/yii/grid/gridview/index.md). Default format is "text" which will format the value as an HTML-encoded plain text when
[`Formatter`](/docs/api/yii/i18n/formatter/index.md) is used as the [formatter](/docs/api/yii/grid/gridview/index.md#property-formatter) of the GridView.

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

```php
public string|null $label = null
```

Label to be displayed in the [header cell](/docs/api/yii/grid/column/index.md#property-header) and also to be used as the sorting link label when sorting is enabled for this column.

If it is not set and the models provided by the GridViews data provider are instances
of [`ActiveRecord`](/docs/api/yii/db/activerecord/index.md), the label will be determined using [`ActiveRecord::getAttributeLabel()`](/docs/api/yii/db/baseactiverecord/index.md#method-getAttributeLabel).
Otherwise [`Inflector::camel2words()`](/docs/api/yii/helpers/baseinflector/index.md#method-camel2words) will be used to get a label.

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

```php
public string|Closure|null $value = null
```

An anonymous function or a string that is used to determine the value to display in the current column.

If this is an anonymous function, it will be called for each row and the return value will be used as the value to
display for every data model. The signature of this function should be: `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 [`DataColumn`](/docs/api/yii/grid/datacolumn/index.md) object.

You may also set this property to a string representing the attribute name to be displayed in this column.
This can be used when the attribute to be displayed is different from the [`$attribute`](/docs/api/yii/grid/datacolumn/index.md#property-attribute) that is used for
sorting and filtering.

If this is not set, `$model[$attribute]` will be used to obtain the value, where `$attribute` is the value of [`$attribute`](/docs/api/yii/grid/datacolumn/index.md#property-attribute).

## Method details

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

```php
public function getDataCellValue(mixed $model, mixed $key, int $index): string|null
```

Returns the data cell value.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | `mixed` | The data model |
| `$key` | `mixed` | The key associated with the data model |
| `$index` | `int` | 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|null`: The data cell value

### 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`

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

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

Returns header cell label.

This method may be overridden to customize the label of the header cell.

**Returns**

`string`: Label

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

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

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

Renders the filter cell content.

The default implementation simply renders a space.
This method may be overridden to customize the rendering of the filter cell (if any).

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