Received data from a server has varying numbers of slashes in the path structure, like:
abc/abca/abcsd/absc.dat
I am attempting to display this data in a treeview using PrimeNG. I have made some progress:
for (var i = 0; i < this.test.length; i++) {
let regex = /([^\/]+)\/?/g;
let result: RegExpExecArray;
while ((result = regex.exec(this.test[i])) !== null) {
console.log(result[1]);
if (result[1].search(".dat")>0) {
let item = {
"label": result[1],
"data": "Documents Folder",
"icon": "fa-file-text-o"
}
this.tree.push(item)
}
else {
let item = {
"label": result[1],
"data": "Documents Folder",
"expandedIcon": "fa-folder-open",
"collapsedIcon": "fa-folder",
"children": [{
}]
}
this.tree.push(item)
}
}
}
"test" contains the described data. Regular expressions help extract strings between slashes. However, the current code does not accurately represent the folder structure in the tree view.
To achieve a dynamic solution for displaying folders, instead of hardcoding based on slash count, I need something like this:
for 3 slashes:
first item: this.items.push(item)
sec. item : this.items[0].children.push(item)
third. item: this.items[0].children[0].children.push(item)
for x slashes:
???
Looking for suggestions on how to dynamically organize and display the folder structure in the tree view.