【问题标题】:Item's depth in ArrayCollectionArrayCollection 中项目的深度
【发布时间】:2010-03-21 16:23:04
【问题描述】:

是否有可能在 ArrayCollection 中获取项目的深度?

【问题讨论】:

    标签: apache-flex arrays collections depth


    【解决方案1】:

    来自livedocs

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

    【讨论】:

    • 它不是深度,而是索引...通过“深度”我的意思是该项目嵌套在 ArrayCollection 树中的程度
    【解决方案2】:

    这是我的代码...

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

    【讨论】:

      【解决方案3】:

      不是答案,我不能发布 cmets。

      来自实时文档:http://livedocs.adobe.com/flex/3/langref/mx/collections/ArrayCollection.html

      ArrayCollection 类是一个包装器 将数组公开为 可以访问的集合和 使用方法和操纵 ICollectionView 的属性或 IList 接口。

      为什么你认为 ArrayCollection 有深度?

      我猜你可以创建一个子 ArrayCollections 的 ArrayCollection。如果是这种情况;那么你可以编写一个函数来搜索它的所有子 ArrayCollections。

      编辑:我认为您建议的功能存在一些错误。这是我尝试过的一个功能:

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

      【讨论】:

      • 我听到的对 Flex 的批评是这样的; Flex 没有很好的集合类。 :-/
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      相关资源
      最近更新 更多