F
Filamentβ€’5mo ago
jjo63

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?
No description
Solution:
Looks perfect - thanks for helping (and apologies also for not looking more carefully at the defaultSort parameter spec) πŸ™‚
Jump to solution
12 Replies
Dennis Koch
Dennis Kochβ€’5mo ago
I don't see anything because it doesn't seem to be a video πŸ˜… Are you ordering them by updated_at or similiar?
jjo63
jjo63OPβ€’5mo ago
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?
Dennis Koch
Dennis Kochβ€’5mo ago
What's the toggle doing?
jjo63
jjo63OPβ€’5mo ago
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.
awcodes
awcodesβ€’5mo ago
Really would help to have a video of what you are seeing.
jjo63
jjo63OPβ€’5mo ago
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.
Dennis Koch
Dennis Kochβ€’5mo ago
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
jjo63
jjo63OPβ€’5mo ago
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?
Dennis Koch
Dennis Kochβ€’5mo ago
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:
->defaultSort(fn ($query, $direction) => $query
->orderBy('folder', $direction)
->orderBy('filename', $direction)
)
->defaultSort(fn ($query, $direction) => $query
->orderBy('folder', $direction)
->orderBy('filename', $direction)
)
jjo63
jjo63OPβ€’5mo ago
Thx I'll experiment
Solution
jjo63
jjo63β€’5mo ago
Looks perfect - thanks for helping (and apologies also for not looking more carefully at the defaultSort parameter spec) πŸ™‚
Dennis Koch
Dennis Kochβ€’5mo ago
No worries. You're welcome
Want results from more Discord servers?
Add your server