开发者

Recursion question for Flex Tree

开发者 https://www.devze.com 2023-02-20 11:35 出处:网络
I have the following xml: <objects> <property name=\"steps\"> <property name=\"array\">

I have the following xml:

<objects>
    <property name="steps">
      <property name="array">
        <object type="com.tn.assistant.models.Step">
          <property name="preDialogues">
            <property name="array">
              <object type="com.tn.assistant.models.Dialogue"/>
            </property>
          </property>
          <property name="question">
            <object type="com.tn.assistant.models.Question">
            </object>
          </property>
        </object>
      </property>
    </property>
 </objects>

I am displaying this with a tree in a Flex 4 / air app. I need to filter out "steps", any "array" nodes, and "preDialogues". They should not show up in the tree, but their children should. I extended DefaultDataDescriptor, and overrode getChildren(), to successfully filter steps and array tags.

override public function getChildren(node:Object, model:Object=null):ICollectionView
{
    var ch:XMLList = new XMLList (node.xns::*);
    var retXMLList:XMLList = new XMLList();
    var retXMLListCtr = 0;
    for (var i = 0; i < ch.length(); i++){
        if (ch[i].@name == "array"){
            return getChildren(ch[i]);
        }

        else if (ch[i].@name == "steps"){
            return getChildren(ch[i]);
        }
/*
        else if (ch[i].@name == "preDialogues"){
            return getChildren(ch[i]);
        }
*/
        else {
            retXMLList[retXMLListCtr] = ch[i];
            retXMLListCtr++;
        }
    }
    var chil:XMLListCollection = new XMLListCollection (retXMLList);
    var chil2:ICollectionView = ICollectionView(chil);
    return chil2;
}

This code filters out "steps" and "array" successfully. However, if I uncomment the "preDialogues" code to try and filter out preDialogues, the q开发者_JAVA百科uestion node gets skipped entirely. I can see why this happens, but what can I do about it? Its been awhile since I've done recursion. I thought I could return some sort of combined list, or something, but I can't get anything to work. Thanks.


It's getting skipped because you're at the level of the "com.tn.assistant.models.Step" node. When you're iterating over the children of that node, in this case "preDialogues" and "question", the first one you come to you simply return it's children (in this case, preDialogues' children). It's never even getting to the point where it checks "question".

It seems like you'd want something more like:

override public function getChildren(node:Object, model:Object=null):ICollectionView
{
    var ch:XMLList = new XMLList (node.xns::*);
    var retXMLList:XMLList = new XMLList();
    var retXMLListCtr = 0;
    for (var i = 0; i < ch.length(); i++){
        var name:String = String(ch[i].@name);
        if (name == "array" ||
            name == "steps" ||
            name == "preDialogues" ||
            name == "question") {
            retXMLList[retXMLListCtr] = getChildren(ch[i]);
        } else {
            retXMLList[retXMLListCtr] = ch[i];
        }
        retXMLListCtr++;
    }
    var chil:XMLListCollection = new XMLListCollection (retXMLList);
    var chil2:ICollectionView = ICollectionView(chil);
    return chil2;
}

It still iterates over each child, but when it gets to an "array" node, it adds its children to the retXMLList and continues iterating, rather than simply returning.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号