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']], ],]);Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$autoIdPrefix static | string | The prefix to the automatically generated widget IDs. Default: 'w'. | yii\ |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$id | string | Id of the widget. | yii\ |
$view | yii\ | The view object that can be used to render views or view files. | yii\ |
$viewPath read-only | string | The directory containing the view files for this widget. | yii\ |
$chars protected | array | Table chars Default: [self::CHAR_TOP => '═',. Available since 2.0.19. | Table |
$columnWidths protected | array | Table column widths Default: []. Available since 2.0.19. | Table |
$headers protected | array | Table headers Default: []. Available since 2.0.19. | Table |
$listPrefix protected | string | List prefix Default: '• '. Available since 2.0.19. | Table |
$rows protected | array | Table rows Default: []. Available since 2.0.19. | Table |
$screenWidth protected | int | Screen width Default: null. Available since 2.0.19. | Table |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
afterRun() | This method is invoked right after a widget is executed. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
beforeRun() | This method is invoked right before the widget is executed. | yii\ |
begin() static | Begins a widget. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
end() static | Ends a widget. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getId() | Returns the ID of the widget. | yii\ |
getView() | Returns the view object that can be used to render views or view files. | yii\ |
getViewPath() | Returns the directory containing the view files for this widget. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the object. | yii\ |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
render() | Renders a view. | yii\ |
renderFile() | Renders a view file. | yii\ |
run() | Table | |
setChars() | Set table chars. | Table |
setHeaders() | Set table headers. | Table |
setId() | Sets the ID of the widget. | yii\ |
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\ |
trigger() | Triggers an event. | yii\ |
widget() static | Creates a widget instance and runs it. | yii\ |
calculateRowHeight() protected | Calculate the height of a row. | Table |
calculateRowsSize() protected | Calculate the size of rows to draw anchor of columns in console. | Table |
getScreenWidth() protected | Getting screen width. | Table |
renderRow() protected | Renders a row of data into a string. | Table |
renderSeparator() protected | Renders separator. | Table |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_INIT | yii\ | An event that is triggered when the widget is initialized via init(). Event name: 'init'. Available since 2.0.11. | yii\ |
EVENT_BEFORE_RUN | yii\ | An event raised right before executing a widget. Event name: 'beforeRun'. Available since 2.0.11. | yii\ |
EVENT_AFTER_RUN | yii\ | An event raised right after executing a widget. Event name: 'afterRun'. Available since 2.0.11. | yii\ |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
DEFAULT_CONSOLE_SCREEN_WIDTH | 120 | Table | |
CONSOLE_SCROLLBAR_OFFSET | 3 | Table | |
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 |
Method details
Section titled “Method details”setChars()
Section titled “setChars() ”public function setChars(array $chars): $thisSet table chars.
Parameters
| Parameter | Type | Description |
|---|---|---|
$chars | array | Table chars |
Returns
$this
setHeaders()
Section titled “setHeaders() ”public function setHeaders(array $headers): $thisSet table headers.
Parameters
| Parameter | Type | Description |
|---|---|---|
$headers | array | Table headers |
Returns
$this
setListPrefix()
Section titled “setListPrefix() ”public function setListPrefix(string $listPrefix): $thisSet list prefix.
Parameters
| Parameter | Type | Description |
|---|---|---|
$listPrefix | string | List prefix |
Returns
$this
setRows()
Section titled “setRows() ”public function setRows(array $rows): $thisSet table rows.
Parameters
| Parameter | Type | Description |
|---|---|---|
$rows | array | Table rows |
Returns
$this
setScreenWidth()
Section titled “setScreenWidth() ”public function setScreenWidth(int $width): $thisSet screen width.
Parameters
| Parameter | Type | Description |
|---|---|---|
$width | int | Screen width |
Returns
$this
calculateRowHeight()
Section titled “calculateRowHeight() ”protected function calculateRowHeight(array $row): intCalculate the height of a row.
Parameters
| Parameter | Type | Description |
|---|---|---|
$row | array |
Returns
int: Maximum row per cell
See also
calculateRowsSize()
Section titled “calculateRowsSize() ”protected function calculateRowsSize(): mixedCalculate the size of rows to draw anchor of columns in console.
getScreenWidth()
Section titled “getScreenWidth() ”protected function getScreenWidth(): intGetting screen width.
If it is not able to determine screen width, default value 123 will be set.
Returns
int: Screen width
renderRow()
Section titled “renderRow() ”protected function renderRow(array $row, string $spanLeft, string $spanMiddle, string $spanRight): stringRenders 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
renderSeparator()
Section titled “renderSeparator() ”protected function renderSeparator(string $spanLeft, string $spanMid, string $spanMidMid, string $spanRight): stringRenders 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