smart gwt TreeGrid addSelectionUpdatedHandler 无法获取所选记录
我有一个treeGrid,并在客户端初始化树。并添加addSelectionUpdatedHandler的hander。棘手的是:它可以在第一时间获取选定的记录。 然后每当我下次选择该记录时,就会触发 SelectionUpdatedEvent,但所选记录始终为空。
public void afterScreenLoad() {
treeGrid = (TreeGrid) BaseWidget.getById(getApp().getScreenName() + "_ListGrid");
...
addTreeGridHandler();
addDataToGrids();
}
private void addDataToGrids() {
Criteria criteria = treeGrid.getCriteria();
tree = new Tree();
root = new TreeNode();
root.setIsFolder(true);
tree.setRoot(root);
jobs.forEach(job -> {
TreeNode jobNode = new TreeNode();
jobNode.setAttribute(ID, job.getId());
jobNode.setAttribute(NAME, job.getVin());
jobNode.setIsFolder(true);
tree.add(jobNode, root);
job.getProcessSteps().forEach(step -> {
TreeNode stepNode = new TreeNode();
stepNode.setAttribute(ID, step.getId());
stepNode.setAttribute(PARENT, job.getId());
stepNode.setAttribute(NAME, step.getName());
tree.add(stepNode, jobNode);
});
});
ListGridField[] fields = treeGrid.getAllFields();
assetsDataSourceIndex += 1;
ClientDataSource clientDataSource = new ClientDataSource(fields, assetsDataSourceIndex, true);
clientDataSource.setCacheData(tree.getAllNodes());
treeGrid.setDataSource(clientDataSource);
treeGrid.fetchData();
treeGrid.setFields(fields);
}
private void addTreeGridHandler() {
getActionUtil().addHandler(treeGrid.addDrawHandler(event -> treeGrid.getBody()
.setCanSelectText(true)));
treeGrid.addDataArrivedHandler((DataArrivedHandler) dataArrivedEvent -> {
SC.logWarn("Data arrived");
if (dataLoaded && !selectionChanged) {
selectPreviousSelectedAssetInTreeGrid();
}
});
getActionUtil().addHandler(treeGrid.addSelectionUpdatedHandler(this::selectionChanged));
}
private void selectionChanged(BrowserEvent selectEvent) {
SC.logWarn(selectEvent.getSource().toString());
TreeNode selectedRecord = treeGrid.getSelectedRecord();
boolean jobSelected = isJobSelected();
SC.logWarn("Selected record existed: " + (selectedRecord != null));
...
}
有人可以帮忙吗?
I have a treeGrid, and initialize the tree in the client. And add hander of addSelectionUpdatedHandler. The tricky thing is: it can get selected record in the first time.
Then whenever I select the record in the next time, The SelectionUpdatedEvent is triggered, but the selected record is always null.
public void afterScreenLoad() {
treeGrid = (TreeGrid) BaseWidget.getById(getApp().getScreenName() + "_ListGrid");
...
addTreeGridHandler();
addDataToGrids();
}
private void addDataToGrids() {
Criteria criteria = treeGrid.getCriteria();
tree = new Tree();
root = new TreeNode();
root.setIsFolder(true);
tree.setRoot(root);
jobs.forEach(job -> {
TreeNode jobNode = new TreeNode();
jobNode.setAttribute(ID, job.getId());
jobNode.setAttribute(NAME, job.getVin());
jobNode.setIsFolder(true);
tree.add(jobNode, root);
job.getProcessSteps().forEach(step -> {
TreeNode stepNode = new TreeNode();
stepNode.setAttribute(ID, step.getId());
stepNode.setAttribute(PARENT, job.getId());
stepNode.setAttribute(NAME, step.getName());
tree.add(stepNode, jobNode);
});
});
ListGridField[] fields = treeGrid.getAllFields();
assetsDataSourceIndex += 1;
ClientDataSource clientDataSource = new ClientDataSource(fields, assetsDataSourceIndex, true);
clientDataSource.setCacheData(tree.getAllNodes());
treeGrid.setDataSource(clientDataSource);
treeGrid.fetchData();
treeGrid.setFields(fields);
}
private void addTreeGridHandler() {
getActionUtil().addHandler(treeGrid.addDrawHandler(event -> treeGrid.getBody()
.setCanSelectText(true)));
treeGrid.addDataArrivedHandler((DataArrivedHandler) dataArrivedEvent -> {
SC.logWarn("Data arrived");
if (dataLoaded && !selectionChanged) {
selectPreviousSelectedAssetInTreeGrid();
}
});
getActionUtil().addHandler(treeGrid.addSelectionUpdatedHandler(this::selectionChanged));
}
private void selectionChanged(BrowserEvent selectEvent) {
SC.logWarn(selectEvent.getSource().toString());
TreeNode selectedRecord = treeGrid.getSelectedRecord();
boolean jobSelected = isJobSelected();
SC.logWarn("Selected record existed: " + (selectedRecord != null));
...
}
Could anybody help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试重新创建您的问题(因为我无法运行您的代码),但我没有得到完全相同的行为。
相反,仅当取消选择 TreeGrid 中的记录(通过按住 Ctrl 键单击它)时,所选记录才为空。 这可能是您第二次单击该记录时发生的情况吗?第三次单击它会发生什么?
也许您可以在较小的可运行示例和帖子中重新创建您的问题。或者您可以从我的示例开始并模仿您的应用程序以查看问题何时发生。也许您甚至会找出它发生的原因。
因为问题可能是由于您的代码中的某些特定内容而发生的,可能是由于您的示例未显示的方法。
I tried to recreate your problem (because I couldn't run your code), but I didn't get the exact same behavior.
Instead the selected record is only null when deselecting a record in the TreeGrid (by ctrl-clicking it). Could that be something that happens the second time you click the record? What happens the third time you click it?
Perhaps you can recreate your problem in a smaller runnable example and post. Or you can start from my example and mimic your application to see when the problem occurs. Perhaps you'll even find out why it occurs.
Because the problem probably occurs because of something specific in your code, perhaps in the methods not shown by your example.