I get a set of id - name pairs from a select statement. The result is in the inject node in the attached nodes. I would like to have a dropdown where the dropdown shows the name and when a name is selected the value shall be the id.
My sample code that obviously is wrong is:
var my_array = ;
var index = 0;
for (var item in msg.payload ) {
var local_label = msg.payload[item].name;
var value = msg.payload[item].fan_id;
my_array[index] = local_label;
index++;
}
msg.options = my_array;
return msg;
If I instead modify the code to create a label:value pair all the labels in the dropdown shows "local_label".
Here is my modified example:
var my_array = ;
var index = 0;
for (var item in msg.payload ) {
var local_label = msg.payload[item].name;
var value = msg.payload[item].fan_id;
my_array[index] = {local_label:value};
index++;
}
msg.options = my_array;
return msg;
What am I doing wrong?
Here is the node tree:
[
{
"id": "d7533a0c.e42048",
"type": "ui_dropdown",
"z": "f7584b9d.ca91f8",
"name": "",
"label": "my dropdown",
"tooltip": "",
"place": "Select option",
"group": "e182c702.116e28",
"order": 3,
"width": 0,
"height": 0,
"passthru": true,
"multiple": false,
"options": [
{
"label": "",
"value": "",
"type": "str"
}
],
"payload": "",
"topic": "",
"x": 580,
"y": 320,
"wires": [
[]
]
},
{
"id": "e88b6b3c.fa2c88",
"type": "debug",
"z": "f7584b9d.ca91f8",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 610,
"y": 500,
"wires": []
},
{
"id": "a9bc7eec.5ece7",
"type": "function",
"z": "f7584b9d.ca91f8",
"name": "Create dropdown with label:value pairs",
"func": "var my_array = [];\nvar index = 0;\nfor (var item in msg.payload ) {\n var local_label = msg.payload[item].name;\n var value = msg.payload[item].fan_id;\n my_array[index] = {local_label:value};\n index++;\n}\n\nmsg.options = my_array;\n\nreturn msg;\n",
"outputs": 1,
"noerr": 0,
"x": 330,
"y": 400,
"wires": [
[
"d7533a0c.e42048",
"e88b6b3c.fa2c88"
]
]
},
{
"id": "c0e3146a.1d9f18",
"type": "inject",
"z": "f7584b9d.ca91f8",
"name": "",
"topic": "",
"payload": "[{\"fan_id\":0,\"name\":\"Output 1\"},{\"fan_id\":1,\"name\":\"Output 2\"},{\"fan_id\":2,\"name\":\"Output 3\"},{\"fan_id\":3,\"name\":\"Output 4\"}]",
"payloadType": "json",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 170,
"y": 500,
"wires": [
[
"a9bc7eec.5ece7"
]
]
},
{
"id": "e182c702.116e28",
"type": "ui_group",
"z": "",
"name": "Setup",
"tab": "73a12294.36614c",
"order": 1,
"disp": true,
"width": "12",
"collapse": false
},
{
"id": "73a12294.36614c",
"type": "ui_tab",
"z": "",
"name": "Setup",
"icon": "dashboard",
"order": 1,
"disabled": false,
"hidden": false
}
]