Making coding of a switch with multiple buttons easier

Hello,

I am currently coding a multi-button switch. Each entry is constituted by a character chain (what is displayed on the button) followed by a number (the value sent by the button upon selection). However it is really difficult because the switch's values field doesn't accept that several entries share the same chain even if they have different values. So far the solution has been to insert spaces before and after chains to make them different from others. The problem is I sometimes need 10 buttons with the same display and it's hard to count the spaces in existing chains in order not to repeat the exact same pattern.

Therefore I am wondering whether there is a way to code the entries without any automatic re-ordering. In some thread about a matrix, the recommended solution was to put the values in an array and use the position in the array to retrieve each value:

var indices = [12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3],
    index = indices[$]

Is it possible to achieve something similar when entries have a structure string:number and with some repetitions? I only need the strings to be arbitrary whereas the number sent should only match the button's position in the switch.

var strings = ["a", "b", "1", "a", "b", "G", ...]

How could I code this?

I only need the strings to be arbitrary whereas the number sent should only match the button's position in the switch.

Then use the Array syntax in the switch's `values`: ```js ["a","a",5,"O"] ``` Order will be preserved and only the index will be sent.

It's not possible currently, I think I'll need to implement another syntax for the values property.

Too bad. :cry:

In next release you'll be able to write this in the values property:

{
  "labels": ["a", "b", "b", "c"],
  "values": [0, 1, 2, 3]
}
2 Likes

Looking forward to this release! Thanks a lot @jean-emmanuel ! :star_struck:

I've just tried the new version and I have a strange layout. The following widget should have 20 lines of 10 buttons.

And here's the way the data are displayed in the editing field: each label is on a different line (which is much easier to edit) whereas all values are on a single line.

image

There's most probably a syntax error somewhere in your property, verify it carefully. When there's no syntax error, objects are automatically formatted so that sub arrays are on the same line to limit the field's height, if you absolutely want to have one item per line wrap it in a #{} block:

#{
{
 "labels": [
    "a",
    "b"
  ],
  "values": [
    0,
    1
  ]
}
}
1 Like