Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow table column classes to be callable #477

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions app/app/Http/Controllers/TableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion resources/views/table/body.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
])
>
<div class="flex flex-row items-center @if($column->alignment == 'right') justify-end @elseif($column->alignment == 'center') justify-center @else justify-start @endif">
@isset(${'spladeTableCell' . $column->keyHash()})
Expand Down
2 changes: 1 addition & 1 deletion resources/views/table/head.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@foreach($table->columns() as $column)
<th
v-show="table.columnIsVisible(@js($column->key))"
class="@if($loop->first && $hasBulkActions) pr-6 @else px-6 @endif py-3 text-left text-xs font-medium tracking-wide text-gray-500 {{ $column->classes }}"
class="@if($loop->first && $hasBulkActions) pr-6 @else px-6 @endif py-3 text-left text-xs font-medium tracking-wide text-gray-500 {{ $column->resolveClasses() }}"
>
@if($column->sortable)
<a @click.exact.prevent="table.navigate(@js($sortByUrl = $sortBy($column)))" dusk="sort-{{ $column->key }}" href="{{ $sortByUrl }}">
Expand Down
34 changes: 29 additions & 5 deletions src/Table/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Table/HasColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down