Numeric TextColumn: sortable() not working properly

Hi, I've faced this issue: sortable() method change randomly the order of the column with numeric field. It doesn't even give an "alphabetical" order on numbers. it was simply random (or in any case not a recognizable order, I couldn't find a pattern...) I found a solution passing sortable as in the code below, but I wonder if it's a known behavior and it's what you guys thought, maybe someone has some insights? return TextColumn::make('priority') ->label('') ->html() ->state('●') ->sortable(query: function(Builder $query, string $direction): Builder { $query->getQuery()->orders = []; $query->orderBy('priority', $direction); return $query; }) ->size(TextColumnSize::Large) ->color(fn (Order $record): string => match (self::determinePriority($record->priority)) { 'high' => 'danger', 'medium' => 'warning', 'low' => 'info', default => 'gray', }); FYI: I tried adding numeric(), removing html(), removing state() showing the numbers to try understanding the pattern and it didn't work...
11 Replies
Wrax
Wrax4mo ago
is priority a numeric type in the db schema?
micraux
micraux4mo ago
Are you setting a default sort?
$table
->defaultSort('priority')
$table
->defaultSort('priority')
hentiru
hentiru4mo ago
No, I don't want the default sort to be by priority. It's when I click the sort arrow that I face the issue (without the fix I posted) yes it's a tinyInt
Wrax
Wrax4mo ago
FYI I tried to replicate this in my app but cannot reproduce the described behaviour. My test was in the context of a RelationManager $table on a field using tinyint unsigned in the db.
return $table
->recordUrl(fn (Dog $record): string => EditDog::getUrl(['record' => $record]))
//->reorderable('litter_position')
->heading('Registered Whelps')
//->recordTitleAttribute('full_name')
// ->defaultSort('litter_position')

->columns([
TextColumn::make('litter_position')
->label('litter pos')
->html()
->state('●')
->sortable(),
return $table
->recordUrl(fn (Dog $record): string => EditDog::getUrl(['record' => $record]))
//->reorderable('litter_position')
->heading('Registered Whelps')
//->recordTitleAttribute('full_name')
// ->defaultSort('litter_position')

->columns([
TextColumn::make('litter_position')
->label('litter pos')
->html()
->state('●')
->sortable(),
In my test, the table loads with no defaultSort() applied to the table so the UI shows an inactive state (greyed out down arrow) to indicate no active sorting on the sortable() column. Clicking the column sort once changes to the up arrow in an active state on the column UI and correctly sorts by Asc value. Second click changes column UI to active down arrow and correctly sorts by Desc values. A third click cycles the column sorting back to the initial inactive state with no sorting applied and inactive UI sort state.
Dennis Koch
Dennis Koch4mo ago
This look super hacky. Why are you using a closure and resetting the sorts in there? Looks like standard sort behavior just way more complicated. Also please read #✅┊rules on how to format code on Discord
hentiru
hentiru4mo ago
I know that it should be the standard sort behavior, simply the standard behavior is not working. I don't know if there's something wrong in my code since @Wrax said that he was not able to replicate... 🤔 Sorry for the code formatting 🙏
Dennis Koch
Dennis Koch4mo ago
So it’s not sorting correctly? What’s the actual content? I see you are using html()
hentiru
hentiru4mo ago
Without my "hack" it's not sorting correctly. the content is an integer (i tried to remove html() but the result is still wrong) in the picture you can see the 'asc' ('desc' is similar) ordering without html so you see the actual state... what it surprises me is that it doesn't even put the same numbers close one another..
No description
Dennis Koch
Dennis Koch4mo ago
Can you share the whole table definition?
hentiru
hentiru4mo ago
It's a bit complex, since the columns are defined in a trait extended by the resource, and there are different tables bound to that resource with the columns defined in the trait.. tell me how can I be of help in providing information
Dennis Koch
Dennis Koch4mo ago
Did you overwrite the table query? Or getEloquentQuery()? Maybe install Laravel Debugbar and check the actual DB query.
Want results from more Discord servers?
Add your server