将节点添加到树

时间:2009-09-10 08:26:40

标签: flex

[Bindable]
            private var company:XML =

              <list>
                <department title="Finance" code="200">
                    <employee name="John H"/>

                    <employee name="Sam K"/>
                </department>
                <department title="Operations" code="400">

                    <employee name="Bill C"/>
                    <employee name="Jill W"/>
                </department>                    
                <department title="Engineering" code="300">

                    <employee name="Erin M"/>
                    <employee name="Ann B"/>
                </department>                                
              </list>;

private function addEmployee():void

            {
                var newNode:XML = <employee/>;
                newNode.@name = empName.text;
                var dept:XMLList =company.department.(@title == "Operations");
                if( dept.length() > 0 ) {

                    dept[0].appendChild(newNode);
                    empName.text = "";
                }
            }

特殊代码为操作添加了一个新节点,但我想添加一个节点我选择的任何项目。

2 个答案:

答案 0 :(得分:1)

尝试使用XMLList,然后循环遍历部门节点,每次检查其部门标题。

我有点不确定你究竟想做什么。

答案 1 :(得分:1)

添加一个组合框(myCombobox)供用户选择添加用户的位置(即操作,财务,工程)。根据选定的部门添加到特定列表:

 private function addEmployee():void
        {
            var newNode:XML = <employee/>;
            newNode.@name = empName.text;

            var dept:XMLList;

            switch(myCombobox.selectedLabel){
                 case 'Operations':
                     dept = company.department.(@title == "Operations");
                     break;

                 case 'Finance':
                     dept = company.department.(@title == "Finance");
                     break;

                 case 'Engineering':
                     dept = company.department.(@title == "Engineering");
                     break;
            }

            if( dept.length() > 0 ) {

                dept[0].appendChild(newNode);
                empName.text = "";
            }
        }