[英]How to change the label of a property from an action (programmatically in runtime) in OpenXava?
[英]Displayed data cleared after changing view with getView().setViewName() from an action in OpenXava
我使用 setViewName() 从操作更改视图并且它可以工作,但是在更改视图后,所有显示的数据都消失了,所有字段都为空白。
这是我的操作代码:
public void execute() throws Exception {
// ...
// Here there is data displayed in the view
if (article.getManagedBy() == ManagedBy.BATCH) {
// Batch is defined as @View(name="Batch", members="...") in the entity
getView().setViewName("Batch");
}
else if (article.getManagedBy() == ManagedBy.SERIE) {
// Serie is defined as @View(name="Serie", members="...") in the entity
getView().setViewName("Serie");
}
// Now no data is displayed though the view has changed
}
如何在保留显示数据的同时更改视图?
当您调用 View.setViewName() 时,视图数据被清除。 这是因为 OpenXava 只加载显示的数据,所以如果您更改为显示更多数据的视图,如果我们保留数据,那么当这些字段应该具有值时,您可能会得到一些空白字段。
因此,您有责任在更改视图后恢复数据。 如果您确定 Batch 和 Serie 视图显示完全相同的数据(可能具有不同的布局),那么您可以在更改视图之前获取数据,然后再将其放回原处。 这样:
// We get the view values before change the view
Map values = getView().getValues();
if (article.getManagedBy() == ManagedBy.BATCH) {
getView().setViewName("Batch");
}
else if (article.getManagedBy() == ManagedBy.SERIE) {
getView().setViewName("Serie");
}
// Then we put the values back again
getView().setValues(values);
但是,如果视图显示不同的数据,则上述技术不能很好地工作。 在这种情况下,您应该获取实体的键,更改视图,将键恢复到视图中并从数据库中刷新视图。 像这样的东西:
// We get the key before changing the view
Map key = getView().getKeyValues();
if (article.getManagedBy() == ManagedBy.BATCH) {
getView().setViewName("Batch");
}
else if (article.getManagedBy() == ManagedBy.SERIE) {
getView().setViewName("Serie");
}
// Then we put the key back again
getView().setValues(key);
// And get the data from the database from the key
getView().findObject();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.