+ Added new example about the new feature: custom column
This commit is contained in:
David
2025-03-17 22:33:11 -04:00
parent 096c7944b9
commit 25c6b316bd
14 changed files with 2260 additions and 7 deletions

View File

@@ -0,0 +1,57 @@
<?php
namespace App\Tables\Extensions;
use Closure;
use Mmt\GenericTable\Attributes\MappedRoute;
use Mmt\GenericTable\Components\Column;
use Mmt\GenericTable\Enums\ColumnSettingFlags;
use Mmt\GenericTable\Interfaces\IColumn;
use Mmt\GenericTable\Interfaces\IColumnFormatter;
use Mmt\GenericTable\Interfaces\IColumnRenderer;
use Str;
class IconColumn implements IColumn, IColumnRenderer
{
public int $settings = 0;
public string $bindedRoute = '';
public ?MappedRoute $mappedRoute = null;
public ?string $databaseColumnName = 'status';
public string $columnTitle = 'Status';
private Closure $setIconCallback;
public function __construct()
{
if($this->databaseColumnName == null)
$this->databaseColumnName = Str::snake($this->columnTitle);
}
public function renderCell(\Illuminate\Database\Eloquent\Model $rowModel): string
{
$icon = $this->setIconCallback->call($this, $rowModel);
return <<<HTML
<div class = "w-100 d-flex justify-content-start">
<i class = "$icon me-2"></i>
</div>
HTML;
}
public function route(MappedRoute $route)
{
$this->mappedRoute = $route;
return $this;
}
public function setIconIf(Closure $callback)
{
$this->setIconCallback = $callback;
return $this;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Tables;
use App\Models\Product;
use App\Tables\Extensions\IconColumn;
use Illuminate\Database\Eloquent\Model;
use Mmt\GenericTable\Components\Column;
use Mmt\GenericTable\Components\ColumnCollection;
use Mmt\GenericTable\Interfaces\IGenericTable;
class TableWithCustomColumn implements IGenericTable
{
public Model|string $model;
public ColumnCollection $columns;
public function __construct()
{
$this->model = new Product();
$this->columns = new ColumnCollection();
$icons = require_once('Extensions/bootstrap_icons.php');
$this->columns->add(new Column('Id'));
$this->columns->add(new Column('Name'));
$this->columns->add(new Column('Description'));
$this->columns->add(new IconColumn()->setIconIf(function(Model $rowModel) use($icons) {
if($rowModel->status == 'discontinued') {
return $icons['bag-check'] . ' text-success';
}
else {
return $icons['bag-x'] . ' text-danger';
}
}));
}
}