Update table cell's values

I have a table which displays data retrieved from an Postgres database. The cells of the table are editable (except the last record where i wish to calculate cell values client side as is described below) .
I was wondering, if it is possible to programmatically update (client side) the value of some non editable cells. Those values are calculated based on the values of some other editable cells. So every time an editable cell is updated, the value of the non editable cell need to be updated as well. The value of those programmatically updated cells will not be updated to the database. It is only for viewing purposes.

Thanks in advance.

@fmihos I think it will depend on how the table is built - if the table is populated with data from a query/resource (from a db for example) then no, this cannot be done without running another query/resource/process (prob have to use a temp state that stores the table data initially)
If the table is built with data on the client side only, you should be able to do this.

Unfortunately, the table is populated with data from db. Therefore, i cannot avoid another trip to the database.
Thanks anyway Scott.

Hey @fmihos!

You might also look into using custom columns here - they're a great way to display additional read-only data in your table. When you pass an inline transformer to the column you can reference values from that row using the currentRow variable as well as any other values in your app model by their usual names! That means you can also pass more complicated standalone transformers and the like as well.

Could that work for your use case?

Both custom columns and transformers seems to be inappropriate for my case.
My scenario is (for a specific column):
If the user edits cells from 1 to N-1...then the value of the Nth cell (last row) must be updated to the SUM of cells 1 to N-1.

Ahh I see, we don't currently support summary rows but it is something that's on our roadmap. We can let you know here as soon as it is supported. In the meantime, you might try building your own summary row out of separate components.

To get a copy of your table data that includes edits it can be useful to referenceyourTable.changeSet. This is an object with all of the edits to your table indexed by their respective row indices and column.

With your data formatted as an array, you can use the following JavaScript to add any existing edits to your table:

const modifiedTableData = tableData.map((row, index) => Object.assign(row, changeSet[index]));

From there, you can sum over each column as well with something like

const legs = modifiedTableData.map(row => row.number_of_legs);
const totalLegs = _.sum(legs);

Note that if you're using the above code you'll first have to declare both tableData and changeSet with appropriate references to the model!

Does that work?

@Kabirdas would the value of the custom columns update automatically if user edits any of the referenced cells ? If so, where can we find these (in which object) ?

You can do something similar in a custom column to reference any updated changes, in the new table that might look something like the following in the value field of your custom column:

{{ table1.changesetObject[i]?.text ?? currentSourceRow.text }}

In the legacy table you'd use:

{{ table1.changeSet[i]?.text ?? currentRow.text }}
1 Like

@Kabirdas could I somehow use this to update the value of one cell base on the value in another cell. eg. I have tabel with product_name in one celle and want to autoupdate a new cell with a product_id from a table where product_name and product_id is referrenced ?

Setting the value in the frontend mapper won't automatically register it as a change that needs to be saved to the database. If that's what you're looking to do I'd recommend adding the expression in the update query you're using.

For instance, if you have a SQL bulk upsert where you're referencing {{ table1.changesetArray }} you could instead do something like {{ table1.changesetArray.map(row => Object.assign(row, {calculated_column: /* some expression using your source columns */})) }}

Let me know if that works! Otherwise, if you're willing to share a screenshot or so of the query you're using to update your database I'd be happy to provide a more specific example!

In case it hasn't reached anyone in this thread yet - summary rows were introduced with row aggregations in the new table :tada:

2 Likes