Jungs versucht, eine dynamische Menüliste zu erstellen. Es gibt keine Begrenzung für die Tiefe der Elemente, die von einem Benutzer erstellt werden können.Javascript leitet einen Baum aus einer Reihe von URLs
MEIN PROBLEM
sieht Mein Satz von URL wie diese
var json_data = [
{
"title" : "Food",
"path" : "/root",
},
{
"title" : "Cloths",
"path" : "/root",
},
{
"title" : "Veg",
"path" : "/root/Food",
},
{
"title" : "Brinjal",
"path" : "/root/Food/Veg",
},
{
"title" : "T shirt",
"path" : "/root/Cloths",
},
{
"title" : "Shirt",
"path" : "/root/Cloths",
},
{
"title" : "Green brinjal",
"path" : "/root/Food/Veg/Brinjal",
}
];
möchte ich die titles
in einer Hierarchie-Format sein, die schließlich machen werde ich von ul > li
in Form des Menüs erstellen - vor -Ende.
Die Hierarchie sollte wie folgt sein:
[
{
"title": "Food",
"path": "/root",
"children": [
{
"title": "Veg",
"path": "/root/Food",
"children": [
{
"title": "Brinjal",
"path": "/root/Food/Veg",
"children": [
{
"title": "Green brinjal",
"path": "/root/Food/Veg/Brinjal",
"children": []
}
]
}
]
}
]
},
{
"title": "Cloths",
"path": "/root",
"children": [
{
"title": "T shirt",
"path": "/root/Cloths",
"children": []
},
{
"title": "Shirt",
"path": "/root/Cloths",
"children": []
}
]
}
]
WAS habe ich versucht,
// Add an item node in the tree, at the right position
function addToTree(node, treeNodes) {
// Check if the item node should inserted in a subnode
for (var i=0; i<treeNodes.length; i++) {
var treeNode = treeNodes[i];
// "/store/travel".indexOf('/store/')
if (node.path.indexOf(treeNode.path + '/') == 0) {
addToTree(node, treeNode.children);
// Item node was added, we can quit
return;
}
}
// Item node was not added to a subnode, so it's a sibling of these treeNodes
treeNodes.push({
title: node.title,
path: node.path,
children: []
});
}
//Create the item tree starting from menuItems
function createTree(nodes){
var tree = [];
for (var i=0; i<nodes.length; i++) {
var node = nodes[i];
addToTree(node, tree);
}
return tree;
}
// variable = "json_data" is the set of URLS
var menuItemsTree = createTree(json_data);
console.log(menuItemsTree)
Und das Ergebnis es ergibt, ist dies:
[
{
"title": "Food",
"path": "/root",
"children": [
{
"title": "Veg",
"path": "/root/Food",
"children": [
{
"title": "Brinjal",
"path": "/root/Food/Veg",
"children": [
{
"title": "Green brinjal",
"path": "/root/Food/Veg/Brinjal",
"children": []
}
]
}
]
},
{
"title": "T shirt",
"path": "/root/Cloths",
"children": []
},
{
"title": "Shirt",
"path": "/root/Cloths",
"children": []
}
]
},
{
"title": "Cloths",
"path": "/root",
"children": []
}
]
Die Elemente "T Shirt"
und "Shirt"
ist geschoben in die Food's Kinder: [
Here aus genommen wird []
Die Lösung, die ich verwendet habe ich versucht, einen Algorithmus abzuleiten mich selber, aber es ist nicht genau das Richtige noch angeklickt:], die diese sollten innerhalb Cloth Kinder sein. Bitte helfen Sie, wenn jemand bereits eine Lösung hat. Falls ich selbst eine Lösung finden kann, werde ich es hier teilen. obigen Code ist Dank
bedaure, aber mit Ihrer Lösung der „Stoff“ selbst ist im Inneren des „Food“ und „Veg“ gehen immer „Food der“ Geschwister –
@SiddharthaChowdhury Oh, ich bin Es tut uns leid. Ich habe nicht getestet .. Ich werde zu dir zurückkommen .. –
Sure Reddy bitte –