# Table

> Table class displays a table in console.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [console](/docs/api/yii/console/index.md) › [widgets](/docs/api/yii/console/widgets/index.md) › Table

| class | `yii\console\widgets\Table` |
| --- | --- |
| Extends | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) » [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\ViewContextInterface`](/docs/api/yii/base/viewcontextinterface/index.md), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0.13 |
| Source | [`framework/console/widgets/Table.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/console/widgets/Table.php#L53) |

Table class displays a table in console.

For example,

```
$table = new Table();

echo $table
    ->setHeaders(['test1', 'test2', 'test3'])
    ->setRows([
        ['col1', 'col2', 'col3'],
        ['col1', 'col2', ['col3-0', 'col3-1', 'col3-2']],
    ])
    ->run();
```

or

```
echo Table::widget([
    'headers' => ['test1', 'test2', 'test3'],
    'rows' => [
        ['col1', 'col2', 'col3'],
        ['col1', 'col2', ['col3-0', 'col3-1', 'col3-2']],
    ],
]);
```

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-autoIdPrefix"></span>[`$autoIdPrefix`](/docs/api/yii/base/widget/index.md#property-autoIdPrefix) static | `string` | The prefix to the automatically generated widget IDs. Default: `'w'`. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-id"></span>[`$id`](/docs/api/yii/base/widget/index.md#property-id) | `string` | Id of the widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="property-view"></span>[`$view`](/docs/api/yii/base/widget/index.md#property-view) | [`yii\web\View`](/docs/api/yii/web/view/index.md) | The view object that can be used to render views or view files. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="property-viewPath"></span>[`$viewPath`](/docs/api/yii/base/widget/index.md#property-viewPath) read-only | `string` | The directory containing the view files for this widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="property-chars"></span>`$chars` protected | `array` | Table chars Default: `[self::CHAR_TOP => '═', self::CHAR_TOP_MID => '╤', self::CHAR_TOP_LEFT => '╔', self::CHAR_TOP_RIGHT => '╗', self::CHAR_BOTTOM => '═', self::CHAR_BOTTOM_MID => '╧', self::CHAR_BOTTOM_LEFT => '╚', self::CHAR_BOTTOM_RIGHT => '╝', self::CHAR_LEFT => '║', self::CHAR_LEFT_MID => '╟', self::CHAR_MID => '─', self::CHAR_MID_MID => '┼', self::CHAR_RIGHT => '║', self::CHAR_RIGHT_MID => '╢', self::CHAR_MIDDLE => '│']`. Available since 2.0.19. | `Table` |
| <span id="property-columnWidths"></span>`$columnWidths` protected | `array` | Table column widths Default: `[]`. Available since 2.0.19. | `Table` |
| <span id="property-headers"></span>`$headers` protected | `array` | Table headers Default: `[]`. Available since 2.0.19. | `Table` |
| <span id="property-listPrefix"></span>`$listPrefix` protected | `string` | List prefix Default: `'• '`. Available since 2.0.19. | `Table` |
| <span id="property-rows"></span>`$rows` protected | `array` | Table rows Default: `[]`. Available since 2.0.19. | `Table` |
| <span id="property-screenWidth"></span>`$screenWidth` protected | `int` | Screen width Default: `null`. Available since 2.0.19. | `Table` |

## 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/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-afterRun"></span>[`afterRun()`](/docs/api/yii/base/widget/index.md#method-afterRun) | This method is invoked right after a widget is executed. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-beforeRun"></span>[`beforeRun()`](/docs/api/yii/base/widget/index.md#method-beforeRun) | This method is invoked right before the widget is executed. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-begin"></span>[`begin()`](/docs/api/yii/base/widget/index.md#method-begin) static | Begins a widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/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-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-end"></span>[`end()`](/docs/api/yii/base/widget/index.md#method-end) static | Ends a widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getId"></span>[`getId()`](/docs/api/yii/base/widget/index.md#method-getId) | Returns the ID of the widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-getView"></span>[`getView()`](/docs/api/yii/base/widget/index.md#method-getView) | Returns the view object that can be used to render views or view files. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-getViewPath"></span>[`getViewPath()`](/docs/api/yii/base/widget/index.md#method-getViewPath) | Returns the directory containing the view files for this widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/widget/index.md#method-init) | Initializes the object. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-render"></span>[`render()`](/docs/api/yii/base/widget/index.md#method-render) | Renders a view. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-renderFile"></span>[`renderFile()`](/docs/api/yii/base/widget/index.md#method-renderFile) | Renders a view file. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| [`run()`](#method-run) |  | `Table` |
| [`setChars()`](#method-setChars) | Set table chars. | `Table` |
| [`setHeaders()`](#method-setHeaders) | Set table headers. | `Table` |
| <span id="method-setId"></span>[`setId()`](/docs/api/yii/base/widget/index.md#method-setId) | Sets the ID of the widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| [`setListPrefix()`](#method-setListPrefix) | Set list prefix. | `Table` |
| [`setRows()`](#method-setRows) | Set table rows. | `Table` |
| [`setScreenWidth()`](#method-setScreenWidth) | Set screen width. | `Table` |
| <span id="method-setView"></span>[`setView()`](/docs/api/yii/base/widget/index.md#method-setView) | Sets the view object to be used by this widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-widget"></span>[`widget()`](/docs/api/yii/base/widget/index.md#method-widget) static | Creates a widget instance and runs it. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| [`calculateRowHeight()`](#method-calculateRowHeight) protected | Calculate the height of a row. | `Table` |
| [`calculateRowsSize()`](#method-calculateRowsSize) protected | Calculate the size of rows to draw anchor of columns in console. | `Table` |
| [`getScreenWidth()`](#method-getScreenWidth) protected | Getting screen width. | `Table` |
| [`renderRow()`](#method-renderRow) protected | Renders a row of data into a string. | `Table` |
| [`renderSeparator()`](#method-renderSeparator) protected | Renders separator. | `Table` |

## Events

| Event | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="event-EVENT_INIT"></span>[`EVENT_INIT`](/docs/api/yii/base/widget/index.md#event-EVENT_INIT) | [`yii\base\Event`](/docs/api/yii/base/event/index.md) | An event that is triggered when the widget is initialized via [`init()`](/docs/api/yii/base/widget/index.md#method-init). Event name: `'init'`. Available since 2.0.11. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="event-EVENT_BEFORE_RUN"></span>[`EVENT_BEFORE_RUN`](/docs/api/yii/base/widget/index.md#event-EVENT_BEFORE_RUN) | [`yii\base\WidgetEvent`](/docs/api/yii/base/widgetevent/index.md) | An event raised right before executing a widget. Event name: `'beforeRun'`. Available since 2.0.11. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="event-EVENT_AFTER_RUN"></span>[`EVENT_AFTER_RUN`](/docs/api/yii/base/widget/index.md#event-EVENT_AFTER_RUN) | [`yii\base\WidgetEvent`](/docs/api/yii/base/widgetevent/index.md) | An event raised right after executing a widget. Event name: `'afterRun'`. Available since 2.0.11. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-DEFAULT_CONSOLE_SCREEN_WIDTH"></span>`DEFAULT_CONSOLE_SCREEN_WIDTH` | `120` |  | `Table` |
| <span id="const-CONSOLE_SCROLLBAR_OFFSET"></span>`CONSOLE_SCROLLBAR_OFFSET` | `3` |  | `Table` |
| <span id="const-CHAR_TOP"></span>`CHAR_TOP` | `'top'` |  | `Table` |
| <span id="const-CHAR_TOP_MID"></span>`CHAR_TOP_MID` | `'top-mid'` |  | `Table` |
| <span id="const-CHAR_TOP_LEFT"></span>`CHAR_TOP_LEFT` | `'top-left'` |  | `Table` |
| <span id="const-CHAR_TOP_RIGHT"></span>`CHAR_TOP_RIGHT` | `'top-right'` |  | `Table` |
| <span id="const-CHAR_BOTTOM"></span>`CHAR_BOTTOM` | `'bottom'` |  | `Table` |
| <span id="const-CHAR_BOTTOM_MID"></span>`CHAR_BOTTOM_MID` | `'bottom-mid'` |  | `Table` |
| <span id="const-CHAR_BOTTOM_LEFT"></span>`CHAR_BOTTOM_LEFT` | `'bottom-left'` |  | `Table` |
| <span id="const-CHAR_BOTTOM_RIGHT"></span>`CHAR_BOTTOM_RIGHT` | `'bottom-right'` |  | `Table` |
| <span id="const-CHAR_LEFT"></span>`CHAR_LEFT` | `'left'` |  | `Table` |
| <span id="const-CHAR_LEFT_MID"></span>`CHAR_LEFT_MID` | `'left-mid'` |  | `Table` |
| <span id="const-CHAR_MID"></span>`CHAR_MID` | `'mid'` |  | `Table` |
| <span id="const-CHAR_MID_MID"></span>`CHAR_MID_MID` | `'mid-mid'` |  | `Table` |
| <span id="const-CHAR_RIGHT"></span>`CHAR_RIGHT` | `'right'` |  | `Table` |
| <span id="const-CHAR_RIGHT_MID"></span>`CHAR_RIGHT_MID` | `'right-mid'` |  | `Table` |
| <span id="const-CHAR_MIDDLE"></span>`CHAR_MIDDLE` | `'middle'` |  | `Table` |

## Method details

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

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

**Returns**

`string`: The rendered table

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

```php
public function setChars(array $chars): $this
```

Set table chars.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$chars` | `array` | Table chars |

**Returns**

`$this`

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

```php
public function setHeaders(array $headers): $this
```

Set table headers.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$headers` | `array` | Table headers |

**Returns**

`$this`

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

```php
public function setListPrefix(string $listPrefix): $this
```

Set list prefix.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$listPrefix` | `string` | List prefix |

**Returns**

`$this`

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

```php
public function setRows(array $rows): $this
```

Set table rows.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$rows` | `array` | Table rows |

**Returns**

`$this`

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

```php
public function setScreenWidth(int $width): $this
```

Set screen width.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$width` | `int` | Screen width |

**Returns**

`$this`

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

```php
protected function calculateRowHeight(array $row): int
```

Calculate the height of a row.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$row` | `array` |  |

**Returns**

`int`: Maximum row per cell

**See also**

- [`Table::render()`](/docs/api/yii/base/widget/index.md#method-render)

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

```php
protected function calculateRowsSize(): mixed
```

Calculate the size of rows to draw anchor of columns in console.

**Returns**

`mixed`

**See also**

- [`Table::render()`](/docs/api/yii/base/widget/index.md#method-render)

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

```php
protected function getScreenWidth(): int
```

Getting screen width.

If it is not able to determine screen width, default value `123` will be set.

**Returns**

`int`: Screen width

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

```php
protected function renderRow(array $row, string $spanLeft, string $spanMiddle, string $spanRight): string
```

Renders a row of data into a string.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$row` | `array` | Row of data |
| `$spanLeft` | `string` | Character for left border |
| `$spanMiddle` | `string` | Character for middle border |
| `$spanRight` | `string` | Character for right border |

**Returns**

`string`

**See also**

- [`Table::render()`](/docs/api/yii/base/widget/index.md#method-render)

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

```php
protected function renderSeparator(string $spanLeft, string $spanMid, string $spanMidMid, string $spanRight): string
```

Renders separator.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$spanLeft` | `string` | Character for left border |
| `$spanMid` | `string` | Character for middle border |
| `$spanMidMid` | `string` | Character for middle-middle border |
| `$spanRight` | `string` | Character for right border |

**Returns**

`string`: The generated separator row

**See also**

- [`Table::render()`](/docs/api/yii/base/widget/index.md#method-render)