views:

48

answers:

3

Hi,

is it somehow possible to get item's depth in ArrayCollection?

A: 

From the livedocs:

// Get the index of the item with the value ME.
var addedItemIndex:int=myAC.getItemIndex("ME");
Greg W
it is not depth, but index...by "depth" I mean how much is that item nested in ArrayCollection tree
luccio
A: 

Not an answer, I can't post comments.

From live docs: http://livedocs.adobe.com/flex/3/langref/mx/collections/ArrayCollection.html

The ArrayCollection class is a wrapper class that exposes an Array as a collection that can be accessed and manipulated using the methods and properties of the ICollectionView or IList interfaces.

Why do you think an ArrayCollection has depth?

I guess you can make an ArrayCollection of sub-ArrayCollections. If this is the case; then you could write a function that searches through all its sub-ArrayCollections.

EDIT: I think there are some bugs in the function you suggested. Here's a function I tried:

public function getItemNestLevel2(needle:Object, haystack:Object):Number
{
    for each (var item:Object in haystack)
    {
        if (item == needle)
            return 0;
        if (item is Array || item is ArrayCollection)
        {
            var nestLevel:int = getItemNestLevel2(needle, item);
            if (nestLevel >= 0)
                return nestLevel + 1;
        }
    }
    return -1;
}
mauvo
I was hoping, that there is already such a function. Thanks
luccio
A critiscism of Flex I've heard is that; Flex doesn't have great collection classes. :-/
mauvo
A: 

here is my code...

public function getItemNestLevel(needle:Object, haystack:Object, level:Number = 0):Number
    {
        //iterate through items
        for each (var item:Object in haystack)
        {
            if (item == needle)
            {
                return level;
            }

            //iterate through item's properties
            for each (var child:Object in item)
            {
                if (child is Array || child is ArrayCollection)
                {
                    var lvl:Number = level + 1;
                    var num:Number = getItemNestLevel(needle, child, lvl);
                    if (num >= 0)
                    {
                        return num;
                    }
                }
            }
        }
        return -1;
    }
luccio