Refactor table components to use GenericTableSettings and remove deprecated classes

This commit is contained in:
David
2025-04-25 01:19:30 -04:00
parent 7466083798
commit 9f12bc28d0
17 changed files with 177 additions and 203 deletions

View File

@@ -3,32 +3,55 @@
namespace App\Tables;
use App\Models\Product;
use App\Tables\Traits\WithExportableProductColumns;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Response;
use Mmt\GenericTable\Attributes\CellFormatter;
use Mmt\GenericTable\Components\Cell;
use Mmt\GenericTable\Components\Column;
use Mmt\GenericTable\Components\ColumnCollection;
use Mmt\GenericTable\Enums\ColumnSettingFlags;
use Mmt\GenericTable\Interfaces\IExportable;
use Mmt\GenericTable\Interfaces\IGenericTable;
use Mmt\GenericTable\Support\ExportEventArgs;
use Mmt\GenericTable\Support\ExportSettings;
use Mmt\GenericTable\Support\GenericTableSettings;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class TableWithExport implements IGenericTable, IExportable
{
use WithExportableProductColumns;
public GenericTableSettings $tableSettings;
public function __construct()
{
$columns = ColumnCollection::make(
new Column('Name')->withSettings(ColumnSettingFlags::EXPORTABLE),
new Column('Description')->withSettings(ColumnSettingFlags::EXPORTABLE),
new Column('Price')->withSettings(ColumnSettingFlags::EXPORTABLE),
new Column('Stock')->withSettings(ColumnSettingFlags::EXPORTABLE)
);
$this->tableSettings = new GenericTableSettings(
Product::class,
$columns
);
}
public function onExport(ExportEventArgs $args) : BinaryFileResponse|Response
{
$args->query->take(5);
$args->settings->useFormatters = true;
$args->settings->fileName = 'products';
$args->useStripTags = true;
return $args->export();
}
#[CellFormatter('price')]
public function priceFormatter($rowModel)
public function priceFormatter(Cell $cell)
{
return '$'.$rowModel->price;
return '$'.$cell->value;
}
#[CellFormatter('name')]
public function nameFormatter(Cell $cell)
{
return '<i>La</i> ' . $cell->value;
}
}