Files
generic_table_examples/app/Tables/Traits/WithColumnFormatter.php

58 lines
1.7 KiB
PHP

<?php
namespace App\Tables\Traits;
use App\Models\Product;
use Mmt\GenericTable\Attributes\CellFormatter;
use Mmt\GenericTable\Components\Column;
use Mmt\GenericTable\Components\ColumnCollection;
use Mmt\GenericTable\Enums\PaginationRack;
use Mmt\GenericTable\Interfaces\ICellData;
use Mmt\GenericTable\Interfaces\IRowData;
use Mmt\GenericTable\Support\GenericTableSettings;
trait WithColumnFormatter
{
public GenericTableSettings $tableSettings;
public int $paginationRack = 0;
public int $rowsPerPage = 10;
public array $rowsPerPageOptions = [10,20,40,60,80];
public function __construct()
{
$columns = ColumnCollection::make(
new Column('Id'),
new Column('Name'),
new Column('Description'),
new Column('Price'),
new Column('Stock'),
new Column('SubDepartment', 'subDepartment.name'),
new Column('Empty Column')->empty(),
new Column('Empty Hook')->empty()->hookFormatter(fn(IRowData $row) => $this->emptyCol($row)),
);
$this->tableSettings = new GenericTableSettings(Product::class, $columns);
PaginationRack::addFlags($this->paginationRack, PaginationRack::TOP, PaginationRack::BOTTOM);
}
public function emptyCol(IRowData $row)
{
return '<i class = "text-danger">'.$row->get('id').'</i>';
}
#[CellFormatter('price')]
public function priceFormatter(ICellData $cell, IRowData $row)
{
return "<b class = 'text-primary'>\$</b> {$cell->value}";
}
#[CellFormatter('id')]
public function idFormatter(ICellData $cell, IRowData $row)
{
return '<b class = "text-primary">#</b> '.$cell->value;
}
}