Dynamic list in node configuration

I wrote a custom node that generates a JSON payload. I would like to have the node configuration have an 'extra tags' list. The first version I wrote lets the user specify these extra tags as a json object:

{ "extraTag": "extraValue"}

but what I really want is to have it look like the injector node does, with a list of keys and values, and an [+add] button.

I'm not exactly sure how to do this, short of doing what Inject does - build it all up manually using jquery. Is there an easier way to make a list like this, or does it really necessitate custom everything? Are there other examples of nodes that have this sort of thing?

Kind of answering my own question here, but after lookign for more examples I found this:

https://nodered.org/docs/api/ui/editableList/

which is used by two internal nodes: Change and Switch. That plus another example I found (not on my native language, but still understandable) got me, I think, on my way. One thing the German example had was it created a 'template' for the row. I think I'll do that as well.

HI @wz2b

Welcome to the forums.

There many many nodes that use the editable list (I am sure)
I am one of them.

Creating the list element
https://github.com/zwave-js/node-red-contrib-zwave-js/blob/ea6aab997fe192ffed390b68c556c7a5b05d0ccc/zwave-js/event-filter.html#L47

Populating the list from serialized data
https://github.com/zwave-js/node-red-contrib-zwave-js/blob/ea6aab997fe192ffed390b68c556c7a5b05d0ccc/zwave-js/event-filter.html#L62

This is called when clicking the add button and when populating data
https://github.com/zwave-js/node-red-contrib-zwave-js/blob/ea6aab997fe192ffed390b68c556c7a5b05d0ccc/zwave-js/event-filter.html#L96

Its also documented here.
https://nodered.org/docs/api/ui/editableList/

EDIT:
Oh you found the docs :laughing:

I did. It took me a little while because i wasn't sure of the name of the thing I was looking for. Thanks for pointing me to some other examples. I have this mostly working:

  • It creates an empty list on startup
  • You can click the 'add' button and add rows
  • When I run the node, it does the right thing (the rows end up in the output payload)

What I don't have working yet is populating the UI list from the stored node configuration. I don't think I understand the semantics of addItem. It gets called (apparently) when one pushes the "add" button. Does this also get called with [data] = whatever the config field is set to when it starts? or does onprepareedit have to do that?

Actually I re-read what you said and you already answered the question: it happens in both cases. I just thought in the case where it's getting initially populated (I from the node def config data) that I would have seen node.something to pull it out of the config. Maybe I defined the variables wrong. In html registration I did this:

tags: {value: [], required: false}

In my node type definitino (which is typescript) I did this:

{ ... tags: Array<Array<string>> ...}

in other words, it's an array of arrays of strings like:

[ [ 'a', 'b'], ['c', 'd' ]] ]

I think I have it. Your zwave example was an immense help. Thank you for pointing me to it.

I noticed what you were doing in prepare was creating the list, then re-selecting it. One of the things I was doing wrong was assuming .editableList actually returned the list, so I then called eList('addItem') as if it was a function. Apparently that's not the case. I've lived my whole world in react and angular and just have no experience with jQuery, so this probably looks like a n00b mistake.

Glad your up and running.

You can see the the items being loaded on the switch node here.
https://github.com/node-red/node-red/blob/5293563a6a5903106a84920043f5948ab205c1c3/packages/node_modules/%40node-red/nodes/core/function/10-switch.html#L413

You can add your items in bulk also in oneditprepare, but not sure if you then get an array in the addItem callback or its still triggered for each item.

addItems
https://nodered.org/docs/api/ui/editableList/#methods-addItems

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