关于 Gwt TreeViewModel getNodeInfo() 方法

发布于 2024-12-01 15:50:26 字数 2004 浏览 4 评论 0原文

我无法理解那部分,也没有尝试展示示例。

我正在使用 AsyncDataProvider 的扩展将我的树绑定到 RPC 服务。这是我的方法:

public <T> NodeInfo<?> getNodeInfo(T value) {
        /*
        if (value instanceof Categoria) {
            dataProvider.setCurrentParent((Categoria)value);
        }
        */
        return new DefaultNodeInfo<Categoria>(dataProvider, new CategoriaCell());
    }

“currentParent”是我的东西:除了 (null => root) 值之外,我将父级设置为通过 RPC 传递到我的服务。实际上,在我的小部件代码中:

dataProvider = new CategorieTreeDataProvider() {            
            @Override
            protected void onRangeChanged(HasData<Categoria> display) {
                updateTree(getCurrentParent());
            }
        };

private void updateTree(Categoria categoria) {
        rpcService.getCategorie(categoria, new AsyncCallback<Categoria[]>() {
            @Override
            public void onSuccess(Categoria[] result) {
                dataProvider.updateRowCount(result.length, true);
                dataProvider.updateRowData(0, Arrays.asList(result));
            }
            @Override
            public void onFailure(Throwable caught) {
                Window.alert(caught.toString());
            }
        });
    }

然而,我的 rpc 服务器代码正在按预期工作:

@Override
    public Categoria[] getCategorie(Categoria parent) {
        List<Categoria> categoryList = categorieDao.listByProperty("parent", parent);
        for (Categoria c : categoryList) {
            if (categorieDao.listByProperty("parent", c).size() == 0) {
                c.setLeaf(true);
            }
        }
        return categoryList.toArray(new Categoria[0]);
    }

**然后我向我的类别添加一些数据:“祖父”、“父亲”和“儿子”。

不幸的是,加载我的小部件后,我看到:

  • 祖父正确,他的“+”如何预期;

  • 然后我单击它,然后...

  • 祖父消失了,我看到“父亲”和他的“+”

  • 父亲的意思相同 ->儿子

,我怀疑这个错误是在 updateRowCount / updateRowData 使用中。**

有什么想法吗?

I can't understand that part, neither trying the showcase examples.

I'm using an extension of AsyncDataProvider to bind my tree to RPC service. Here's my method:

public <T> NodeInfo<?> getNodeInfo(T value) {
        /*
        if (value instanceof Categoria) {
            dataProvider.setCurrentParent((Categoria)value);
        }
        */
        return new DefaultNodeInfo<Categoria>(dataProvider, new CategoriaCell());
    }

"currentParent" is my stuff: except for (null => root) values, I set the parent to pass via RPC to my service. Actually, in my widget code:

dataProvider = new CategorieTreeDataProvider() {            
            @Override
            protected void onRangeChanged(HasData<Categoria> display) {
                updateTree(getCurrentParent());
            }
        };

private void updateTree(Categoria categoria) {
        rpcService.getCategorie(categoria, new AsyncCallback<Categoria[]>() {
            @Override
            public void onSuccess(Categoria[] result) {
                dataProvider.updateRowCount(result.length, true);
                dataProvider.updateRowData(0, Arrays.asList(result));
            }
            @Override
            public void onFailure(Throwable caught) {
                Window.alert(caught.toString());
            }
        });
    }

My rpc-server code, however, is working as expected:

@Override
    public Categoria[] getCategorie(Categoria parent) {
        List<Categoria> categoryList = categorieDao.listByProperty("parent", parent);
        for (Categoria c : categoryList) {
            if (categorieDao.listByProperty("parent", c).size() == 0) {
                c.setLeaf(true);
            }
        }
        return categoryList.toArray(new Categoria[0]);
    }

**Then I add some data to my Categories: 'GrandFather', 'Father' and 'Son'.

