Skip to content
PHP Ecosystem
Explore

Table

API reference › yii › console › widgets › Table

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']],
],
]);
PropertyTypeDescriptionDefined by
$autoIdPrefix staticstringThe prefix to the automatically generated widget IDs. Default: 'w'.yii\base\Widget
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$idstringId of the widget.yii\base\Widget
$viewyii\web\ViewThe view object that can be used to render views or view files.yii\base\Widget
$viewPath read-onlystringThe directory containing the view files for this widget.yii\base\Widget
$chars protectedarrayTable 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
$columnWidths protectedarrayTable column widths Default: []. Available since 2.0.19.Table
$headers protectedarrayTable headers Default: []. Available since 2.0.19.Table
$listPrefix protectedstringList prefix Default: '• '. Available since 2.0.19.Table
$rows protectedarrayTable rows Default: []. Available since 2.0.19.Table
$screenWidth protectedintScreen width Default: null. Available since 2.0.19.Table
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\Component
__clone()This method is called after the object is created by cloning an existing one.yii\base\Component
__get()Returns the value of a component property.yii\base\Component
__isset()Checks if a property is set, i.e. defined and not null.yii\base\Component
__set()Sets the value of a component property.yii\base\Component
__unset()Sets a component property to be null.yii\base\Component
afterRun()This method is invoked right after a widget is executed.yii\base\Widget
attachBehavior()Attaches a behavior to this component.yii\base\Component
attachBehaviors()Attaches a list of behaviors to the component.yii\base\Component
beforeRun()This method is invoked right before the widget is executed.yii\base\Widget
begin() staticBegins a widget.yii\base\Widget
behaviors()Returns a list of behaviors that this component should behave as.yii\base\Component
canGetProperty()Returns a value indicating whether a property can be read.yii\base\Component
canSetProperty()Returns a value indicating whether a property can be set.yii\base\Component
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
detachBehavior()Detaches a behavior from the component.yii\base\Component
detachBehaviors()Detaches all behaviors from the component.yii\base\Component
end() staticEnds a widget.yii\base\Widget
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.yii\base\Component
getBehavior()Returns the named behavior object.yii\base\Component
getBehaviors()Returns all behaviors attached to this component.yii\base\Component
getId()Returns the ID of the widget.yii\base\Widget
getView()Returns the view object that can be used to render views or view files.yii\base\Widget
getViewPath()Returns the directory containing the view files for this widget.yii\base\Widget
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.yii\base\Component
hasMethod()Returns a value indicating whether a method is defined.yii\base\Component
hasProperty()Returns a value indicating whether a property is defined for this component.yii\base\Component
init()Initializes the object.yii\base\Widget
off()Detaches an existing event handler from this component.yii\base\Component
on()Attaches an event handler to an event.yii\base\Component
render()Renders a view.yii\base\Widget
renderFile()Renders a view file.yii\base\Widget
run()Table
setChars()Set table chars.Table
setHeaders()Set table headers.Table
setId()Sets the ID of the widget.yii\base\Widget
setListPrefix()Set list prefix.Table
setRows()Set table rows.Table
setScreenWidth()Set screen width.Table
setView()Sets the view object to be used by this widget.yii\base\Widget
trigger()Triggers an event.yii\base\Component
widget() staticCreates a widget instance and runs it.yii\base\Widget
calculateRowHeight() protectedCalculate the height of a row.Table
calculateRowsSize() protectedCalculate the size of rows to draw anchor of columns in console.Table
getScreenWidth() protectedGetting screen width.Table
renderRow() protectedRenders a row of data into a string.Table
renderSeparator() protectedRenders separator.Table
EventTypeDescriptionDefined by
EVENT_INITyii\base\EventAn event that is triggered when the widget is initialized via init(). Event name: 'init'. Available since 2.0.11.yii\base\Widget
EVENT_BEFORE_RUNyii\base\WidgetEventAn event raised right before executing a widget. Event name: 'beforeRun'. Available since 2.0.11.yii\base\Widget
EVENT_AFTER_RUNyii\base\WidgetEventAn event raised right after executing a widget. Event name: 'afterRun'. Available since 2.0.11.yii\base\Widget
ConstantValueDescriptionDefined by
DEFAULT_CONSOLE_SCREEN_WIDTH120Table
CONSOLE_SCROLLBAR_OFFSET3Table
CHAR_TOP'top'Table
CHAR_TOP_MID'top-mid'Table
CHAR_TOP_LEFT'top-left'Table
CHAR_TOP_RIGHT'top-right'Table
CHAR_BOTTOM'bottom'Table
CHAR_BOTTOM_MID'bottom-mid'Table
CHAR_BOTTOM_LEFT'bottom-left'Table
CHAR_BOTTOM_RIGHT'bottom-right'Table
CHAR_LEFT'left'Table
CHAR_LEFT_MID'left-mid'Table
CHAR_MID'mid'Table
CHAR_MID_MID'mid-mid'Table
CHAR_RIGHT'right'Table
CHAR_RIGHT_MID'right-mid'Table
CHAR_MIDDLE'middle'Table
public function run(): string

Returns

string: The rendered table

public function setChars(array $chars): $this

Set table chars.

Parameters

ParameterTypeDescription
$charsarrayTable chars

Returns

$this

public function setHeaders(array $headers): $this

Set table headers.

Parameters

ParameterTypeDescription
$headersarrayTable headers

Returns

$this

public function setListPrefix(string $listPrefix): $this

Set list prefix.

Parameters

ParameterTypeDescription
$listPrefixstringList prefix

Returns

$this

public function setRows(array $rows): $this

Set table rows.

Parameters

ParameterTypeDescription
$rowsarrayTable rows

Returns

$this

public function setScreenWidth(int $width): $this

Set screen width.

Parameters

ParameterTypeDescription
$widthintScreen width

Returns

$this

protected function calculateRowHeight(array $row): int

Calculate the height of a row.

Parameters

ParameterTypeDescription
$rowarray

Returns

int: Maximum row per cell

See also

protected function calculateRowsSize(): mixed

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

Returns

mixed

See also

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

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

Renders a row of data into a string.

Parameters

ParameterTypeDescription
$rowarrayRow of data
$spanLeftstringCharacter for left border
$spanMiddlestringCharacter for middle border
$spanRightstringCharacter for right border

Returns

string

See also

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

Renders separator.

Parameters

ParameterTypeDescription
$spanLeftstringCharacter for left border
$spanMidstringCharacter for middle border
$spanMidMidstringCharacter for middle-middle border
$spanRightstringCharacter for right border

Returns

string: The generated separator row

See also