How To Bind OnClick To State Of A Checkbox

Hello :slight_smile:

I’m creating a simple todo list using a table.
The table is fetching a collection from Xano. The result is sorted in the frontend on WeWeb.
One of the columns in the table is a checkbox element. The init value of the checkbox is binded to a property of an item from the collection.
On clicking a checkbox, a workflow is triggered where the updated value is sent to my Xano backend and then I fetch the collection again. All of this works perfectly well till here.

Challenge I’m facing:
When the checkbox is clicked, there is no immediate visual feedback to the user that the checkbox was clicked. If the fetch collection did not have sort applied, then it’s not an issue. But becuase there is a sort that is applied, the entire row abruptly gets pushed to the bottom. Without a visual feedback to the user that the checkbox was clicked, it is a very jarring experience.

Things I’ve tried:

  1. I’ve tried adding time delays to the workflow between sending updated data to Xano backend and fetching collection. Did not work.
  2. I’ve watched the YT tutorial on using custom states. I tried using the method of applying the variable and then updating the variable on click in the workflow. Since my checkbox are linked to the collection, whenever any one checkbox is checked, it changes the value of all the checkboxes.

To put it very simply, this is the flow I’d like:

  1. User clicks on any checkbox
  2. An immediate visual feedback is shown (nothing fancy, just confirmation that the checkbox got checked)
  3. Send the data to the backend to update the database using workflow
  4. Fetch the collection

I’m getting steps 1,3 and 4 working correctly. How do I get step number 2?

Below is a loom of the problem. Hope it helps.
Many thanks in advance!

Pynapple | WeWeb - 19 September 2023 - Watch Video

Or is it possible to add sort to a collection as a separate action in a workflow?

I also tried adding a custom javascript action in the workflow to sort the fetched collection. While running test, it showed that the sort worked. However in preview it did not work.

I would need to see how you bind and sort your data, but it should definitely work with the delay

Try adding a delay (+Add Workflow Action → Delay) before fetch collection. This way

  1. The checkbox will get checked
  2. Collection gets updated

and after like 300ms it will go down (get sorted) after the update of a collection. Then just experiment with the amount of delay you’re giving it.

Tried that. Delay doesn’t work. Here’s why:

The “checked” state of the checkbox is binded to the item property of the collection. It shows as checked only if a item property has a value true. When the user clicks a checkbox, it sends the update to the backend, but since the “checked” state gets updated only on frontend update, it needs to fetch collection to show the updated state. So adding any amount of delay does not help in this case.

Is there any other way to change the state of the checkbox before fetching collection?

You could sort it via a function instead of on the load of the data, but it’s a little more complicated.

But what you could try is, add a delay. And on click, actually do item.checked = true via custom javascript right after the click. This way it will force the change. This will reset after the collection fetches again after the delay.

Here’s how my data is binded and sorted. Hope this helps

On the left panel, todolist is bound to my “todo” collection, which is sorted

Below is the checkbox init value binding

Below is the state of the checkbox and it’s binding condition

Try this: [after xano set todo completed], add custom javascript, NOT formula containing this

[Item.data.complete] = true

Then add your delay before the fetch collection (after the javascript). This now should force the state to checked and then flush it away with the new data from fetch collection and sort it.

You could probably also set it to true with change variable action, but with JS it’s guaranteed to be forced to change.

I tried this approach but I think I’m getting the JS code wrong. I tested the custom JS action in the workflow builder and it showed succeed, but the frontend still does not behave the expected way. Possible to point out what the mistake is? Screenshot below

Also tried it in this order

Dont do ==
And without return

Do just [item.data.complete]=true
Or you can try this with change variable too

If I do that, then the custom js action shows error. Screenshots below.
I’m not able to use a variable here because the checkboxes load as per the collection. And because the collection is dynamic, it’s not creating any checkbox variables for me to apply a change variable value in the workflow.

The item data checked you need to do from your context. The purple one :grin:

If I manully create a variable and assign the bind condition of the checkbox state to that variable, and then change the variable value in the workflow, then if I click on one checkbox, all the checkboxes get changed at once. There is no unique variable assigned to each checkbox since (i assume) the checkboxes are binded to the collection

That’s what I did in the second screenshot. In the custom js action, I clicked on the purple “complete” and it generated this

context.item.data?.['complete']

I simply added the =true after that.

Am I missing something? :cry:

Check this out

It seems to work for me like this
in your case it would be like this: context.item.data.complete I thought it was simpler, sorry for wrong answers before (I was on the phone and unable to try it out myself). Like this it should work.


Yup, this way the action results in no bug. But the front end still doesn’t show the expected behaviour. It still shows teh checked state of the checkbox after the fetch collection.

My current flow:

On Change → Xano request to update backend → Custom JS action → Time delay → Fetch collection

Hey there :wave:

Did you find the solution @mriganka?

If not, you might want to jump in the next Office Hours on Tuesday at 12pm EST.

@Quentin and @flo can take a look at your project and work it out with you.

Hey @Joyce

Haven’t found a fix for this yet. Office hours sounds like a good idea. I’ll try to join in. Thanks for letting me know :slight_smile:

1 Like