Updating custom column value in a table component

Hello! I'm fairly new to retool and not very strong in JS :exploding_head:

I have a table component that allows the user to input values in some columns.

The goal is to view a calculated value in a custom column that i created (with a formula: new_custom_column = fee_initial_value * (index_current_value / index_initial_value).

  • I would like for the user to be able to click on a button that will trigger a calculation and show the calculated value in new_custom_column. This is before the data is inserted into the database, its just for the user to be able to view the calculated value.

  • Details:
    I added a button to the canvas and created an event handler to it that triggers a 'run script'. I think I'm missing something because this hasn't worked :frowning:
    See below script:

let updatedData = [...table31.data];

table31.changesetArray.forEach(change => {
let rowIndex = change.index;
Object.keys(change).forEach(key => {
if (key !== "index") {
updatedData[rowIndex][key] = change[key];
}
});
updatedData[rowIndex].currentFeeValue = updatedData[rowIndex].fee_initial_value * (updatedData[rowIndex].sale_index_current_value / updatedData[rowIndex].sale_index_initial_value)

});

table31.setData(updatedData);
console.log("Updated Table Data:", updatedData);
return updatedData;

Thanks!

2 Likes

Hi @gia, and welcome to the forum!

So, table31.data is read only, so that's why table31.setData won't work.

Unfortunately, there is no easy way to achieve what you're trying to do. The only one I can think of is creating a variable which uses table31.data as default value, use this variable as your table's dataSource, and use a js query to change the variable's value using variable1.setIn.

Let me know if you want to give it a shot and I can fetch some posts in the forum that may be able to help you!

2 Likes

thank you @MiguelOrtiz ! very excited to be part of the community!

ahh ok i understand, yes please send them through!

Thanks again!