From 289a15612b1fc4fcb1229e5a5b1ad315d69d8e3a Mon Sep 17 00:00:00 2001 From: James Johnson Date: Mon, 10 Jul 2023 07:55:27 -0500 Subject: [PATCH 1/3] allow table column classes to be callable for customizing class based on column data --- resources/views/table/body.blade.php | 2 +- src/Table/Column.php | 12 +++++++----- src/Table/HasColumns.php | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/resources/views/table/body.blade.php b/resources/views/table/body.blade.php index 97667b3a..940dca9a 100644 --- a/resources/views/table/body.blade.php +++ b/resources/views/table/body.blade.php @@ -29,7 +29,7 @@ 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 @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 @php echo is_callable($column->classes) ? call_user_func($column->classes, $getColumnDataFromItem($item, $column), $item) : $column->classes; @endphp" >
@isset(${'spladeTableCell' . $column->keyHash()}) diff --git a/src/Table/Column.php b/src/Table/Column.php index 4777950b..e74ac87b 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|null $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); + } } /** diff --git a/src/Table/HasColumns.php b/src/Table/HasColumns.php index 4524b1a9..cee38de9 100644 --- a/src/Table/HasColumns.php +++ b/src/Table/HasColumns.php @@ -55,7 +55,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|null $as = null, string $alignment = 'left', ): self { From 4f34f8e371da43f00608c1ed9dab3a7f94f9f967 Mon Sep 17 00:00:00 2001 From: Pascal Baljet Date: Wed, 20 Mar 2024 17:58:34 +0100 Subject: [PATCH 2/3] Refactor --- app/app/Http/Controllers/TableController.php | 17 ++++++++----- resources/views/table/body.blade.php | 10 +++++++- resources/views/table/head.blade.php | 2 +- src/Table/Column.php | 25 ++++++++++++++++++-- 4 files changed, 44 insertions(+), 10 deletions(-) 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 940dca9a..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 @php echo is_callable($column->classes) ? call_user_func($column->classes, $getColumnDataFromItem($item, $column), $item) : $column->classes; @endphp" + @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 646c0123..6a1c98ba 100644 --- a/src/Table/Column.php +++ b/src/Table/Column.php @@ -7,7 +7,6 @@ use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Str; -use Orchestra\Workbench\Recipes\AssetPublishCommand; class Column implements Arrayable { @@ -29,7 +28,7 @@ public function __construct( public Closure|string|null $exportFormat = null, public Closure|array|null $exportStyling = null, public Closure|array|string|null $classes = null, - public Closure|null $as = null, + public ?Closure $as = null, public string $alignment = 'left', ) { if (!is_callable($classes)) { @@ -82,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 From fe0a50831131edeafce53554ab1fa3cb2bbffddd Mon Sep 17 00:00:00 2001 From: pascalbaljet Date: Wed, 20 Mar 2024 16:59:09 +0000 Subject: [PATCH 3/3] Fix styling --- src/Table/HasColumns.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Table/HasColumns.php b/src/Table/HasColumns.php index c37da37b..827a7b47 100644 --- a/src/Table/HasColumns.php +++ b/src/Table/HasColumns.php @@ -55,7 +55,7 @@ public function column( callable|string|null $exportFormat = null, callable|array|null $exportStyling = null, callable|array|string|null $classes = null, - callable|null $as = null, + ?callable $as = null, string $alignment = 'left', ): self { $key = $key !== null ? $key : Str::kebab($label);