Unfortunately, after loading my widget, I see:

  • The grandfather correctly, with his "+" how expected;

  • Then I click it and...

  • The grandfather disappear and I see 'Father' with his '+'

  • same for father -> son

I suspect the bug is in updateRowCount / updateRowData usage.**

Any ideas?

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

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

发布评论

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

评论(2

⒈起吃苦の倖褔 2024-12-08 15:50:26

每当您打开节点时都会调用 getNodeInfo,因此您必须为每个节点的子节点创建不同的 DataProvider。

public <T> NodeInfo<?> getNodeInfo(T value) {
        if (value == null) {
            return new DefaultNodeInfo<Category>(dataProvider, new CategoriaCell());
        }
        else if (value instanceof Categoria) {
                 Category category  = (Category)value;
             return new DefaultNodeInfo<Grandfather>(new ListDataProvider<Grandfather>(category.getGrandFathers()),new GrandFatherCell());
        }
        else if (value instanceof Grandfather) {
             Grandfather grandfather = (Grandfather)value;
             return new DefaultNodeInfo<Father>(new ListDataProvider<Father>(granfather.getFathers()),new FatherCell());
        }
        else if (value instanceof Father) {
            //same as above but with fathers.
        }
    }

例如,category.getGrandFathers() 函数可以向服务器发出 RPC 请求,或者如果您在一个 RPC 请求中检索所有内容,则仅返回列表。

根据评论更新:

因此,如果您只有一个类并且想要实现动态 CellTree(级别数未预先确定),您可以采取以下方法。

public <T> NodeInfo<?> getNodeInfo(T value) {
    if (value == null) {
        return new DefaultNodeInfo<Category>(dataProvider, new CategoriaCell());
    }
    else {
        Category category  = (Category)value;
        return new DefaultNodeInfo<Category>(new ListDataProvider<Category>(category.getSubCategories()),new CategoryCell());
    }
}

category.getSubCategories() 是检索当前类别的子类别的 RPC 调用,或者如果 Category 类是链接列表类型数据结构,则它可以仅返回子类别列表。

The getNodeInfo is called whenever you open a node so you have to create distinct DataProvider for each of the nodes's childs.

public <T> NodeInfo<?> getNodeInfo(T value) {
        if (value == null) {
            return new DefaultNodeInfo<Category>(dataProvider, new CategoriaCell());
        }
        else if (value instanceof Categoria) {
                 Category category  = (Category)value;
             return new DefaultNodeInfo<Grandfather>(new ListDataProvider<Grandfather>(category.getGrandFathers()),new GrandFatherCell());
        }
        else if (value instanceof Grandfather) {
             Grandfather grandfather = (Grandfather)value;
             return new DefaultNodeInfo<Father>(new ListDataProvider<Father>(granfather.getFathers()),new FatherCell());
        }
        else if (value instanceof Father) {
            //same as above but with fathers.
        }
    }

The category.getGrandFathers() function can for example do a RPC request to the server or just return the list if you retrieve everything in one RPC request.

UPDATE based on comment:

So in case you have only one class and want to achieve a dynamic CellTree (number of levels are not pre-determined) you could take following approach.

public <T> NodeInfo<?> getNodeInfo(T value) {
    if (value == null) {
        return new DefaultNodeInfo<Category>(dataProvider, new CategoriaCell());
    }
    else {
        Category category  = (Category)value;
        return new DefaultNodeInfo<Category>(new ListDataProvider<Category>(category.getSubCategories()),new CategoryCell());
    }
}

category.getSubCategories() is either an RPC call which retrieves the subcategories for the current category or if the Category class is a linked list type datastructure it could just return the list of subcategories.

半岛未凉 2024-12-08 15:50:26

每个数据提供程序都会更新给定的“列表”(给定父节点的子节点),因此您必须为每个父节点使用不同的数据提供程序实例,否则您的调用将更新一些随机列表。

Each data provider updates a given "list" (child nodes of a given parent node), so you have to use a distinct data provider instance for each parent node, or your calls will update some random list.

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