tags:

views:

40

answers:

2

I'm working on a tree component using a XMLLIST as a data provider.

<list>
    <menuItem label="Home" menuItemId="1" >
         <menuItem label="Info 1" menuItemId ="4"></menuItem>
     </menuItem>
     <menuItem label="Services" menuItemId="2" >
     </menuItem>
     <menuItem label="About" menuItemId="3" >
     </menuItem>
</list>

I need to select a nested node by the property 'menuItemId' without knowing the index.For example, select the item with the menuItemId 4.

Any ideas?

A: 

Use E4X. For example,

var myList:XMLList = <list>
    <menuItem label="Home" menuItemId="1" >
         <menuItem label="Info 1" menuItemId ="4"></menuItem>
     </menuItem>
     <menuItem label="Services" menuItemId="2" >
     </menuItem>
     <menuItem label="About" menuItemId="3" >
     </menuItem>
</list>;

var menuItemId4:XMLList = myList.menuItem.(@menuItemId==4);
Robusto
What I need is to select the node in the tree, for example: selectedIndex = 2; but i don't know the index I need to select it by the property 'menuItemId'.
A: 

Try something like this, filtering the data provider of the tree to find the right object, and then getting the index of the object in the data provider and telling the tree to select that item. Seems like a roundabout way to do it, but I think that is the best I can come up with right now.

var filter:Array = tree.dataProvider.toArray().filter(filterFunc)
if (filter.length > 0)
    tree.selectedIndex = tree.dataProvider.getItemIndex(filter[0]);

protected function filterFunc(item:*, index:int, array:Array):Boolean{
    return (item as MenuItem).menuItemId == "2"
}
rosswil