diff --git a/app/app/Http/Controllers/TableController.php b/app/app/Http/Controllers/TableController.php index fe53a95f..e8bb4883 100644 --- a/app/app/Http/Controllers/TableController.php +++ b/app/app/Http/Controllers/TableController.php @@ -18,12 +18,17 @@ public function as(bool $spladeQueryBuilder = false) $resource = $spladeQueryBuilder ? $query : $query->paginate(10); $table = SpladeTable::for($resource) - ->column('name') - ->column('email', as: function ($email, $user) { - if ($email === $user->email) { - return strrev($email); - } - }); + ->column('name', classes: 'font-bold') + ->column( + 'email', + classes: function ($data = null, $item = null) { + return $data ? 'font-bold' : 'italic'; + }, + as: function ($email, $user) { + if ($email === $user->email) { + return strrev($email); + } + }); if ($spladeQueryBuilder) { $table->paginate(10); diff --git a/resources/views/table/body.blade.php b/resources/views/table/body.blade.php index 97667b3a..4ca4d91d 100644 --- a/resources/views/table/body.blade.php +++ b/resources/views/table/body.blade.php @@ -29,7 +29,15 @@ class="rounded border-gray-300 text-indigo-600 shadow-sm focus:border-indigo-300 @click="(event) => table.visit(@js($table->rowLinks->get($itemKey)), @js($table->rowLinkType), event)" @endif v-show="table.columnIsVisible(@js($column->key))" - class="whitespace-nowrap text-sm @if($loop->first && $hasBulkActions) pr-6 @else px-6 @endif py-4 @if($column->highlight) text-gray-900 font-medium @else text-gray-500 @endif @if($table->rowLinks->has($itemKey)) cursor-pointer @endif {{ $column->classes }}" + @class([ + 'whitespace-nowrap text-sm py-4' => true, + $column->resolveClasses($item) => true, + 'pr-6' => $loop->first && $hasBulkActions, + 'px-6' => !($loop->first && $hasBulkActions), + 'text-gray-900 font-medium' => $column->highlight, + 'text-gray-500' => !$column->highlight, + 'cursor-pointer' => $table->rowLinks->has($itemKey), + ]) >
@isset(${'spladeTableCell' . $column->keyHash()}) diff --git a/resources/views/table/head.blade.php b/resources/views/table/head.blade.php index cfc230d3..2ab3005c 100644 --- a/resources/views/table/head.blade.php +++ b/resources/views/table/head.blade.php @@ -9,7 +9,7 @@ @foreach($table->columns() as $column) @if($column->sortable) diff --git a/src/Table/Column.php b/src/Table/Column.php index 6c6d9818..6a1c98ba 100644 --- a/src/Table/Column.php +++ b/src/Table/Column.php @@ -27,15 +27,17 @@ public function __construct( public bool|Closure $exportAs, public Closure|string|null $exportFormat = null, public Closure|array|null $exportStyling = null, - public array|string|null $classes = null, + public Closure|array|string|null $classes = null, public ?Closure $as = null, public string $alignment = 'left', ) { - if (is_array($classes)) { - $classes = Arr::flatten($classes); - } + if (!is_callable($classes)) { + if (is_array($classes)) { + $classes = Arr::flatten($classes); + } - $this->classes = Arr::toCssClasses($classes); + $this->classes = Arr::toCssClasses($classes); + } } /** @@ -79,6 +81,28 @@ public function toArray() ]; } + /** + * Resolve the column classes for the given item. + */ + public function resolveClasses(mixed $item = null): string + { + if (is_callable($this->classes)) { + $classes = call_user_func( + $this->classes, + $item ? $this->getDataFromItem($item) : null, + $item, + ) ?? ''; + + if (is_array($classes)) { + $classes = Arr::flatten($classes); + } + + return Arr::toCssClasses($classes); + } + + return $this->classes; + } + /** * It gets thet data from the given item, based on the column * and whether that column is based on a relationship diff --git a/src/Table/HasColumns.php b/src/Table/HasColumns.php index d5889db7..827a7b47 100644 --- a/src/Table/HasColumns.php +++ b/src/Table/HasColumns.php @@ -54,7 +54,7 @@ public function column( bool|callable $exportAs = true, callable|string|null $exportFormat = null, callable|array|null $exportStyling = null, - array|string|null $classes = null, + callable|array|string|null $classes = null, ?callable $as = null, string $alignment = 'left', ): self {