Table

Table #

PhpTui\Tui\Extension\Core\Widget\TableWidget

Shows tabular data arranged in columns. The column spacing is determined by the “width” constraints.

Example #

Show code

<?php

use PhpTui\Tui\DisplayBuilder;
use PhpTui\Tui\Extension\Core\Widget\Table\TableCell;
use PhpTui\Tui\Extension\Core\Widget\Table\TableRow;
use PhpTui\Tui\Extension\Core\Widget\TableWidget;
use PhpTui\Tui\Layout\Constraint;

require 'vendor/autoload.php';

$display = DisplayBuilder::default()->build();
$display->draw(
    TableWidget::default()
        ->widths(
            Constraint::percentage(25),
            Constraint::percentage(25),
            Constraint::percentage(50),
        )
        ->header(
            TableRow::fromStrings(
                'Name',
                'Quantity',
                'Approved?',
            )->height(2)->bottomMargin(2)
        )
        ->rows(
            TableRow::fromCells(
                TableCell::fromString('Cliff'),
                TableCell::fromString('1'),
                TableCell::fromString('✅'),
            ),
            TableRow::fromStrings(
                'Tree',
                '15',
                '✅',
            ),
            TableRow::fromStrings(
                'Slate',
                '519',
                '✅',
            ),
        )
);

Parameters #

Configure the widget using the builder methods named as follows:

NameTypeDescription
stylePhpTui\Tui\Style\StyleStyle of the area occupied by the table.
widthslist<Constraint>Constraints to use to determine the column widths.
columnSpacingintSpacing to enforce between columns.
highlightStylePhpTui\Tui\Style\StyleStyle used when a row is highlighted.
highlightSymbolstringSymbol to show when the row is highlighted.
headerPhpTui\Tui\Extension\Core\Widget\Table\TableRow|nullOptional header.
rowslist<TableRow>Table rows.
highlightSpacingPhpTui\Tui\Extension\Core\Widget\List\HighlightSpacingHighlight spacing strategy.
statePhpTui\Tui\Extension\Core\Widget\Table\TableStateHold the state of the table (i.e. selected row, current offset).