Sum rows in table with edited cells

I have a table where a column of percentage cells are editable. I want to make sure the user cannot save changes unless all of the rows add up to 100% (including the edited cell). The problem I'm having is that table.displayedData does not use the edited cell value, but uses the original value before the user changed it.

Hey dan1989! Thanks for reaching out about this. Any changes made to a table's editable cells are stored in table.recordUpdates.

Unfortunately, it's not a direct mapping of table row index to recordUpdate row index. Do your rows have a unique identifier like an id?

1 Like

Hey @dan1989!

I made an example on how you can do logic like this using @joeBumbaca 's record updates and a temporary state. There are probably some better ways to do this too.

So basically what you could do is map and add all values from that row together for each object inside of the recordUpdates array inside of a temporary state array. Then create a boolean expression:
{{table1.recordUpdates.map(d=>parseFloat(d.id) + parseFloat(d.sales) == 60)}}

The temprorary state gains a false value unless the ID and sales add up to 60. Then set the disable save changes to false for when this temporary state includes false

{{_.includes(state2.value, false)}}

If I wanted to do this for a single column I could change up my syntax a bit:

Temporary State: {{_.sum(table1.recordUpdates.map(d=>parseFloat(d.id))) == 50}} - This tests if all IDS add up to 50

Disable save: {{state2.value == false}} - we can just set this as a boolean expression since we no longer have an array

Hope that helps!

1 Like

Thanks everyone for your responses. I was actually able to accomplish this in just 5 lines of code in a transformer.. I needed to sum edited rows with unedited rows. So, I created a transformer that does just that. Then I can throw an error if the total of all the rows is over 100%. This was possible because displayedData doesn't include recordUpdates changes. So I could compare the two.

const editedRows = {{ objectivesTable.recordUpdates.map(row => parseInt(row.id)) }}
const editedWeight = _.sumBy({{objectivesTable.recordUpdates}}, row => { return parseFloat(row.weight) })
const staticRows = {{ objectivesTable.displayedData}}.filter(row => !editedRows.includes(parseInt(row.id)))
const staticWeight = _.sumBy(staticRows, row => { return row.weight} )
return staticWeight + editedWeight

Hey dan1989, that's awesome! Glad you got it working, and thanks for sharing.