Mapping integer to a string in a table column

How do I replace integer values of a column with a corresponding text.

We have a use case where an integer column represents a state, these state can be mapped to a string name. Instead of displaying a state We want to display a text using a map.

Map = {1: 'Created', 2:'Pending', 3:'Done'}
cell value = 1, text to display in column = Created

We tried using transformer and mapper but how do we access the cell value in a transformer. Please help or share any references if any

@alex-w Can you help with this issue ?

Hey there @jainendra-chg! You can access the cell value in a mapper with self. So you could try something like {{ {1: 'Created', 2:'Pending', 3:'Done'}[self] }}

This throws errors. Has anyone solved this?

Hi Scott! Can you share your settings and the error you are getting? self inside of the table column’s mapper setting should evaluate as the current value of that cell, and in this case {{ {1: 'Created', 2:'Pending', 3:'Done'}[1] }} would return a string of ‘Created’

Hello @justin Thanks. I did the same

@scottcrowley I think you might have copy-pasted the code. This could be due the quotes that you used in ‘Created’, ‘Pending’, ‘Done’. the quotes usually gets changed while copy pasting code from web. Please type it afresh and it should work fine.

@justin @alex-w Retool team, I think you guys should include this in you Mapper example in the Retool docs. This will help a lot of new folks

1 Like

Thanks, removing the quotes around the index and leaving them on the ‘Pending’ txt worked.

Appreciate the help

1 Like

Ah darn, yeah it looks like your original values here have opening and closing single quotes rather than straight single quotes. Web typography strikes again!

I still can't get beyond the errors, for something like:

{{ {1: 'Created', 2:'Pending', 3:'Done'}[self] }}

Get:

Hey @stevemushero, it looks like your self value does is not equal to 1, 2, or 3. Basically what this ({{ {1: 'Created', 2:'Pending', 3:'Done'}[self] }}) is doing is keying into the object using the self as the key.

{{ {1: 'Created', 2:'Pending', 3:'Done'}[1] }} ==> 'Created'
{{ {1: 'Created', 2:'Pending', 3:'Done'}[2] }} ==> 'Pending'
{{ {1: 'Created', 2:'Pending', 3:'Done'}[3] }} ==> 'Done'

Make sure your self value returns one of the keys inside the object.

Thanks - values are only 0 and 2, but still errors right in the editor, I think before it even refreshes the data. And does it on the first value, without regard to other values, etc.

Note this has the same error:
{{ {1: 'Created', 2:'Pending', 3:'Done'}[1] }} ==> 'Created'

It sounds like you don't have a {{ self }} value for 1, causing you to get an error. You'll likely need to change the code to {{ {0: 'Created', 2:'Pending'}[self] }} since you mentioned you don't have a 1.

Could you share with us what {{ self }} evaluates to in your app?