Nested records in a table
I want to implement the display of nested records ( nesting hierarchy ) in a table.
The nesting itself will be displayed simply: in the name of each nested (child) element there will be a prefix: " -- "
So far I see only one way to do it:
1) override the getTableQuery method in ListRecords so that the query returns records in the correct order, taking into account nesting
2) Implement in getStateUsing for TextColumn the logic of displaying prefixes of the form: " -- " to display nesting levels
Maybe there is something ready for this?
8 Replies
I think If you do it with formatStateUsing to insert the - -, it’s keeping track of the depth that’s the issue. So you could set a depth option when storing the records and use that to insert that column extra padding on the records?
No problem, just one method:
private static function getNestedPrefix($parent_id, $prefix=' '): string
{
static $parents = null;
if ($parents==null)
$parents = self::$model::all()->pluck('parent_id', 'id');
if ($parent_id==0) return '';
if (isset($parents[$parent_id]) && $parents[$parent_id])
$prefix .= self::getNestedPrefix($parents[$parent_id], $prefix);
return $prefix;
}
and its usage:
return $table
->columns([
Tables\Columns\TextColumn::make('id'),
Tables\Columns\TextColumn::make('parent_id'),
Tables\Columns\TextColumn::make('name')
->getStateUsing(function(FileDocumentCat $record) {
return self::getNestedPrefix($record->id) . ($record->parent_id ? '-- ' : '') . $record->name;
})
->html(),
Great stuff
Thanks, now I'll post the trick on filamentphp.com )
Great stuff!
Filament
Nested records in a table by Leonid - Tricks - Filament
Filament is a collection of tools for rapidly building beautiful TALL stack apps, designed for humans.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View