Ui_dropdown: set msg.options for multiple entries

Hi..
I am using ui_dropdown node. I am able to set msg.options in change node and populated in dropdown for single entry but when I try to inject multiple entries, it is repopulating or refreshing the menu. How to populate in dropdown menu in dashboard. In the flow, there are different msg.topic's, I need to split and should remove duplicates if any. I have only problem with msg.options to dropdown, if multiple entries given continuously.
For that not to loose values in arrays, I have created global arrays. which inject once and whenever function node executes, those values will be stored.

here is the flow..

[{"id":"ae72b941.c54948","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"cfcfc88f.6c3888","type":"inject","z":"ae72b941.c54948","name":"Initialization","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"0.5","topic":"","payload":"","payloadType":"date","x":130,"y":380,"wires":[["bc6d90a5.5df89"]]},{"id":"bc6d90a5.5df89","type":"function","z":"ae72b941.c54948","name":"","func":"const domainSet = []; //msg.topic[0]\nconst nodeSet = [];  //msg.topic[1];\n\nglobal.set(\"domain_Arr\", domainSet);\nglobal.set(\"node_Arr\", nodeSet);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":380,"wires":[[]]},{"id":"8f95d1f8.dfca8","type":"inject","z":"ae72b941.c54948","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"2","crontab":"","once":false,"onceDelay":0.1,"topic":"test1/Node1/2/Data","payload":"{\"ID\":\"13dfsl48\",\"TimeStamp\":0,\"SensorNamwe\":\"Sensor_3\",\"SensorRaw\":-332,\"SensorCal\":-234}","payloadType":"json","x":130,"y":480,"wires":[["8e7e2f4d.29d91"]]},{"id":"a7b7c128.f77b9","type":"inject","z":"ae72b941.c54948","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"4","crontab":"","once":false,"onceDelay":0.1,"topic":"test2/Node2/3/Data","payload":"{\"ID\":\"2e3fffs\",\"TimeStamp\":0,\"SensorNamwe\":\"Sensor_5\",\"SensorRaw\":-197,\"SensorCal\":-165}","payloadType":"json","x":130,"y":540,"wires":[["8e7e2f4d.29d91"]]},{"id":"8e7e2f4d.29d91","type":"json","z":"ae72b941.c54948","name":"","property":"payload","action":"obj","pretty":true,"x":290,"y":520,"wires":[["e3552595.566938","de237166.3276b"]]},{"id":"e3552595.566938","type":"function","z":"ae72b941.c54948","name":"","func":"\nvar NodeArray = global.get(\"node_Arr\");\n\nvar Topic = msg.topic.split(\"/\");\nvar Node = Topic[1];\nNodeArray.push(Node);\n\n//Removing duplicates in Array\nlet y = (NodeArray) => NodeArray.filter((v,i) => NodeArray.indexOf(v) === i)\nconst unQNodeArr = y(NodeArray);\n\n\nmsg.options = unQNodeArr;\n// return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":540,"wires":[["d5568bfb.00b7c8"]]},{"id":"de237166.3276b","type":"function","z":"ae72b941.c54948","name":"","func":"\nvar DomainArray = global.get(\"domain_Arr\");\nlet Topic = msg.topic.split(\"/\");\n\nvar Domain = Topic[0];\nDomainArray.push(Domain);\n\n//Removing duplicates in Array\nlet x = (DomainArray) => DomainArray.filter((v,i) => DomainArray.indexOf(v) === i)\nconst unQDomainArr = x(DomainArray);\n\nmsg.options = unQDomainArr;\n// return msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":500,"wires":[["f61ee996.db28e8"]]},{"id":"f61ee996.db28e8","type":"change","z":"ae72b941.c54948","name":"","rules":[{"t":"set","p":"options","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":500,"wires":[["af33f919.47e1c8"]]},{"id":"d5568bfb.00b7c8","type":"change","z":"ae72b941.c54948","name":"","rules":[{"t":"set","p":"options","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":540,"wires":[["996903c6.7f2f7"]]},{"id":"af33f919.47e1c8","type":"ui_dropdown","z":"ae72b941.c54948","name":"","label":"Select the Domain","tooltip":"","place":"Select option","group":"f4ddc713.cb9a58","order":2,"width":"6","height":1,"passthru":true,"multiple":false,"options":[],"payload":"","topic":"","x":830,"y":500,"wires":[[]]},{"id":"996903c6.7f2f7","type":"ui_dropdown","z":"ae72b941.c54948","name":"","label":"Select the Node","tooltip":"","place":"Select option","group":"f4ddc713.cb9a58","order":1,"width":"6","height":1,"passthru":true,"multiple":false,"options":[],"payload":"","topic":"","x":820,"y":540,"wires":[[]]},{"id":"c729fc1e.8194c","type":"inject","z":"ae72b941.c54948","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"6","crontab":"","once":false,"onceDelay":0.1,"topic":"test3/Node3/4/Data","payload":"{\"ID\":\"3fg4rks\",\"TimeStamp\":0,\"SensorNamwe\":\"Sensor_5\",\"SensorRaw\":-197,\"SensorCal\":-165}","payloadType":"json","x":130,"y":600,"wires":[["8e7e2f4d.29d91"]]},{"id":"f4ddc713.cb9a58","type":"ui_group","name":"T1","tab":"763667c5.ae1af8","order":1,"disp":true,"width":"6","collapse":false},{"id":"763667c5.ae1af8","type":"ui_tab","name":"LAB","icon":"dashboard"}]```

Your function is not returning the msg. so no option set. Then your change node overwrites msg.option with msg.payload.

But to only pass on changed msg.options compare the array lengths. e.g.

[{"id":"f454e332.520a5","type":"inject","z":"63af3844.ce5a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test1/Node1/2/Data","payload":"{\"ID\":\"13dfsl48\",\"TimeStamp\":0,\"SensorNamwe\":\"Sensor_3\",\"SensorRaw\":-332,\"SensorCal\":-234}","payloadType":"json","x":120,"y":480,"wires":[["e114e419.a101c8"]]},{"id":"e114e419.a101c8","type":"json","z":"63af3844.ce5a8","name":"","property":"payload","action":"obj","pretty":true,"x":290,"y":520,"wires":[["855b6a36.104f88","b1a7676e.dbe78"]]},{"id":"855b6a36.104f88","type":"function","z":"63af3844.ce5a8","name":"","func":"\nvar NodeArray = global.get(\"node_Arr\");\nlet NodeLength = NodeArray.length;\n\nvar Topic = msg.topic.split(\"/\");\nvar Node = Topic[1];\nNodeArray.push(Node);\n\n//Removing duplicates in Array\nlet y = (NodeArray) => NodeArray.filter((v,i) => NodeArray.indexOf(v) === i)\nconst unQNodeArr = y(NodeArray);\n\nif (unQNodeArr.length >  NodeLength){\n    msg.options = unQNodeArr;\n    return msg;\n}else{\n    return;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":430,"y":540,"wires":[["4d2e065.e237e78","b14b4ae5.4f17d8"]]},{"id":"b1a7676e.dbe78","type":"function","z":"63af3844.ce5a8","name":"","func":"\nvar DomainArray = global.get(\"domain_Arr\");\nlet DomainLength = DomainArray.length;\nlet Topic = msg.topic.split(\"/\");\n\nvar Domain = Topic[0];\nDomainArray.push(Domain);\n\n//Removing duplicates in Array\nlet x = (DomainArray) => DomainArray.filter((v,i) => DomainArray.indexOf(v) === i)\nconst unQDomainArr = x(DomainArray);\nif (unQDomainArr.length >  DomainLength){\n    msg.options = unQDomainArr;\n    return msg;\n}else{\n    return;\n}\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":430,"y":500,"wires":[["4d2e065.e237e78","2948e3a9.45edd4"]]},{"id":"7efe7e3e.a2ec2","type":"inject","z":"63af3844.ce5a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test2/Node2/3/Data","payload":"{\"ID\":\"2e3fffs\",\"TimeStamp\":0,\"SensorNamwe\":\"Sensor_5\",\"SensorRaw\":-197,\"SensorCal\":-165}","payloadType":"json","x":120,"y":540,"wires":[["e114e419.a101c8"]]},{"id":"fa85e99.3871e98","type":"inject","z":"63af3844.ce5a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test3/Node3/4/Data","payload":"{\"ID\":\"3fg4rks\",\"TimeStamp\":0,\"SensorNamwe\":\"Sensor_5\",\"SensorRaw\":-197,\"SensorCal\":-165}","payloadType":"json","x":120,"y":600,"wires":[["e114e419.a101c8"]]},{"id":"4d2e065.e237e78","type":"debug","z":"63af3844.ce5a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":630,"y":520,"wires":[]},{"id":"b14b4ae5.4f17d8","type":"ui_dropdown","z":"63af3844.ce5a8","name":"","label":"","tooltip":"","place":"Select option","group":"165e1e1a.e752fa","order":6,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"","x":660,"y":580,"wires":[[]]},{"id":"2948e3a9.45edd4","type":"ui_dropdown","z":"63af3844.ce5a8","name":"","label":"","tooltip":"","place":"Select option","group":"165e1e1a.e752fa","order":6,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"","x":610,"y":460,"wires":[[]]},{"id":"165e1e1a.e752fa","type":"ui_group","z":"","name":"Default","tab":"f9b58883.fa613","order":1,"disp":true,"width":"12","collapse":false},{"id":"f9b58883.fa613","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Thank you. The flow you posted worked. But when I write return msg, it is overwriting msg.options eariler. Thats why no options populating in drowpdown menu.

Your code throws a 'TypeError: Cannot read property 'length' of undefined' in both function nodes if the globals do not exist. Changing the first line adding || [] for example:
var DomainArray = global.get("domain_Arr") || [];
will prevent this error.

Yes it would, the op has a inject to initialise the global. My example was to show how to compare the arrays and only pass on a changed option array. My flow also does not show global.set() either.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.