如何从 GWT 中的 CellTree 获取所选项目的路径

发布于 2024-12-11 21:58:32 字数 105 浏览 4 评论 0原文

我有一个包含组织列表的 CellTree。在模型中,每个父级都包含一个子级列表。 有没有一种简单的方法来获取所选项目的路径?或者我必须使用通常的方法,迭代树并按 id 查找。

谢谢

I have a CellTree containing a list of organisations. In the Model, each parent contains a list of a children.
Is there an easy way to get the path of a selected item ? or I have to go with the usual methode by iterating through the tree and lookup by id.

Thanks

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

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

发布评论

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

评论(3

不念旧人 2024-12-18 21:58:32

CellTree 本身没有任何内置功能来检索所选项目的路径。
您必须手动遍历树结构才能获取路径。您可以使用递归函数来检索路径。我已经做了类似的事情,但出于不同的目的(以编程方式打开 CellTree 中的节点)。

The CellTree itself does not have any built-in functionality to retrieve the path for the selected item.
You will have to manually traverse through your tree structure to get the path. You can use a recursive function to retrieve the path. I have done something like that but for a different purpose (programmatically opening nodes in a CellTree).

避讳 2024-12-18 21:58:32

这不是一个优雅的解决方案。我从业务模型而不是树中获取路径。

public void getNodeContainingEntity(EntityBase selectedEntity, TreeNode node){
    if (exit) return;
    int count = node.getChildCount();
    for(int i=0;i<count;i++){   
        EntityBase entityChild = (EntityBaseProxy) node.getChildValue(i);           
            if(selectedEntity.getNodeId() == entityChild.getNodeId()){
                    exit = true;
                    currentNode =  node;
            }
            else if(node.isChildOpen(i)){
                TreeNode n = node.setChildOpen(i, true);
                if(n!=null)
                    getNodeContainingEntity(selectedEntity,n);
            }           
    }
}


public List<EntityBaseProxy> getPath(EntityBase selectedEntity){
    exit = false;
    getNodeContainingEntity(selectedEntity,cellTree.getRootTreeNode());
    path.clear();
    path.add(selectedEntity);
    if (null == currentNode) return path;           
        path.add(((EntityBase)currentNode.getValue()));
        while (null != currentNode.getParent() ){
            currentNode  = currentNode.getParent();
            EntityBase entity = (EntityBase)currentNode.getValue();
            if (null != entity) path.add((EntityBaseProxy)currentNode.getValue());  
        }       
    return path;
}

Not an elegant solution. I fetch the path from the business model instead of the Tree.

public void getNodeContainingEntity(EntityBase selectedEntity, TreeNode node){
    if (exit) return;
    int count = node.getChildCount();
    for(int i=0;i<count;i++){   
        EntityBase entityChild = (EntityBaseProxy) node.getChildValue(i);           
            if(selectedEntity.getNodeId() == entityChild.getNodeId()){
                    exit = true;
                    currentNode =  node;
            }
            else if(node.isChildOpen(i)){
                TreeNode n = node.setChildOpen(i, true);
                if(n!=null)
                    getNodeContainingEntity(selectedEntity,n);
            }           
    }
}


public List<EntityBaseProxy> getPath(EntityBase selectedEntity){
    exit = false;
    getNodeContainingEntity(selectedEntity,cellTree.getRootTreeNode());
    path.clear();
    path.add(selectedEntity);
    if (null == currentNode) return path;           
        path.add(((EntityBase)currentNode.getValue()));
        while (null != currentNode.getParent() ){
            currentNode  = currentNode.getParent();
            EntityBase entity = (EntityBase)currentNode.getValue();
            if (null != entity) path.add((EntityBaseProxy)currentNode.getValue());  
        }       
    return path;
}
撩心不撩汉 2024-12-18 21:58:32

好吧,我希望这听起来不会太明显或错过您正在寻找的内容。但一种思考方法是从选定的节点开始并向上(而不是从根开始向下)。 GWT TreeItems 有一个 getParent() 方法,因此请继续调用它,将您获得的内容添加到列表中,直到您到达根(或什么都没有),这将为您提供一个反向路径作为向上的节点列表,它是唯一的,一个节点只能有一个父节点,然后只需反转列表即可使路径继续下去。

这是一些未经测试的代码,旨在成为 TreeItem 子类的方法(否则您会将节点作为该方法的参数,例如“selectedItem”,然后在整个

ArrayList getPath(){
    ArrayList path= new ArrayList();  // in reverse ie going up
    path.add(this);
    TreeItem search;

    search=this.getParentItem();

    while (search!=null){
        path.add(search);
        search=search.getParentItem();      
    }

    Collections.reverse(path);  // now coming down from the root

return
        path;   
}

Best 中替换 selectedItem,
马丁

Well, I hope this does not sound too obvious or misses what you are looking for. But one way to think about it is to start with the selected node and go up (not to start at the root and go down). GWT TreeItems have a getParent() method so keep calling it, adding what you get to a list, until you hit the root (or nothing) and that will give you a reversed path as a list of nodes going up, it is unique, a node can have only one parent, then just reverse the list to get your path going down.

Here is some untested code aimed to be a method of a subclass of TreeItem (otherwise you'd put the node as a parameter to the method, say 'selectedItem' and then substitute selectedItem for this throughout

ArrayList getPath(){
    ArrayList path= new ArrayList();  // in reverse ie going up
    path.add(this);
    TreeItem search;

    search=this.getParentItem();

    while (search!=null){
        path.add(search);
        search=search.getParentItem();      
    }

    Collections.reverse(path);  // now coming down from the root

return
        path;   
}

Best,
Martin

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