Vaadin - 行修改后刷新网格 [英] Vaadin - Refresh grid after row modification
问题描述
我使用数据库中的数据创建简单网格:
I create simple grid with data from database:
BeanItemContainer<Customer> container = new BeanItemContainer<>(Customer.class, customerRepository.findAll());
Grid grid = new Grid(container);
要编辑按钮的每一行:
Button edit = new Button("Edit", clickEvent -> openWindow((Customer) grid.getSelectedRows().iterator().next()));
这个打开的新窗口带有编辑表格。接受所有更改后,我必须手动刷新整个页面以查看Grid上的修改。我的问题是:
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:
修改任何行条目后如何刷新网格?
以及如何将这些修改保存到数据库(也许beanItemContainer可以做到)?
How refresh only Grid after modification of any row entry? And how save those modifications to database (maybe beanItemContainer could do it)?
推荐答案
这是一个错误。在底层容器中进行更改后,Grid不会自行更新,也没有任何合理的刷新方法。围绕这个问题有几个黑客,即
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();
或
grid.setEditorEnabled(true);
grid.setEditorEnabled(false);
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();
}
可能重复使用一组新数据更新网格,在Vaadin 7.4应用程序中
这篇关于Vaadin - 行修改后刷新网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!