Files
generic_table_examples/app/Tables/TableWithExport.php

57 lines
1.7 KiB
PHP

<?php
namespace App\Tables;
use App\Models\Product;
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\GenericTableSettings;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class TableWithExport implements IGenericTable, IExportable
{
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(Cell $cell)
{
return '$'.$cell->value;
}
#[CellFormatter('name')]
public function nameFormatter(Cell $cell)
{
return '<i>La</i> ' . $cell->value;
}
}