GXT 3 动态更新 treeStore 中的元素
GXT 3 dynamically update an element in a treeStore
我目前正在使用 GXT 3 在树中显示元素。
这些元素是从数据库中检索的,并在树中由它们的 id 标识(我的意思是 id 是我商店的 ModelKeyProvider)。
我还使用户可以使用以下代码在树中本地创建对象:
private Tree<EntityDAO, String> tree;
private TreeStore<EntityDAO> store;
int count = 1;
// instanciation and irrelevant stuff
...
EntityDAO sel = tree.getSelectionModel().getSelectedItem();
EntityDAO child = new EntityDAO();
child.setId((long) count);
store.add(store.getParent(sel), child);
count++;
tree.setExpanded(sel, true);
tree.getSelectionModel().select(child, false);
如您所见,我为我的本地对象设置了一个临时 ID(计数)。
当我将对象保存在数据库中时会出现此问题。然后将永久 ID 设置为我的 EntityDAO,但是当我尝试将此 ID 设置为我的本地对象以将其与数据库同步时,它不起作用。
我试过直接修改子id
child.setId(result);
tree.update(child);
我已尝试使用正确的 ID 添加我的对象的副本,然后从树中删除我的对象
EntityDAO newPR = child;
newPR.setId(result);
store.add(store.getParent(child), newPR);
store.remove(child);
但是显示永远不会更新。有什么线索吗?
让我们讨论一下您尝试的第一种方式,即更新方法:
child.setId(result);
tree.update(child);
从更新方法 API 说明:
Replaces the item that matches the key of the given item, and fires a
StoreUpdateEvent to indicate that this change has occurred. Any
changes to the previous model via it's record instance will be lost
and the record will be removed. This will not cause the sort or filter
to be re-applied to the object. Overrides: update(...) in Store
Parameters: item the new item to take its place in the Store.
所以基本上,更新方法将替换商店内与您的参数具有相同键的项目。您的数据有一个商店中不存在的新密钥,这就是它不会对您的树显示产生任何影响的原因。
其次,让我们讨论创建对象的副本并为其设置正确的 ID:
EntityDAO newPR = child;
newPR.setId(result);
store.add(store.getParent(child), newPR);
store.remove(child);
这个方法其实是可行的,只是你遇到了一个小问题。您的代码的第一行实际上只是给您一个变量,该变量引用了您的旧对象(子对象),因此每当您删除子对象时,newPR 也会删除。你真的应该使用构造函数创建一个新对象,我认为你应该这样做:
EntityDAO newPR = new EntityDAO();
newPR.setId(result);
newPR.setOtherProperty(child.getOtherProperty());
// just copy all property of child to newPR
store.add(store.getParent(child), newPR);
store.remove(child);
希望对您有所帮助。
我目前正在使用 GXT 3 在树中显示元素。 这些元素是从数据库中检索的,并在树中由它们的 id 标识(我的意思是 id 是我商店的 ModelKeyProvider)。 我还使用户可以使用以下代码在树中本地创建对象:
private Tree<EntityDAO, String> tree;
private TreeStore<EntityDAO> store;
int count = 1;
// instanciation and irrelevant stuff
...
EntityDAO sel = tree.getSelectionModel().getSelectedItem();
EntityDAO child = new EntityDAO();
child.setId((long) count);
store.add(store.getParent(sel), child);
count++;
tree.setExpanded(sel, true);
tree.getSelectionModel().select(child, false);
如您所见,我为我的本地对象设置了一个临时 ID(计数)。 当我将对象保存在数据库中时会出现此问题。然后将永久 ID 设置为我的 EntityDAO,但是当我尝试将此 ID 设置为我的本地对象以将其与数据库同步时,它不起作用。 我试过直接修改子id
child.setId(result);
tree.update(child);
我已尝试使用正确的 ID 添加我的对象的副本,然后从树中删除我的对象
EntityDAO newPR = child;
newPR.setId(result);
store.add(store.getParent(child), newPR);
store.remove(child);
但是显示永远不会更新。有什么线索吗?
让我们讨论一下您尝试的第一种方式,即更新方法:
child.setId(result);
tree.update(child);
从更新方法 API 说明:
Replaces the item that matches the key of the given item, and fires a StoreUpdateEvent to indicate that this change has occurred. Any changes to the previous model via it's record instance will be lost and the record will be removed. This will not cause the sort or filter to be re-applied to the object. Overrides: update(...) in Store Parameters: item the new item to take its place in the Store.
所以基本上,更新方法将替换商店内与您的参数具有相同键的项目。您的数据有一个商店中不存在的新密钥,这就是它不会对您的树显示产生任何影响的原因。
其次,让我们讨论创建对象的副本并为其设置正确的 ID:
EntityDAO newPR = child;
newPR.setId(result);
store.add(store.getParent(child), newPR);
store.remove(child);
这个方法其实是可行的,只是你遇到了一个小问题。您的代码的第一行实际上只是给您一个变量,该变量引用了您的旧对象(子对象),因此每当您删除子对象时,newPR 也会删除。你真的应该使用构造函数创建一个新对象,我认为你应该这样做:
EntityDAO newPR = new EntityDAO();
newPR.setId(result);
newPR.setOtherProperty(child.getOtherProperty());
// just copy all property of child to newPR
store.add(store.getParent(child), newPR);
store.remove(child);
希望对您有所帮助。