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

**URL:** <https://community.weweb.io/t/sum-a-field-across-datagrid-selectedrows-on-click-bug/3124>\
**Category:** Ask us anything\
**Created:** [June 19, 2023, 6:24pm UTC](https://community.weweb.io/t/sum-a-field-across-datagrid-selectedrows-on-click-bug/3124 "2023-06-19T18:24:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dorian](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorian/32/13183_2.png) [@Dorian](https://community.weweb.io/u/Dorian)\
**Post date:** [June 19, 2023, 6:24pm UTC](https://community.weweb.io/t/sum-a-field-across-datagrid-selectedrows-on-click-bug/3124/1 "2023-06-19T18:24:36Z")

</div>

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! 😂

---

<div class="post-metadata">

**Author:** ![Dorian](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorian/32/13183_2.png) [@Dorian](https://community.weweb.io/u/Dorian)\
**Post date:** [June 19, 2023, 9:11pm UTC](https://community.weweb.io/t/sum-a-field-across-datagrid-selectedrows-on-click-bug/3124/2 "2023-06-19T21:11:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [June 19, 2023, 10:00pm UTC](https://community.weweb.io/t/sum-a-field-across-datagrid-selectedrows-on-click-bug/3124/3 "2023-06-19T22:00:07Z")

</div>

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

**checkbox value**

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

```

**if true**

```auto
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**

```auto
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

> **[JavaScript Logic for Checkbox Clicks 👨‍💻](https://www.loom.com/share/5248c126ada1496a9a9825b4691acfc3?sid=ee9ee67b-4f7c-4b23-824f-4e61bc8eac92)**
>
> Hey, I've made a table and you made it the base. In this Loom, I'll be explaining the JavaScript logic for checkbox clicks. We switch to JavaScript and evaluate the string, then get the onClick event and the current target, which is the checkbox. We...

**Reference to the onclick vs onchange:**

> <https://stackoverflow.com/questions/4471401/getting-value-of-html-checkbox-from-onclick-onchange-events>

---

<div class="post-metadata">

**Author:** ![Dorian](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorian/32/13183_2.png) [@Dorian](https://community.weweb.io/u/Dorian)\
**Post date:** [June 20, 2023, 12:10am UTC](https://community.weweb.io/t/sum-a-field-across-datagrid-selectedrows-on-click-bug/3124/4 "2023-06-20T00:10:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Dorian](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorian/32/13183_2.png) [@Dorian](https://community.weweb.io/u/Dorian)\
**Post date:** [June 20, 2023, 12:35am UTC](https://community.weweb.io/t/sum-a-field-across-datagrid-selectedrows-on-click-bug/3124/5 "2023-06-20T00:35:48Z")

</div>

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:

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

```

true:

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

```

false:

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

```

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [June 20, 2023, 6:46am UTC](https://community.weweb.io/t/sum-a-field-across-datagrid-selectedrows-on-click-bug/3124/6 "2023-06-20T06:46:23Z")

</div>

Hey Dorian, I’m really sorry that it does not work ☹ 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?

---

<div class="post-metadata">

**Author:** ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)\
**Post date:** [June 20, 2023, 7:36am UTC](https://community.weweb.io/t/sum-a-field-across-datagrid-selectedrows-on-click-bug/3124/7 "2023-06-20T07:36:44Z")

</div>

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](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/6/6331f71f421b5b209b5a87dc01b751bbc97179c3.png)  
 ![chrome_0kfR5QDf1V](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/0/006c426391a2ef4880782a2bdf321242855d2685.gif)

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [June 20, 2023, 7:42am UTC](https://community.weweb.io/t/sum-a-field-across-datagrid-selectedrows-on-click-bug/3124/8 "2023-06-20T07:42:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Dorian](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorian/32/13183_2.png) [@Dorian](https://community.weweb.io/u/Dorian)\
**Post date:** [June 20, 2023, 7:48am UTC](https://community.weweb.io/t/sum-a-field-across-datagrid-selectedrows-on-click-bug/3124/9 "2023-06-20T07:48:59Z")

</div>

Thanks for trying @Broberto!

---

<div class="post-metadata">

**Author:** ![Dorian](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorian/32/13183_2.png) [@Dorian](https://community.weweb.io/u/Dorian)\
**Post date:** [June 20, 2023, 7:49am UTC](https://community.weweb.io/t/sum-a-field-across-datagrid-selectedrows-on-click-bug/3124/10 "2023-06-20T07:49:22Z")

</div>

Worked perfectly thanks @dorilama!

---

<div class="post-metadata">

**Author:** ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)\
**Post date:** [June 20, 2023, 7:50am UTC](https://community.weweb.io/t/sum-a-field-across-datagrid-selectedrows-on-click-bug/3124/11 "2023-06-20T07:50:27Z")

</div>

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](https://community.weweb.io/t/checkboxes-within-a-collection-list/1379/3)

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [June 20, 2023, 7:50am UTC](https://community.weweb.io/t/sum-a-field-across-datagrid-selectedrows-on-click-bug/3124/12 "2023-06-20T07:50:57Z")

</div>

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