Gwt CellTree isLeaf() 问题
我正在尝试按照基本的 CellTree gwt 示例显示类别树。
我所坚持的是确定类别的“叶子”条件。
当一个类别没有子项时,它就“是一片叶子”,对吧?所以,这是我的类别(我使用 Objectify 进行 appengine 持久性):
@Entity
public class Categoria implements Serializable {
private static final long serialVersionUID = 1L;
@Id
Long id;
String nome;
Key<Categoria> parent;
public Categoria() { }
public Categoria(String nome) {
super();
this.nome = nome;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Key<Categoria> getParent() {
return parent;
}
public void setParent(Key<Categoria> parent) {
this.parent = parent;
}
}
我的 TreeViewModel 基于 AsyncDataProvider (我从外部传递):
public class CategorieTreeViewModel implements TreeViewModel {
private AbstractDataProvider<Categoria> dataProvider;
public CategorieTreeViewModel(AbstractDataProvider<Categoria> dataProvider) {
this.dataProvider = dataProvider;
}
@Override
public <T> NodeInfo<?> getNodeInfo(T value) {
return new DefaultNodeInfo<Categoria>(dataProvider, new CategoriaCell());
}
@Override
public boolean isLeaf(Object value) {
return false;
}
}
所以这里是:
dataProvider = new AsyncDataProvider<Categoria>() {
@Override
protected void onRangeChanged(HasData<Categoria> display) {
updateTree();
}
};
private void updateTree() {
rpcService.getCategorie(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());
}
});
}
问题是:因为我没有“叶子属性”我的类别 bean,我怎么知道它是否有孩子?显然是进行了查询,但是 isLeaf 方法需要同步返回,我该如何进行 rpc 调用?
或者我可以在 getCategorie() 调用中检索“叶子”信息,在运行时填充属性,但这可能是一个性能问题。
我能做些什么?
I'm trying to display a tree of Categories, following the basic CellTree gwt examples.
What I am stuck at is determining the "leaf" condition of a Category.
A Category "is-a-leaf" when it hasn't children, right? So, here's my Category (I am using Objectify for appengine persistence):
@Entity
public class Categoria implements Serializable {
private static final long serialVersionUID = 1L;
@Id
Long id;
String nome;
Key<Categoria> parent;
public Categoria() { }
public Categoria(String nome) {
super();
this.nome = nome;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Key<Categoria> getParent() {
return parent;
}
public void setParent(Key<Categoria> parent) {
this.parent = parent;
}
}
My TreeViewModel is based on AsyncDataProvider (which I pass from outside):
public class CategorieTreeViewModel implements TreeViewModel {
private AbstractDataProvider<Categoria> dataProvider;
public CategorieTreeViewModel(AbstractDataProvider<Categoria> dataProvider) {
this.dataProvider = dataProvider;
}
@Override
public <T> NodeInfo<?> getNodeInfo(T value) {
return new DefaultNodeInfo<Categoria>(dataProvider, new CategoriaCell());
}
@Override
public boolean isLeaf(Object value) {
return false;
}
}
So here it is:
dataProvider = new AsyncDataProvider<Categoria>() {
@Override
protected void onRangeChanged(HasData<Categoria> display) {
updateTree();
}
};
private void updateTree() {
rpcService.getCategorie(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());
}
});
}
The question is: since I don't have a "leaf property" on my Category bean, how can I know if it has children or not? By doing a query obviously, but the isLeaf method needs to return synchronously, how can I make my rpc call?
Or I can retrieve that "leaf" information in the getCategorie() call, filling the property at runtime, but this could be a performance problem.
What can I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会向 Category 类添加一个瞬态属性,即一个布尔值 isLeaf,然后在 setParent 方法中,您可以将父级的 isLeaf 属性设置为false(因为如果
this
对象有that
的父对象,则that
对象不是叶子)。使属性成为瞬态意味着它不会被持久化,因此您不必担心数据模型中是否存在该字段。编辑:这是我对
Categoria
类的setParent
方法进行编码的方式...这样,一旦您建立了
Categoria
节点的模型,他们每个人都知道它是否是一片叶子。这是有效的,因为如果parent
将this
作为子项,则parent
不可能是叶子。将isLeaf
属性默认为true
,只需检查其属性即可知道任何给定的Categoria
是否为叶子。I would add a transient property to the Categoria class, a
boolean isLeaf
, then inside thesetParent
method, you could set the parent'sisLeaf
property to false (because ifthis
object has a parent ofthat
, thenthat
object is not a leaf). Making the property transient means it won't be persisted, so you don't have to worry about having that field in your data model.EDIT: Here is how I would code the
Categoria
class'ssetParent
method...That way, once you have built up your model of
Categoria
nodes, each one of them knows whether it is a leaf or not. This works because ifparent
hasthis
as a child,parent
can't possibly be a leaf. Default theisLeaf
property totrue
and you'll know if any givenCategoria
is a leaf just by checking it's property.