F
Filament2mo ago
F alko

Format state on empty column

I have table with width, height and length parameters. When showing this in the table, I want to add a size column That shows width x height x length.
Tables\Columns\TextColumn::make('width')
->label('Size')
->formatStateUsing(fn($record) => sprintf(
'%s x %s x %s mm',
$record->width ?? '?',
$record->height ?? '?',
$record->length ?? '?',
),
),
Tables\Columns\TextColumn::make('width')
->label('Size')
->formatStateUsing(fn($record) => sprintf(
'%s x %s x %s mm',
$record->width ?? '?',
$record->height ?? '?',
$record->length ?? '?',
),
),
However, when one is null, I want to show ? instead. When width is null, it doesn't hit the formatStateUsing method. Is there a way to make a virtual column that is always resolved?
3 Replies
F alko
F alkoOP2mo ago
Ohhh, I can use the default like this:
Tables\Columns\TextColumn::make('width')
->label('Size')
->default(fn($record) => sprintf(
'%s x %s x %s mm',
$record->width ?? '?',
$record->height ?? '?',
$record->length ?? '?',
))
->formatStateUsing(fn($record) => sprintf(
'%s x %s x %s mm',
$record->width ?? '?',
$record->height ?? '?',
$record->length ?? '?',
))
Tables\Columns\TextColumn::make('width')
->label('Size')
->default(fn($record) => sprintf(
'%s x %s x %s mm',
$record->width ?? '?',
$record->height ?? '?',
$record->length ?? '?',
))
->formatStateUsing(fn($record) => sprintf(
'%s x %s x %s mm',
$record->width ?? '?',
$record->height ?? '?',
$record->length ?? '?',
))
LeandroFerreira
LeandroFerreira2mo ago
Or ->state() instead of formatStateUsing
F alko
F alkoOP2mo ago
Nice! That's even cleaner.
Tables\Columns\TextColumn::make('width')
->label('Size')
->state(fn($record) => sprintf(
'%s x %s x %s mm',
$record->width ?? '?',
$record->height ?? '?',
$record->length ?? '?',
)),
Tables\Columns\TextColumn::make('width')
->label('Size')
->state(fn($record) => sprintf(
'%s x %s x %s mm',
$record->width ?? '?',
$record->height ?? '?',
$record->length ?? '?',
)),
Want results from more Discord servers?
Add your server