To pythonians.
This is the hardest for me - cause it involve recursive
Consider the navigation tree (perspective tree component):
How do I build this programmatically, given a table from database:
(is the table structured even right?)
In other words, how do I programmatically walk thru the table above to create the code below.
[
{
"label": "Main",
"items": [
{
"label": "Prod A",
"items": [
{
"label": "Line 1",
"items": []
},
{
"label": "Line 2",
"items": []
}
]
},
{
"label": "Prod B",
"items": [
{
"label": "Line 3",
"expanded": false,
"items": []
},
{
"label": "Line 4",
"items": []
}
]
}
]
}
]
I started with the below function:
def getItems(<some query here>):
itemObj={}
itemObj['label'] = 'USA'
itemObj['items'] = []
return itemObj
I can't think how to start my for loop
@Matthew.gaitan helped me solve a similar problem. In my application I used a Tree component as a navigation tree. The tree was populated by a text file very similar to the database data you've shown there. It worked out really well and the text file made editing the site menu a very simple task using a text field within the application (with appropriate user rights). I documented my final code so I hope it will be of use to you.
I'm experimenting with using the Tree component for navigation rather than the Menu Tree. It offers the user clear visibility of where in the site they are whereas the Menu Tree doesn't.
I'm making the tree configurable within the application (subject to user roles) and want to use a simple markup structure to generate the Tree's props.items.
Markup would look like this with the syntax anchor text | page URL. The URL can be passed to the tree's onActionPerformed event to cause a navigation to …
4 Likes
Hi Transistor, thank you for this, no doubt this is the solution,
but I do not follow on:
Create a source of the menu structure in text.
Create an Expression Binding on the Tree's props.items
to your text.
It is blur for me,
How do you put the "structured text" on the expression field on the binding?
Sorry, bedtime in Ireland. I'll look at it tomorrow.
I got it.
I literally, copied the code below.
The input is a column from dataset with `completePath'.
Work like a charm.
Thank you for replying to my post quick.
I've been using a modified version of this:
I'm using this code in a script transform. Original binding is to a dataset containing path data. ex: "A/B/C"
items = []
for row in range(value.rowCount):
path = value.getValueAt(row, 0)
current = items
for part in path.split("/"):
# Check if the current folder exists in our items list
folderExists = False
for itemsPointerItem in current:
if part == itemsPointerItem['label']:
folderExists = True
current = itemsPointerItem['ite…