I'm trying to implement tree view search using JavaScript. I found search filter in treeview useful, but the answer provided there wasn't the expected output in my scenario.
Example input:
[
{
"id": 1,
"templateName": "Item 1",
"isFolder": "true",
"children": [
{
"id": 2,
"templateName": "Subitem 1",
"isFolder": "true",
"children": [
{
"id": 3,
"templateName": "Misc 1",
"isFolder": "true",
"children": [
{
"id": 4,
"templateName": "Misc 2"
}
]
}
]
},
{
"id": 5,
"templateName": "Subitem 2",
"isFolder": "true",
"children": [
{
"id": 6,
"templateName": "Misc 3"
}
]
}
]
},
{
"id": 7,
"templateName": "Item 2",
"isFolder": "true",
"children": [
{
"id": 8,
"templateName": "Subitem 1",
"isFolder": "true",
"children": [
{
"id": 9,
"templateName": "Misc 1"
}
]
},
{
"id": 10,
"templateName": "Subitem 8",
"isFolder": "true",
"children": [
{
"id": 11,
"templateName": "Misc 4"
},
{
"id": 12,
"templateName": "Misc 5"
},
{
"id": 13,
"templateName": "Misc 6",
"isFolder": "true",
"children": [
{
"id": 14,
"templateName": "Misc 7"
}
]
}
]
}
]
}
]
If I search for Subitem 1, then the expected output is:
[
{
"id": 1,
"templateName": "Item 1",
"isFolder": "true",
"children": [
{
"id": 2,
"templateName": "Subitem 1",
"isFolder": "true",
"children": [
{
"id": 3,
"templateName": "Misc 1",
"isFolder": "true",
"children": [
{
"id": 4,
"templateName": "Misc 2"
}
]
}
]
}
]
},
{
"id": 7,
"templateName": "Item 2",
"isFolder": "true",
"children": [
{
"id": 8,
"templateName": "Subitem 1",
"isFolder": "true",
"children": [
{
"id": 9,
"templateName": "Misc 1"
}
]
}
]
}
]
But the answer in the referenced Q&A returns objects without children properties.
I tried to modify that answer's code and also I added isFolder props for additional validation for my help. But I cannot make it return the expected output above.
Subitem 1, it id insideItem 1. So I want the output Item 1 with only Subitem 1 and their children. Same, if subitem 1 where present in other child, I want that complete parent track