Refactor table components to use GenericTableSettings and remove deprecated classes
This commit is contained in:
@@ -3,48 +3,56 @@
|
||||
namespace App\Tables\Traits;
|
||||
|
||||
use App\Models\Product;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Mmt\GenericTable\Attributes\CellFormatter;
|
||||
use Mmt\GenericTable\Attributes\MappedColumn;
|
||||
use Mmt\GenericTable\Components\Column;
|
||||
use Mmt\GenericTable\Components\ColumnCollection;
|
||||
use Mmt\GenericTable\Enums\ColumnSettingFlags;
|
||||
use Mmt\GenericTable\Enums\PaginationRack;
|
||||
use Mmt\GenericTable\Interfaces\ICellData;
|
||||
use Mmt\GenericTable\Interfaces\IRowData;
|
||||
use Mmt\GenericTable\Support\GenericTableSettings;
|
||||
|
||||
trait WithColumnFormatter
|
||||
{
|
||||
public Model|string $model = Product::class;
|
||||
public GenericTableSettings $tableSettings;
|
||||
|
||||
public ColumnCollection $columns;
|
||||
public int $paginationRack = 0;
|
||||
|
||||
public int $rowsPerPage = 10;
|
||||
|
||||
public array $rowsPerPageOptions = [10,20,40,60,80];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->columns = ColumnCollection::make(
|
||||
$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')->withSettings(ColumnSettingFlags::EMPTY),
|
||||
new Column('Empty Hook')->withSettings(ColumnSettingFlags::EMPTY)->hookFormatter(fn($rowModel) => $this->emptyCol($rowModel)),
|
||||
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($model)
|
||||
public function emptyCol(IRowData $row)
|
||||
{
|
||||
return '<i class = "text-danger">'.$model->id.'</i>';
|
||||
return '<i class = "text-danger">'.$row->get('id').'</i>';
|
||||
}
|
||||
|
||||
#[CellFormatter('price')]
|
||||
public function priceFormatter(Model $modelItem)
|
||||
public function priceFormatter(ICellData $cell, IRowData $row)
|
||||
{
|
||||
return "<b class = 'text-primary'>\$</b> {$modelItem->price}";
|
||||
return "<b class = 'text-primary'>\$</b> {$cell->value}";
|
||||
}
|
||||
|
||||
#[CellFormatter('id')]
|
||||
public function idFormatter(Model $modelItem)
|
||||
public function idFormatter(ICellData $cell, IRowData $row)
|
||||
{
|
||||
return '<b class = "text-primary">#</b> '.$modelItem->id;
|
||||
return '<b class = "text-primary">#</b> '.$cell->value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user