Building a complex countdown table column
I'm trying to build a countdown into a table column. I'm currently using
ViewColumn
to do this with some alpine code to handle updating the remaining time. The table is displaying records from a ReverseAuction
table. The ReverseAuction
model has an attribute to calculate what time that auction will end because users in the system can take actions which would change the end time. I call this attribute true_ends_at
. I have the table polling every 10 seconds to check for updates made by other users. If I output the true_ends_at
timestamp in a table column, I see that this is correctly updating when other users affect the ending time.
The problem is that the countdown column will not adjust to the correct time. The countdown just continues from whatever the state of the column was when the page was first loaded.17 Replies
Here is the column in the table:
The view file that is being passed in:
And the store that is defined on the page:
i believe the solution could be this https://alpinejs.dev/magics/watch
using x-init you could pass
on the same div as x-data
it would watch changes in the getState and recalculate everything
Ok, I think I see where you are going but I want to clarify a few things. I'm not using
x-init
so I'll put it in init()
instead. Shouldn't this be $watch('endTime', value => ....)
instead of $watch('{{ $getState()->toISOString() }}', value => ....)
?
Yeah, it seems that the key would have to match a property in x-data
. This watch is not being triggered however, even though the state is changing. I think this is because when the table polls, Alpine is reinitialized. If I add console.log('init')
to the beginning of the method, I do see that when the table polls and finds new data
Trying to simplify this to narrow down the issue...
After changing to the following code, I see that init()
is still running when the table polls and finds new data for the true_ends_at
column. I can also verify that remaining
is changing but the display does not update until the following poll before it updates.
So at the moment the sequence is like this:
- true_ends_at changes
- table polls and new data is printed to console but display does not update
- table polls again and there is NOT new data but the display updates with the new data from the previous poll
Just a random thought, maybe try entangling the end time $getState()
How exactly would I do that?
endTime: $wire.$entangle('{{ $getState() }}')
throws errors because it isn't found on the component. How would I specify a field from the record that is associated with that table row?getStatePath()
I know that I can use
$getRecord()->true_ends_at
or $getState()
but I need to entangle the property name
Ok, let me try that
I get an undefined variable errorUndefined for what exactly?
Is
$getStatePath()
available on a ViewColumn
?Maybe that’s only a form thing
Sorry, I’m probably leading you down a wrong path. Do you have a repo you could share?
No, sorry. It is a private repo. I could upload the view I'm using for the column and the custom page. Would that work for you?
The
countdown-column.blade.php
that I just uploaded is stripped down from what it need to be in its final state. It should ultimately be using the countdown store to trigger an update of the countdown every second but I pulled some of that out to try to figure out what is going onSorry, not ignoring you, just had a medical thing happen. Hoping to get back to you soon.
No worries. Praying for a speedy recovery!