Odd "jumping" behaviour when using a toggle
Hi there, in the screen shot you'll see a toggle - it's unpredictable, but sometimes when I toggle the item its position with the folder "group" changes - so you click say the 2nd in the list and the toggle visually updates, the DB updates but there is some kind of refresh going on with the rows in a different order to that expected.
Anyone seen this kind of behaviour before?
Solution:Jump to solution
Looks perfect - thanks for helping (and apologies also for not looking more carefully at the defaultSort parameter spec) π
12 Replies
I don't see anything because it doesn't seem to be a video π
Are you ordering them by
updated_at
or similiar?Apols - am not familiar with capturing a quick video - will hone my skills in that department π
However the opening declaration of the table is
return $table
->defaultGroup('folder')
->columns([
and the sorting is
->deferLoading()
->defaultSort('folder', 'filename')
Does this help?What's the toggle doing?
The toggle code is :
ToggleColumn::make('is_indexed')
->label('Index?')
->onColor('info')
->beforeStateUpdated(function ($record, $state) {
// Runs before the state is saved to the database.
})
->afterStateUpdated(function ($record, $state) {
// Runs after the state is saved to the database.
if ($record->record_type == 'FOLDER') {
// want to set all the items within that folder as
// indexable/ not indexable - but should this recurse? No - let's
// leave it that just the items in that folder which are files
// will be updated
// setIndex($folder,$state);
$res = DB::table('image_cat_pending')
->where('folder', '=', $record->folder)
->update(['is_indexed' => $state]);
}
})
It's being used to set an individual row's "is_indexed" (boolean) value - a row representing either a "file" or a "folder" - in the case of the item being toggled being a folder, all of the items within that folder are being set (by the SQL). The odd visuals are being experienced only when toggling individual "file" records.
I did wonder if it might be linked to "grouping" the folders.Really would help to have a video of what you are seeing.
Hi worked out how to record the screen - hopefully attached now. As you'll see, when I toggle the 4th time the order changes. This is unpredictable as well - sometimes the reordering occurs on the first toggle.
I think you probably need to add a second orderBy column because you only order by folder and they all have the same folder so the order is random
I feel a bit stupid now - I have years' of SQL experience and know all about the importance of specifying the order in which you want rows returned... in my defence, I wasn't aware that each update would result in a re-querying of the database.
Although.... the order by is actually already set as folder,filename:
->defaultSort('folder', 'filename')
So is it something else? Or is perhaps the full sort order not being respected on a re-query?in my defence, I wasn't aware that each update would result in a re-querying of the database.That's just how Livewire works. Request to the server, rerender, swap out HTML
->defaultSort('folder', 'filename')
The second param is the sort direction (ASC
/DESC
). But the first param accepts a Closure. I am not sure, but I think you can modify the query:
Thx I'll experiment
Solution
Looks perfect - thanks for helping (and apologies also for not looking more carefully at the defaultSort parameter spec) π
No worries. You're welcome