Sum a field across DataGrid selectedRows - 'on click' bug?

I’m having trouble summing a value across the selected rows in a datagrid. I’ve tried a nocode formula and custom JS generated by co-pilot but both options are working incorrectly in the same way. Details in loom. Please help! :joy:

I’ve played with this further and I think this must be a bug. The ‘on click’ that triggers the workflow only seems to be recognized on the second click, i.e. when deselecting the row. Bug?

Hey,
the snippets I’m mentioning in the video are:

checkbox value

return eval(event?.['currentTarget'].querySelector("input").value)

if true

variables[/* grand_total */'7227a980-d953-4178-9d1c-9fbf6e4aecb9'] -= context.item.data?.['val']/2
//e.g variables[your variable here] -= [your collection value]/2

if false

variables[/* grand_total */'7227a980-d953-4178-9d1c-9fbf6e4aecb9'] += context.item.data?.['val']/2
//e.g variables[your variable here] += [your collection value]/2

btw: eval() is just a little evil haha

Reference to the onclick vs onchange:

1 Like

Thanks so much for going to the effort of putting this together @Broberto! I will try it out now and report back. And noted on not relying on the total for charging.

Almost works @Broberto! Except for some reason the true/false split is always evaluating as false, so the amount is always added, never subtracted. Please let me know if I’m doing something wrong. Thanks again!

My code snippets:

true/false:

return eval(event?.['currentTarget'].querySelector("input").value)

true:

variables[/* premiums-total */'76e741d6-cb8f-48c5-8306-7764207c132c'] -= context.item.data?.['premium']
//e.g variables[your variable here] -= [your collection value]/2

false:

variables[/* premiums-total */'76e741d6-cb8f-48c5-8306-7764207c132c'] += context.item.data?.['premium']
//e.g variables[your variable here] += [your collection value]/2

Hey Dorian, I’m really sorry that it does not work :frowning: I’m trying to think about how to help you out, but unless.I could fiddle with it on my own, I don’t think I can debug it remotely. Maybe we could hop on a call?

1 Like

I don’t know why you use eval at all.
The selected rows variable contains the row data, so it’s just a matter of creating an array of values by mapping the selected array and sum its values.
If you use a formula to calculate it dynamically there is no need to use workflows.
In my example the price property is called n, you can get the sum of the selected prices like this:
image
chrome_0kfR5QDf1V

2 Likes

Could you explain the logic behind adding the values to the array? Where do the values in the array you’re mapping and summing come from? e.g how do you generate selectedRows?

Thanks for trying @Broberto!

Worked perfectly thanks @dorilama!

1 Like

selected rows is generated by the grid component.
If you are not using the grid component but use a collection list or repeated items in a container yuo can keep track of selected items by yourself with a workflow like this example

I might see it now, you’re using the datagrid! I now I know something more about the Weweb haha :smiley: Yeah, it is simple from there, I was doing this using the native Checkboxes :smiley: That’s why it was such a pain… My bad lol :smiley: Thanks for showing me

1 Like