PHP code example of bosun18 / tui-datatable

1. Go to this page and download the library: Download bosun18/tui-datatable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bosun18 / tui-datatable example snippets


use Bosun18\TuiDataTable\Align;
use Bosun18\TuiDataTable\Column;
use Bosun18\TuiDataTable\Event\RowSelectEvent;
use Bosun18\TuiDataTable\TableWidget;
use Symfony\Component\Tui\Tui;

$table = new TableWidget(
    columns: [
        new Column('name', 'Package'),
        new Column(
            'downloads',
            'Downloads',
            align: Align::Right,
            formatter: static fn (mixed $value): string => number_format((int) $value),
        ),
        new Column('license', 'License', width: 14, sortable: false),
    ],
    rows: [
        ['name' => 'symfony/tui', 'downloads' => 41207, 'license' => 'MIT'],
        ['name' => 'symfony/console', 'downloads' => 812004331, 'license' => 'MIT'],
    ],
    maxVisible: 15,
);

$tui = new Tui();
$tui->addStyleSheet(TableWidget::defaultStyleSheet());

$table
    ->onRowSelect(static function (RowSelectEvent $event) use ($tui): void {
        // $event->row is the row array, $event->index its position on screen
        $tui->stop();
    })
    ->onCancel(static fn () => $tui->stop());

$tui->add($table);
$tui->setFocus($table);
$tui->run();

new TableWidget($columns, $rows, 10, new Keybindings([
    'cancel' => [Key::ESCAPE, 'ctrl+c', 'q'],
]));

$table->expandVertically(true);

$collator = new \Collator('ru_RU');

new Column('guest', 'Guest', comparator: static fn (mixed $a, mixed $b): int
    => $collator->compare((string) $a, (string) $b) ?: 0);

$table->sortBy('downloads', SortDirection::Desc);
$table->getSort();  // ['key' => 'downloads', 'direction' => SortDirection::Desc]
$table->clearSort();

$table->setFilter('symfony/');
$table->setFilter(static fn (array $row): bool => $row['downloads'] > 1_000_000);
$table->clearFilter();

$input = new InputWidget()->setPrompt('  Filter: ');

$input
    // Filtering as you type reads better than waiting for Enter.
    ->onChange(function (ChangeEvent $event) use ($table): void {
        $event->isBlank() ? $table->clearFilter() : $table->setFilter($event->getValue());
    })
    ->onSubmit(fn () => $tui->setFocus($table));

$table->onInput(function (string $data) use ($tui, $input): bool {
    if ('/' !== $data) {
        return false;   // let the table handle the key
    }

    $tui->setFocus($input);

    return true;        // and keep '/' out of the table
});

$table
    ->setEmptyText('Пока нет данных')
    ->setNoMatchText('Ничего не найдено');

$tui->addStyleSheet(TableWidget::defaultStyleSheet());

$tui->addStyleSheet(new StyleSheet([
    TableWidget::class.'::selected' => new Style()->withBackground('blue'),
    TableWidget::class.'::header' => new Style()->withBold()->withUnderline(),
]));