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
6 Replies
In this case, I added a Emoji to the top 3 rows. Here's my code:
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.
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
@Zoltar glad you did it!
Solution
I solved it definitively with a ViewColumn and a custom view
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
]);
}
osedyo has been warned, this is their first warning.