F
Filamentβ€’2mo ago
Zoltar

Table ImageColumn only for certain rows

I'm creating a leaderboard table and would like the first 3 rows to have an image instead of text position (1,2,3). Is it possible? Any idea? thnks
Solution:
I solved it definitively with a ViewColumn and a custom view
Jump to solution
6 Replies
Diogo Pinto
Diogo Pintoβ€’2mo ago
In this case, I added a Emoji to the top 3 rows. Here's my code:
Tables\Columns\TextColumn::make('title')
->formatStateUsing(function ($state, $record, $rowLoop) {
if ($rowLoop->iteration <= 3) {
return "πŸ† {$state}";
}
return $state;
})
Tables\Columns\TextColumn::make('title')
->formatStateUsing(function ($state, $record, $rowLoop) {
if ($rowLoop->iteration <= 3) {
return "πŸ† {$state}";
}
return $state;
})
Problem: if you have pagination, all the top 3 records will show this emoji. Also, if allow sorting, it won't work too. You can make the same thing happen and use it according to your logic. Instead of relying in $rowLoop, you can query the user to check if it's in the leaderboard (create a function in your model to check the position in leaderboard, for example) and use the same formatStateUsing.
No description
Zoltar
ZoltarOPβ€’2mo ago
Ok thnks, I solved the problem using view in mysql with DENSE_RANK() OVER (PARTITION BY... Table use custom query with this view and instead of using "$rowLoop->iteration <= 3" use $record->position
Diogo Pinto
Diogo Pintoβ€’2mo ago
@Zoltar glad you did it!
Solution
Zoltar
Zoltarβ€’2mo ago
I solved it definitively with a ViewColumn and a custom view
fenerli
fenerliβ€’2mo ago
Yes, you can do it. Here's my code. code e.g: use Filament\Tables; use Filament\Resources\Table; public static function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('position') ->label('Position') ->formatStateUsing(fn ($state) => $state <= 3 ? view('components.leaderboard-position', ['position' => $state]) : $state), Tables\Columns\TextColumn::make('name') ->label('Name'), Tables\Columns\TextColumn::make('score') ->label('Score'), ]) ->actions([ // Your actions here ]); }
Carl-bot
Carl-botβ€’2mo ago
osedyo has been warned, this is their first warning.
Want results from more Discord servers?
Add your server