F
Filament9mo ago
Basti

Adding an additional item to the grouping

Hello folks! I'm currently trying out this grouping feature in Filament 3 and I've just got a specific question. I have pages that are grouped by a parentPages relation. To be clear: In my case, pages can get a parent page. The grouping works great, but I would like to have the parent page listed in this grouping as well and not individually in a different place of the table. How do I trick the grouping so that the parent page is also included in the grouping? Any idea? I did not find anything in the documentation. Or I have missed it 🫤 //Edit: An example in the screenshot: I would like to be able to find "Third Page" also in the grouping named "Third Page" and not way above the grouping as it is in the screenshot.
No description
Solution:
```php ->prefix(function (Page $record, Table $table): ?Htmlable { if (! static::shouldShowParent($table) && $record->parent) { return new HtmlString('<span style="margin-inline-end: 0.75rem;">&boxur;</span>');...
Jump to solution
16 Replies
awcodes
awcodes9mo ago
Are you trying to do something like how Wordpress displays it’s pages?
Basti
Basti9mo ago
Well, the last time I saw the Wordpress Backend was 3 years ago. What exactly do you mean? :D
Basti
Basti9mo ago
Oh wait, I know what you might mean. That "tree structure" from Wordpress, right? That's more or less what I wanted it to look like
No description
awcodes
awcodes9mo ago
Yea. There was a good help thread about this. No real solution though. Be cause it falls apart with filters/searching. Was trying to find it for you but the discord search on mobile is complete crap. I did manage to get something that works similar to how Wordpress is doing it. But it was posted in that thread I can’t find right now.
Solution
awcodes
awcodes9mo ago
->prefix(function (Page $record, Table $table): ?Htmlable {
if (! static::shouldShowParent($table) && $record->parent) {
return new HtmlString('<span style="margin-inline-end: 0.75rem;">&boxur;</span>');
}

return null;
})
->description(function (Page $record, Table $table): ?Htmlable {
if (static::shouldShowParent($table) && $record->parent) {
return new HtmlString('<span>Parent: ' . Str::of($record->parent)->title() . '</span>');
}

return null;
})
->prefix(function (Page $record, Table $table): ?Htmlable {
if (! static::shouldShowParent($table) && $record->parent) {
return new HtmlString('<span style="margin-inline-end: 0.75rem;">&boxur;</span>');
}

return null;
})
->description(function (Page $record, Table $table): ?Htmlable {
if (static::shouldShowParent($table) && $record->parent) {
return new HtmlString('<span>Parent: ' . Str::of($record->parent)->title() . '</span>');
}

return null;
})
awcodes
awcodes9mo ago
public static function shouldShowParent(Table $table): bool {
$isSearching = $table->hasSearch();
$isSorting = $table->getSortColumn();
$hasActiveFilters = collect($table->getFilters())
->filter(fn (\Filament\Tables\Filters\BaseFilter $filter) => $filter->getIndicators())
->isNotEmpty();

return $isSearching || $isSorting || $hasActiveFilters;
}
public static function shouldShowParent(Table $table): bool {
$isSearching = $table->hasSearch();
$isSorting = $table->getSortColumn();
$hasActiveFilters = collect($table->getFilters())
->filter(fn (\Filament\Tables\Filters\BaseFilter $filter) => $filter->getIndicators())
->isNotEmpty();

return $isSearching || $isSorting || $hasActiveFilters;
}
First block goes on the column
Basti
Basti9mo ago
This looks like it only affects the title column and not a grouping, right? I would give it a try and it would be an option to me
awcodes
awcodes9mo ago
Yea. It won’t work with grouping, that was discussed in that thread. I can’t think of a way to show the grouped parent since they are the same type of record. And that’s how it’s being grouped is by a parent id.
Basti
Basti9mo ago
That worked for me, thanks a lot! :) But I guess this is from v2? Because ->prefix() can't output Html for me. Only strings. Or did i miss something?
awcodes
awcodes9mo ago
It’s v3 The new HtmlString converts it to a sting Make sure you imported Htmlable and HtmlString
Basti
Basti9mo ago
Mh okay thats weird. It converts to a string but the output is not formatted as html. Everything seems right to me
No description
No description
awcodes
awcodes9mo ago
Weird.
Basti
Basti9mo ago
Oh my mistake. I should have used ->html() in the column xD
awcodes
awcodes9mo ago
Makes sense. Sorry. Mine is on BadgeableColumn from a plug-in that is probably handling that by default. Glad it works for you though.
Basti
Basti9mo ago
Nevermind. But thanks anyway for your time and help. Have a great night :)
awcodes
awcodes9mo ago
You too.