Store table on group

This commit is contained in:
Ralph J. Smit
2024-12-28 18:52:23 +01:00
parent f531d8723d
commit d66f65ac42
3 changed files with 57 additions and 3 deletions
@@ -0,0 +1,28 @@
<?php
namespace Filament\Tables\Grouping\Concerns;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Table;
trait BelongsToTable
{
protected Table $table;
public function table(Table $table): static
{
$this->table = $table;
return $this;
}
public function getTable(): Table
{
return $this->table;
}
public function getLivewire(): HasTable
{
return $this->getTable()->getLivewire();
}
}
+15 -1
View File
@@ -17,6 +17,8 @@ use Illuminate\Support\Arr;
class Group extends Component
{
use Concerns\BelongsToTable;
protected ?string $column;
protected ?Closure $getDescriptionFromRecordUsing = null;
@@ -33,7 +35,7 @@ class Group extends Component
protected ?Closure $scopeQueryByKeyUsing = null;
protected string | Closure | null $label;
protected string | Closure | null $label = null;
protected string $id;
@@ -471,4 +473,16 @@ class Group extends Component
return $query->with([$relationshipName]);
}
/**
* @return array<mixed>
*/
protected function resolveDefaultClosureDependencyForEvaluationByName(string $parameterName): array
{
return match ($parameterName) {
'livewire' => [$this->getLivewire()],
'table' => [$this->getTable()],
default => parent::resolveDefaultClosureDependencyForEvaluationByName($parameterName),
};
}
}
@@ -72,6 +72,10 @@ trait CanGroupRecords
public function defaultGroup(string | Group | null $group): static
{
if ($group instanceof Group) {
$group->table($this);
}
$this->defaultGroup = $group;
return $this;
@@ -82,6 +86,12 @@ trait CanGroupRecords
*/
public function groups(array | Closure $groups): static
{
foreach ($groups as $group) {
if ($group instanceof Group) {
$group->table($this);
}
}
$this->groups = $groups;
return $this;
@@ -159,7 +169,8 @@ trait CanGroupRecords
return $group;
}
return Group::make($this->defaultGroup);
return Group::make($this->defaultGroup)
->table($this);
}
/**
@@ -171,7 +182,8 @@ trait CanGroupRecords
$this->evaluate($this->groups),
function (array $carry, $group): array {
if (! $group instanceof Group) {
$group = Group::make($group);
$group = Group::make($group)
->table($this);
}
$carry[$group->getId()] = $group;