Java Vaadin - 行修改后刷新网格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31861375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vaadin - Refresh grid after row modification
提问by jsosnowski
I create simple grid with data from database:
我用数据库中的数据创建简单的网格:
BeanItemContainer<Customer> container = new BeanItemContainer<>(Customer.class, customerRepository.findAll());
Grid grid = new Grid(container);
To edit each row the button was created:
要编辑创建按钮的每一行:
Button edit = new Button("Edit", clickEvent -> openWindow((Customer) grid.getSelectedRows().iterator().next()));
This open new window with edit form. After all changes accepted, I must manually refresh whole page to see modification on Grid. My question is:
这会打开带有编辑表单的新窗口。接受所有更改后,我必须手动刷新整个页面才能查看 Grid 上的修改。我的问题是:
How refresh only Grid after modification of any row entry? And how save those modifications to database (maybe beanItemContainer could do it)?
修改任何行条目后如何仅刷新网格?以及如何将这些修改保存到数据库中(也许 beanItemContainer 可以做到)?
采纳答案by kukis
It's a bug. Grid doesn't update itself after changes were done in underlying container nor has any reasonable method to refresh. There are several hacks around this issue i.e.
这是一个错误。在底层容器中完成更改后,网格不会自行更新,也没有任何合理的刷新方法。这个问题有几个技巧,即
grid.clearSortOrder();
or
或者
grid.setEditorEnabled(true);
grid.setEditorEnabled(false);
SSCCE:
SSCCE:
TextField text = new TextField("Edit");
Grid grid;
BeanItemContainer<Customer> container;
@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
container = new BeanItemContainer<>(Customer.class, Arrays.asList(new Customer("1"), new Customer("2")));
grid = new Grid(container);
Button open = new Button("open");
open.addClickListener(this :: openListener);
Button save = new Button("save");
save.addClickListener(this :: saveListener);
layout.addComponents(grid, open, save, text);
setContent(layout);
}
private void openListener(Button.ClickEvent clickEvent){
text.setValue(((Customer) (grid.getSelectedRow())).getName());
}
private void saveListener(Button.ClickEvent clickEvent){
Customer c = (Customer) grid.getSelectedRow();
c.setName(text.getValue());
grid.clearSortOrder();
}
Possible duplicate Update Grid with a fresh set of data, in Vaadin 7.4 app
回答by user1411778
Since Vaadin 7.7.4 there is a refreshRows(Object ... itemIds)
and a refreshAllRows()
method in the grid: https://dev.vaadin.com/ticket/16765
从 Vaadin 7.7.4 开始,网格中有refreshRows(Object ... itemIds)
一个refreshAllRows()
方法:https: //dev.vaadin.com/ticket/16765
回答by Harald Hetzner
With Vaadin 8, the following works to refresh the grid after rows have been added or removed or the underlying data has been changed:
使用 Vaadin 8,以下工作可以在添加或删除行或更改基础数据后刷新网格:
grid.getDataProvider().refreshAll();
回答by kosiara - Bartosz Kosarzycki
I had to explicitly callgrid.setItems()
after each call to saveListener
event to make my Vaadin 8.3 Grid refresh data inside the grid. It's strange that the visual grid state does notreflect edited values.
我必须在每次调用事件后显式调用grid.setItems()
,saveListener
以使我的 Vaadin 8.3 Grid 刷新网格内的数据。很奇怪的是,视觉网格的状态并不能反映编辑的值。
Scala implementation:
斯卡拉实现:
var advicesList : mutable.MutableList[Advice] = null
private def initGrid() = {
advicesList = mutable.MutableList(itmemsFromDB: _*)
setItems(advicesList.asJava)
getEditor.addSaveListener((event) => {
val bean = event.getBean
Notification.show("Saved value: " + bean)
// ==== RELOAD GRID ITEMS AFTER EDITION
val oldItemIdx = advicesList.indexOf(advicesList.filter(a => a.id == bean.id).head)
advicesList.update(oldItemIdx, bean)
event.getGrid.setItems(advicesList.asJava)
})
}
回答by Veselin
There is a way to solve the problem much better:
有一种方法可以更好地解决问题:
You have to get the BeanItem
from the container and pass this to your editor window. There you can change the bean itself by asscessing it hrough the BeanItem
s Properies
. This is a very manual work so I use instead always databindg of FieldGroup
- just call bind
(field, "propertyName"); where propertyName is the getter method without get and non capital letter.
您必须BeanItem
从容器中获取 并将其传递给您的编辑器窗口。在那里,您可以通过BeanItem
s访问 bean 来更改 bean 本身Properies
。这是一项非常手动的工作,所以我总是使用 databindg of FieldGroup
- just call bind
(field, "propertyName"); 其中 propertyName 是没有 get 和非大写字母的 getter 方法。