开发者

Item's depth in ArrayCollection

is it somehow possible to get item's depth in A开发者_如何转开发rrayCollection?


From the livedocs:

// Get the index of the item with the value ME.
var addedItemIndex:int=myAC.getItemIndex("ME");


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;
    }


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