GWT CellTree 父节点选择

发布于 2024-12-06 17:23:07 字数 396 浏览 0 评论 0原文

我正在尝试使用 GWT CellTree 来显示异构的分层数据模型。我需要能够进行单个选择,但能够选择父节点以及子节点。例如,如果您查看 GWT 自己的 示例,你会发现它们只为离开节点提供了一种选择模型。

我尝试通过为所有节点提供一种选择模型来扩展他们的示例。然而,这似乎是不可能的。所以我最终得到了 3 个 SelectionModels,每个节点类型(Composer、PlayList、Song)一个。

我缺少什么?

提前致谢。

I'm trying to use the GWT CellTree to display a heterogeneous, hierarchical data model. I need to be able to a single selection, but be able to select Parent nodes as well as child nodes. For example, if you look at GWT's own example, you'll see that they only provide one selection model for the leave nodes.

I tried to extend their example by providing one selection model for all nodes. However, that seems impossible. So what I ended up with where 3 SelectionModels one for each node type (Composer, PlayList, Song).

What am I missing?

Thanks in advance.

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

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

发布评论

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

评论(1

只有一腔孤勇 2024-12-13 17:23:07

TreeViewModelgetNodeInfo 函数中,您必须将选择模型传递给每个级别的每个新 DefaultNodeInfo 实例。

return new DefaultNodeInfo<MyDTO>(dataProvider,new MyDTOCell(),selectionModel,null);

然后在 SelectionChangeEventHandler 中执行如下操作:

selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
    @Override
    public void onSelectionChange(SelectionChangeEvent event) {
        Object object = selectionModel.getSelectedObject();
        if (object instanceof MyRootDTO)
        {
              // DO SOMETHING with root level selected node
        }
        else if (object instanceof MySecondLevelDTO) {
        // DO SOMETHING WITH 2. level selected node
        }
        // additional levels
});

更新
为了解决打字问题,您可以定义一个由所有 DTO 扩展的抽象基类。

public abstract class BaseModel  {

    public static final ProvidesKey<BaseModel> KEY_PROVIDER = new ProvidesKey<BaseModel>() {
      public Object getKey(BaseModel item) {
        return item == null ? null : item.getId();
      }
    };

    public abstract Object getId();
}

在 DTO 中,您扩展 BaseModel 并实现抽象 getId() 方法:

public class MyDTO extends BaseModel {
        @Override
        public Object getId() {
             //return unique ID (i.e. MyDTO_1)
        }
}

In the getNodeInfo function of your TreeViewModel you have to pass the selectionModel to each new DefaultNodeInfo instance at each level.

return new DefaultNodeInfo<MyDTO>(dataProvider,new MyDTOCell(),selectionModel,null);

and then in the SelectionChangeEventHandler you have do something like this:

selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
    @Override
    public void onSelectionChange(SelectionChangeEvent event) {
        Object object = selectionModel.getSelectedObject();
        if (object instanceof MyRootDTO)
        {
              // DO SOMETHING with root level selected node
        }
        else if (object instanceof MySecondLevelDTO) {
        // DO SOMETHING WITH 2. level selected node
        }
        // additional levels
});

Update:
In order to get around the typing problem, you can define an abstract base class which is extended by all your DTO's.

public abstract class BaseModel  {

    public static final ProvidesKey<BaseModel> KEY_PROVIDER = new ProvidesKey<BaseModel>() {
      public Object getKey(BaseModel item) {
        return item == null ? null : item.getId();
      }
    };

    public abstract Object getId();
}

In your DTO's you extend the BaseModel and implement the abstract getId() method:

public class MyDTO extends BaseModel {
        @Override
        public Object getId() {
             //return unique ID (i.e. MyDTO_1)
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文