# DataColumn

> DataColumn is the default column type for yii2\extensions\csvexport\CsvExporter.

[API reference](/docs/api/index.md) › [CSV Export](/docs/extensions/csv-export/reference/index.md) › DataColumn

| class | `yii2\extensions\csvexport\DataColumn` |
| --- | --- |
| Extends | [`Column`](/docs/extensions/csv-export/reference/column/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Source | [`src/DataColumn.php`](https://github.com/yii2-extensions/csv-export/blob/main/src/DataColumn.php#L17) |

DataColumn is the default column type for [`CsvExporter`](/docs/extensions/csv-export/reference/csvexporter/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$attribute`](#property-attribute) | `string\|null` | The attribute name associated with this column. When neither [`$content`](/docs/extensions/csv-export/reference/column/index.md#property-content) nor [`$value`](/docs/extensions/csv-export/reference/datacolumn/index.md#property-value) is specified, the value of the specified attribute will be retrieved from each data model and displayed. Default: `null`. | `DataColumn` |
| <span id="property-content"></span>[`$content`](/docs/extensions/csv-export/reference/column/index.md#property-content) | `callable(mixed, mixed, int,` [`Column`](/docs/extensions/csv-export/reference/column/index.md)`): mixed\|null` | This is a callable that will be used to generate the content of each cell. Default: `null`. | [`Column`](/docs/extensions/csv-export/reference/column/index.md) |
| <span id="property-footer"></span>[`$footer`](/docs/extensions/csv-export/reference/column/index.md#property-footer) | `string\|null` | Footer cell content, or `null` to use the exporter fallback. Default: `null`. | [`Column`](/docs/extensions/csv-export/reference/column/index.md) |
| [`$format`](#property-format) | `string\|array<int, mixed>` | Yii formatter name or format with arguments, such as `['date', 'php:Y-m-d']`. Default: `'raw'`. | `DataColumn` |
| <span id="property-grid"></span>[`$grid`](/docs/extensions/csv-export/reference/column/index.md#property-grid) | [`CsvExporter`](/docs/extensions/csv-export/reference/csvexporter/index.md) | The exporter that owns this column. | [`Column`](/docs/extensions/csv-export/reference/column/index.md) |
| <span id="property-header"></span>[`$header`](/docs/extensions/csv-export/reference/column/index.md#property-header) | `string\|null` | Header cell content, or `null` to use the exporter fallback. Default: `null`. | [`Column`](/docs/extensions/csv-export/reference/column/index.md) |
| <span id="property-label"></span>`$label` | `string\|null` | Header label when no explicit header is set. When unset, the column uses the label model's attribute label or humanizes the attribute name. Default: `null`. | `DataColumn` |
| <span id="property-labelModel"></span>`$labelModel` write-only | [`yii\base\Model`](/docs/api/yii/base/model/index.md)`\|null` | Model whose attribute labels supply headers, or `null` to use humanized attribute names. | `DataColumn` |
| [`$value`](#property-value) | `string\|callable(mixed, mixed, int,` [`DataColumn`](/docs/extensions/csv-export/reference/datacolumn/index.md)`): mixed\|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/extensions/csv-export/reference/column/index.md#property-visible) | `bool` | Whether this column is visible. Defaults to true. Default: `true`. | [`Column`](/docs/extensions/csv-export/reference/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-getGrid"></span>[`getGrid()`](/docs/extensions/csv-export/reference/column/index.md#method-getGrid) | Returns the exporter that owns this column. | [`Column`](/docs/extensions/csv-export/reference/column/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) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`renderDataCellContent()`](#method-renderDataCellContent) | Renders the formatted cell value, or the result of `content` when it is set. | `DataColumn` |
| <span id="method-renderFooterCellContent"></span>[`renderFooterCellContent()`](/docs/extensions/csv-export/reference/column/index.md#method-renderFooterCellContent) | Renders the footer cell using configured text or the exporter fallback. | [`Column`](/docs/extensions/csv-export/reference/column/index.md) |
| [`renderHeaderCellContent()`](#method-renderHeaderCellContent) | Renders the first available of `header`, `label`, the model's attribute label and a humanized attribute name. | `DataColumn` |
| <span id="method-setGrid"></span>[`setGrid()`](/docs/extensions/csv-export/reference/column/index.md#method-setGrid) | Sets the exporter that owns this column. | [`Column`](/docs/extensions/csv-export/reference/column/index.md) |
| [`setLabelModel()`](#method-setLabelModel) | Sets the label context from the effective source of this export. | `DataColumn` |

## Property details

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

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

The attribute name associated with this column. When neither [`$content`](/docs/extensions/csv-export/reference/column/index.md#property-content) nor [`$value`](/docs/extensions/csv-export/reference/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/extensions/csv-export/reference/datacolumn/index.md#property-label) is not specified, the label associated with the attribute will be displayed.

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

```php
public string|array<int, mixed> $format = 'raw'
```

Yii formatter name or format with arguments, such as `['date', 'php:Y-m-d']`.

The default `raw` format leaves values unformatted.

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

```php
public string|callable(mixed, mixed, int, yii2\extensions\csvexport\DataColumn): mixed|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/extensions/csv-export/reference/datacolumn/index.md) object.

You may also set this property to a string representing the attribute name to be displayed in this column.

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

## Method details

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

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

Returns the data cell value.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | `mixed` | Data row from the effective source. |
| `$key` | `mixed` | Key associated with the data row. |
| `$index` | `int` | Zero-based row index across the export. |

**Returns**

`mixed`: The unformatted value, or `null` when neither `value` nor `attribute` is set.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if an attribute is read from a row that is neither an array nor an object.

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

```php
public function renderDataCellContent(mixed $model, mixed $key, int $index): mixed
```

Renders the formatted cell value, or the result of `content` when it is set.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | `mixed` | Data row from the effective source. |
| `$key` | `mixed` | Key associated with the data row. |
| `$index` | `int` | Zero-based row index across the export. |

**Returns**

`mixed`: The formatted value, the exporter's `nullDisplay` for a `null` value, or the result of `content`.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if an attribute is read from a scalar row, or no exporter or formatter exists.

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

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

Renders the first available of `header`, `label`, the model's attribute label and a humanized attribute name.

**Returns**

`string`: The header text, or the exporter's empty-cell value when nothing names the column.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the fallback is needed and the column has no exporter.

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

```php
public function setLabelModel(yii\base\Model|null $model): void
```

Sets the label context from the effective source of this export.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | [`yii\base\Model`](/docs/api/yii/base/model/index.md)`\|null` | Model whose attribute labels supply headers, or `null` to use humanized attribute names. |