How can I get dynamic references with $index to work in dmx-html?

I have ten fields in a database 'add_costs_1', 'add_costs_2', 'add_costs_3' to 'add_costs_10'

I am using them in a repeat and so want to reference them dynamically

If I reference them manually ie with '.add_costs_1' at the end, like this

dmx-html="$index == 0 ? '<i class=\'fa-ban fas\'></i>' : sc_ad_add_costs_mq.data.qp_ad_add_costs_mq[add_costs_id-1].add_costs_1"

then the results are as expected (but only reference the first field repeatedly, many times).

image

If I try to reference them dynamically then I cannot get a result. So I have tried things like

dmx-html="$index == 0 ? '<i class=\'fa-ban fas\'></i>' : add_costs_$index"
dmx-html="$index == 0 ? '<i class=\'fa-ban fas\'></i>' : add_costs_{{$index}}"
dmx-html="$index == 0 ? '<i class=\'fa-ban fas\'></i>' : add_costs_[$index]"
dmx-html="$index == 0 ? '<i class=\'fa-ban fas\'></i>' : 'add_costs_' + $index"

but nothing I do seems to work in the 'dmx-html'.

I know I don’t need to put

dmx-html="$index == 0 ? '<i class=\'fa-ban fas\'></i>' : sc_ad_add_costs_mq.data.qp_ad_add_costs_mq[add_costs_id-1].add_costs_1"

and I can just use

dmx-html="$index == 0 ? '<i class=\'fa-ban fas\'></i>' : add_costs_1"

But I’m still stuck on how to make that last digit dynamic.

The only way out is custom JS, if I understand this correctly.

Create a custom formatter which will return the value using dmx.parse.
In dmx.parse, you can append the last digit part via JS.

Understand that but not sure how I would do it.
I’ve hard coded it 10 times for now as I needed to get something to show my client in the morning.
I’d much rather do it dynamically, with a repeat and as you suggest.

Would it make sense to normalize the data and have an add_costs table rather than having numbered fields?

Hi Ken, I did think of that when setting this up, wish I’d gone down that route because this is horrible at the moment.

Sid suggested an option above but I think I’ll cut my losses and create a separate table.
It’ll be cleaner, easier to manage, and I have three or four more sections to do in a similar fashion.

Thanks Ken for the bash on the head :crazy_face:

1 Like

Ya, it’s a dead give away when you start numbering db columns. :slightly_smiling_face: Good luck!

2 Likes

It’s not a big issue really :smile: