我正在使用Vaadin爲單個數據庫表構建簡單編輯器,使用JPAContainer和Vaadin表組件。刪除和編輯項目工作正常,但我在添加項目時遇到問題。如何在使用JPAcontainer時在Vaadin表中創建新的內聯項?
Vaadin教程解釋瞭如何使用彈出窗口添加項目,但我希望通過在表格中使用新的空白行來添加項目。所需的功能是爲用戶提供一行默認數據,然後允許用戶在對新數據中的數據感到滿意後保存數據。
我遇到的問題是我試圖做我認爲應該工作時得到UnsupportedOperation。
我的表是建立並綁定到一個JPAContainer的位置實體:
private JPAContainer locations = JPAContainerFactory.make(Location.class,PERSISTENCE_UNIT);
我設定的表進行緩衝,所以我不保存數據到數據庫,直到用戶點擊保存按鈕:
locationTable = new Table(null,locations);
locationTable.setVisibleColumns(new Object[]{ "id","x","y","z","createDate","lastModDate" });
locationTable.setSelectable(true);
locationTable.setSizeFull();
locationTable.setImmediate(true);
locationTable.setBuffered(true);
locationTable.setVisible(true);
locationTable.setEditable(true);
..保存按鈕將更改保存到數據庫中:
Button saveButton = new Button("Save Changes");
saveButton.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
locations.commit();
statusLabel.setCaption("Changes Saved");
}
});
,然後綁定一個嘗試添加新按鈕的按鈕。
Button addButton = new Button("Add An Item");
addButton.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
Object newkey = locationTable.addItem(new Object[]{"NEWLOCATION","0","0","0","7/10/2013","7/10/2013"}, "NEWLOCATION");
locationTable.select(newkey);
statusLabel.setCaption("Item Added. Populate Data and click Save to make permanent.");
}
});
點擊按鈕的AddItem拋出UnsupportedOperationException。
我已經嘗試調用addItem的其他變體,我無法獲得任何工作。有人知道如何使用表中的新行在表中創建新項目?
完整的init()來源:
@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
final Label statusLabel = new Label("");
locationTable = new Table(null,locations);
locationTable.setVisibleColumns(new Object[]{ "id","x","y","z","createDate","lastModDate" });
locationTable.setSelectable(true);
locationTable.setSizeFull();
locationTable.setImmediate(true);
locationTable.setBuffered(true);
locationTable.setVisible(true);
locationTable.setEditable(true);
Button saveButton = new Button("Save Changes");
saveButton.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
locations.commit();
statusLabel.setCaption("Changes Saved");
}
});
Button addButton = new Button("Add An Item");
addButton.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
Object newkey = locationTable.addItem(new Object[]{"NEWLOCATION","0","0","0","7/10/2013","7/10/2013"}, "NEWLOCATION");
locationTable.select(newkey);
statusLabel.setCaption("Item Added. Populate Data and click Save to make permanent.");
}
});
Button deleteButton = new Button("Delete Selected");
deleteButton.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
locations.removeItem(locationTable.getValue());
statusLabel.setCaption("Item Removed. Click Save to make permanent.");
}
});
layout.addComponent(locationTable);
layout.addComponent(saveButton);
layout.addComponent(addButton);
layout.addComponent(deleteButton);
layout.addComponent(statusLabel);
}