Dynamically build Tree items binding from tag path

I’d like to know how to make a binding that builds the items for a Perspective Tree component automatically. So basically, if I have a tag path that is “Item1/Child1/Grandchild 1”, I can have the items of the Tree Component populate accordingly to make that view. Not sure how to make an “items” binding.

You need to use a script transform to build a list of your items, following the structure of the items prop for nested items.

https://docs.inductiveautomation.com/display/DOC81/Perspective+-+Tree

Note you can create an example structure and copy it, and paste into notepad to get the example json.if you give more specifics of what you’re doing we can help more with some example code

So, a tag browse tree ?
https://docs.inductiveautomation.com/display/DOC81/Perspective+-+Tag+Browse+Tree

I was going to suggest that, but sounded like they wanted only specific tags :person_shrugging: I also haven’t used that yet (but will next week actually)

Ideally, I’d like it to work with the Perspective Tree component as I have a little more control of how I want it to look.

Basically, we have a view that shows additional information about a device. In this case, we show the tag path. I have something working by parsing the tag path and listing each folder that looks like this.

But, I’d prefer to show the tree view look instead. like this.

Where I’m stuck is the syntax of the script to build the json out of the tag folder string.

So how to turn this:
Treatment/3KNG/200/100_01_Basin1_Mixer1

into this:

[
  {
    "label": "Treatment",
    "expanded": true,
    "items": [
      {
        "label": "3KNG",
        "expanded": true,
        "items": [
          {
            "label": 200,
            "expanded": true,
            "items": [
              {
                "label": "100_01_Basin1_Mixer1",
                "expanded": false,
                "items": []
              }
            ]
          }
        ]
      }
    ]
  }
]

Something like this, perhaps?

path = "Treatment/3KNG/200/100_01_Basin1_Mixer1"

parent = []
ret = parent
for part in path.split("/"):
    item = {
        "label": part,
        "expanded": True,
        "items": parent
    }
    parent.append(item)
    parent = []

return ret

I had to make the following changes to get this working:

path = "Treatment/3KNG/200/100_01_Basin1_Mixer1"
	
parent = []
ret = parent
for part in path.split("/")[::-1]:
    item = {
        "label": part,
        "expanded": True,
        "items": parent
    }
    parent = [item]
    ret = parent
	
return ret
1 Like

Something about it doesn’t quite seem to be returning an actual array.

That worked. Thanks guys!

I did one last change by changing “path” to “value” to bring in the actual property itself.