Cannot save changes to a JSON table

I am new to Retool and I am facing some problems. I created a table from JSON using a variable (eg, variableTableJSON). Now I want to save changes a specific column which is editable. I created Add on -> Save action -> Run script and wrote the following codes there. But it it doesn't work. Can anyone please help me? Thanks in advance.

{{ variableTableJSON.value = [...variableTableJSON.value,...table1.changesetArray] }}
table1.refresh()

Rgds.

Sudip

2 Likes

Welcome to the Forum Sudip! :wave:

1 Like

welcome. You should use setIn or setValue instead of = to set the value of variable. and {{ }} in the script block is not need.

variableTableJSON.setValue ([...variableTableJSON.value,...table1.changesetArray]);
table1.refresh();

here is the doc of variable The Temporary State object | Retool Docs

1 Like

Thanks a lot!!! I shall try and inform you.

Rgds

Sorry, changes in the table isn't saved. Moreover table1.refresh(); reset the table. I commented this line. But still it's not saved.

Thanks for your help :wink:

Sudip

Hello, more detail? You read the data in the query right? and save it to variableTableJSON, then refer it with {{variableTableJSON.value}} in table data source?

I think you are write your first app.
here is a docs for your refer

you also can export app to json to us to check, thanks.

or maybe this topic is helpful for you.

Ok, thank you. BTW, my latest codes added new records.
Trying more ...
:wink:

1 Like

yes, you can try it with retool database

Finally I am able to solve this problem with following codes:

let tempData = variableTableJSON.value

table1.changesetArray.forEach(changedRow => {
tempData = tempData.map(tempRow => {
    if (changedRow.id === tempRow.id) {
      return {
        id: tempRow.id,
        field_type: tempRow.field_type,
        replace_formula: changedRow.replace_formula
      }
    } else {
      return tempRow
    }
  })
})

variableTableJSON.setValue(tempData)

table1.refresh();

Thanks! :wink:

3 Likes