Flex树的递归问题

发布于 2024-10-28 01:40:10 字数 1782 浏览 2 评论 0原文

我有以下 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>

我在 Flex 4 / Air 应用程序中用树显示它。我需要过滤掉“步骤”、任何“数组”节点和“对话前”。他们不应该出现在树上,但他们的孩子应该出现。我扩展了 DefaultDataDescriptor,并覆盖了 getChildren(),以成功过滤步骤和数组标签。

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;
}

此代码成功过滤掉“steps”和“array”。但是,如果我取消注释“pre​​Dialogues”代码来尝试过滤掉 preDialogues,则问题节点将被完全跳过。我明白为什么会发生这种情况,但我能做什么呢?自从我完成递归以来已经有一段时间了。我以为我可以返回某种组合列表或其他东西,但我无法让任何东西发挥作用。谢谢。

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 question 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神妖 2024-11-04 01:40:10

它被跳过,因为您处于“com.tn.assistant.models.Step”节点的级别。当您迭代该节点的子节点(在本例中为“preDialogues”和“question”)时,您遇到的第一个节点只需返回它的子节点(在本例中为 preDialogues 的子节点)。它甚至从未达到检查“问题”的地步。

看起来您想要的东西更像是:

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;
}

它仍然迭代每个子节点,但是当它到达“数组”节点时,它将其子节点添加到 retXMLList 并继续迭代,而不是简单地返回。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文