简体   繁体   English

如何使用flex3将HierarchicalCollectionView转换为Array?

[英]how to convert HierarchicalCollectionView to Array using flex3?

i used advance datagrid of dataProvider as HierarchicalData . 我使用dataProvider的高级datagrid作为HierarchicalData。 it's an my array collection strature 这是我的数组集合结构

private var groupList:ArrayCollection = new ArrayCollection([
               {Country:'India', children:[
                               {Matches:'India Test series 1',isEnable:false},
                               {Matches:'India Test series 2',isEnable:false},
                               {Matches:'India Test series 3',isEnable:false}]},
               {Country:'Australia', children:[
                               {Matches:'Australia Test series 1',isEnable:false},
                               {Matches:'Australia Test series 2',isEnable:false},
                               {Matches:'Australia Test series 3',isEnable:false}]},
               {Country:'japan', children:[
                               {Matches:'Australia Test series 1',isEnable:false},
                               {Matches:'Australia Test series 2',isEnable:false},
                               {Matches:'Australia Test series 3',isEnable:false}]},
              
            ]);

i want to get particular data like matches details and count which one is (isEnable==true) value . 我想获取特定数据,例如匹配详细信息并计算(isEnable == true)value之一。 so convert HierarchicalCollectionView to Array like 所以将HierarchicalCollectionView转换为Array就像

var hCollView:HierarchicalCollectionView = updategrid.dataProvider as HierarchicalCollectionView;

        var hCollData:HierarchicalData = hCollView.source  as HierarchicalData;

        var hArrayColl:ArrayCollection =  hCollData.source as ArrayCollection;

        var hArray:Array = hArrayColl.source as Array;


        for(var i:Number=0;i<hArray.length;i++)
     {


        Alert.show(hArray[i].Country);




        if(hArray[i].isEnable=="true")
        {
                    count1++;

        }


       }

It shows only country details but if i tried matches and isEnable not found in array . 它仅显示国家/地区详细信息,但如果我尝试匹配,并且在array中找不到isEnable。 How can i found is isEnable and matches details ? 我如何找到isEnable并匹配详细信息? Please refer me 请介绍我

You are still only testing the top level children in your loop. 您仍然仅在循环中测试顶级子级。 Change it to, 更改为

for( var i : Number = 0; i < hArray.length; i++ ) {
    Alert.show( hArray[i].Country );
    for each ( match : Object in hArray[i].children ) {
        if ( match.isEnable ) {
            count1++;
        }
    }
}

This way, you are looping through the children of each of the top level objects to reach the match objects. 这样,您将循环遍历每个顶级对象的子级以到达匹配对象。

You should probably think about making a typed object for both your Country and Match objects, you'll be able to encapsulate this type of behaviour much easier. 您可能应该考虑为Country和Match对象创建一个类型化的对象,这样您就可以更轻松地封装这种类型的行为。

public static function convertHCollViewToArrColl(hcoll: HierarchicalCollectionView): ArrayCollection
{
    var arrcoll: ArrayCollection = new ArrayCollection();
    fectchNodeFromHColl(hcoll, null, arrcoll);
    return arrcoll;

}

internal static function fectchNodeFromHColl(hcoll: HierarchicalCollectionView, node: Object, targetArrayColl: ArrayCollection): void
{
    var subNodes: ICollectionView;

    if(node == null)
    {
        subNodes = (hcoll.source as HierarchicalData).source as ArrayCollection;
    }else
    {
        targetArrayColl.addItem(node);
        subNodes = hcoll.getChildren(node);

    }

    if(subNodes !=null && subNodes.length > 0)
    {
        for each(var n: Object in subNodes)
        {
            fectchNodeFromHColl(hcoll, n, targetArrayColl);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM