I am currently working on an application that involves a tree structure of various objects. Each object has a unique ID and the possibility of having a parent object.
Unfortunately, I am facing an issue with the standard filter syntax when the current ID is undefined, especially at the root level. The filter works perfectly when the current ID is defined, but fails when it is not.
Is there a way to create an expression that can handle both undefined values and actual values?
For example:
<ol>
<li ng-repeat='item in list | filter:{parent:current_item_id}'>
<a ng-click='current_item_id = item._id'>{{ item }}</a>
</li>
</ol>
If the answer is that it cannot be done, that's okay. I have already developed a custom filter that provides a similar functionality. It may not be as versatile as the built-in 'filter' filter, but it gets the job done.
As requested, here is a sample of JSON data:
{
"items":[
{'_id':1,'name':"Parent 1"},
{'_id':2,'name':"Parent 2"},
{'_id':3,'name':"Child 1", "parent":1},
{'_id':4,'name':"Child 2", "parent":2}
]
}