隐藏Flex树组件中的节点以保持子项可见

时间:2011-04-09 20:17:44

标签: flex actionscript tree

我有一个带有数据提供者的树,它采用以下形式:

<details name="Cars">
    <contact_person>aaaa</contact_person>
    <list>
        <car type="A">
            <car name="A1"/>
            <car name="A2"/>
        </car>
        <car type="B">
            <car name="B1"/>
            <car name="B2"/>
        </car>
    </list>
</details>

我想让树像这样显示

Cars
     A
          A1
          A2
     B
          B1
          B2

那就是我想要隐藏contact_person和list nodes.Deleting来自dataprovider的节点无法完成。所以我做的是通过扩展DefaultDataDescriptor来创建自定义树数据描述符。然后重写getChildren方法并应用过滤器函数到super.getChildren返回的集合。问题是当我隐藏'list'节点时我不能让子节点显示出来。所以我有什么方法可以隐藏'list'但是显示'node'的子节点?

1 个答案:

答案 0 :(得分:0)

XML作为dataProvider传递适用于演示,并且在涉及真实产品时不起作用。通常的做法是将XML解析为强类型对象:

public class Details 
{ 
    public function Details(xml:XML)
    {
        label = xml.@name;
        var childrenArray:Array = [];
        for each (var carNode:XML in xml.list.car)
        {
            childrenArray.push(new CarType(carNode));
        }
        children = new ArrayCollection(childrenArray);
    }

    [Bindable]
    public var label:String;

    [Bindable]
    public var children:ArrayCollection /* of CarType */;
}

public class CarType
{
    public function CarType(xml:XML)
    {
        label = xml.@type;
        var childrenArray:Array = [];
        for each (var carNode:XML in xml.car)
        {
            childrenArray.push(new Car(xml));
        }
        children = new ArrayCollection(childrenArray);
    }

    [Bindable]
    public var label:String;

    [Bindable]
    public var children:ArrayCollection /* of Car */;
}

public class Car
{
    public function Car(xml:XML)
    {
        label = xml.@name;
    }

    [Bindable]
    public var label:String;
}

用法:

var xml:XML = <details name="Cars">...</details>;
var details:Details = new Details(xml);
var tree:Tree = new Tree();
tree.dataProvider = new ArrayCollection([ details ]);

为简化代码,我在构造函数中解析XML。变量也可以转换为只读属性。