将Dynamic children添加到数组或arraycollection

时间:2009-12-28 02:34:19

标签: flex multidimensional-array

我有点新兴,并且无法理解这个问题。有人可以帮忙吗提前谢谢。

我有一个字符串列表路径 路径1 - “一/二/三”
                           路径2 - “一/二/四”
                           路径3 - “五/六”

我需要一个高级数据网格来显示如此的树结构 一个/
...二/
........三轮/
............ 4个
五类/
....... 6个
但我想用数组,对象或数组收集(如适用)来实现这个动态 我需要使用字符串方法遍历每个字符串路径,这不是一个问题,但我如何创建“动态”(深度)孩子?请帮忙,因为我要把头发拉出来。

2 个答案:

答案 0 :(得分:0)

您可以尝试以下内容:

var paths:Array = ['one/two/three','one/two/four','five/six'];
var pathsCollection:ArrayCollection = new ArrayCollection();

for(var i:int = 0 ; i < paths.length ; i++){
    var folderArr:Array = paths[i].split('/');
    var folderNum:int = folderArr.length;
    var folderLabel:String = '';
    for(var j:int = 0 ; j < folderNum; j++){
        trace(folderLabel+folderArr[j]);
        pathsCollection.addItem({label:folderLabel+folderArr[j],level:j,path:folderArr});
        folderLabel += '...';
    }
}

正如Sharvey所说,看看递归。

答案 1 :(得分:0)

当您对包含未知数量元素的对象进行操作时,递归是您的选择。试试这个示例应用程序:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    creationComplete="{init();}"
    layout="vertical"
    verticalAlign="middle">
<mx:Script>
    <![CDATA[
        import mx.utils.ObjectUtil;
        import mx.collections.HierarchicalData;
        private var paths:Array = ['one/two/three','one/two/four','five/six'];
        private static const DELIMITER:String = "/";

        private function init():void {
            var test:Array = buildHierarchy(paths);
            dg_test.dataProvider = new HierarchicalData(test);
            trace(ObjectUtil.toString(test));
        }

        private function buildHierarchy(arr:Array):Array {
            var ret:Array = new Array();
            var o:Object = new Object();
            /* Loop over the paths array */
            for (var i:int = 0; i < arr.length; i++) {
                /* Split the string block according to the delimiter */
                var parts:Array = String(arr[i]).split(DELIMITER);
                if (parts.length) {
                    /* Make a new object with a label equal to the first string element */
                    o = new Object();
                    o.label = parts[0];

                    /* Remove the first item in the string list */
                    parts.splice(0, 1);

                    /* Important - If the string has remaining members, call this 
                        function again with the remaining members. Assign this to 
                        the 'children' property of the newly created object */
                    if (parts.length > 0) 
                        o.children = buildHierarchy([parts.join(DELIMITER)]);

                    /* Add the object to the new array */
                    ret.push(o);
                }                   
            }
            return ret;
        }
    ]]>
</mx:Script>
<mx:AdvancedDataGrid id="dg_test" height="200" width="400">
    <mx:columns>
        <mx:AdvancedDataGridColumn id="col_label" dataField="label"/>
    </mx:columns>
</mx:AdvancedDataGrid>

此函数将为“string / string / string”块中包含的每个元素调用一次。在ADG中显示此结构的关键是设置adg.dataProvider = new HierarchicalData(myArray);

希望这有效!我无法将代码格式化为100%,但您应该明白这一点。不要忘记添加结束应用程序标记。