F
Filament14mo 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
awcodes14mo ago
Are you trying to do something like how Wordpress displays it’s pages?
Basti
BastiOP14mo ago
Well, the last time I saw the Wordpress Backend was 3 years ago. What exactly do you mean? :D
Basti
BastiOP14mo 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
awcodes14mo 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
awcodes14mo 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
awcodes14mo 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
BastiOP14mo 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
awcodes14mo 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
BastiOP14mo 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
awcodes14mo ago
It’s v3 The new HtmlString converts it to a sting Make sure you imported Htmlable and HtmlString
Basti
BastiOP14mo 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
awcodes14mo ago
Weird.
Basti
BastiOP14mo ago
Oh my mistake. I should have used ->html() in the column xD
awcodes
awcodes14mo 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
BastiOP14mo ago
Nevermind. But thanks anyway for your time and help. Have a great night :)
awcodes
awcodes14mo ago
You too.
Want results from more Discord servers?
Add your server