Reputation: 933
I'm building a simple CRUD application with a MySql database as back end. So far I managed to link a grid to the contents of a SQLContainer with the FreeformQuery (so the read is fine).
String query = "select a.id, a.name name, b.name type from asset a join " +
" assettype b on a.assettype_id = b.id";
grid.setContainerDataSource(container);
For the container the SQLContainer. I created a form and I did the binding between its contents and a selected row
grid.addSelectionListener(event -> {
if (event.getSelected().isEmpty()) {
form.setVisible(false);
} else {
Item item = container.getItem(event.getSelected().iterator().next());
form.setAsset(item);
}
});
So, as you see, the form is linked to an Item. setAsset is simply a form method that links the row contents to the text fields of the form.
public void setAsset(Item item) {
this.item = item;
FieldGroup binder = new FieldGroup(this.item);
binder.bind(textField1, "name");
binder.bind(textField2, "type");
setVisible(true);
}
Well, I don't know anymore how to add a row, or how to edit an existing one. For editing a row I tried with a save method for the form (that I call with a button) as follows
private void save() throws UnsupportedOperationException, SQLException {
SQLContainer container = db.getContainer();
container.addItem(item);
container.commit();
}
Well, problem one is that, if I select a grid item, I see the contents of name and type in the text fields but, if I modify their values in the text field before entering the save method, this.item still has the original values (when I thought it would take the new value in the text field because the textfields are bound to the RowItem). Does anybody know what's going on?
In addition, if I want to create a new row, I would like to have something like this
Button createAsset = new Button("Add asset");
createAsset.addClickListener(e ->
{
grid.select(null);
form.setAsset(new Item());
});
and fill the contents of the blank Item() in the form, before pushing it to the table. Of course I can't because Item and RowItem are interfaces. So how can I instantiate an empty row of a container before filling its contents?
Thanks very much in advance.
Upvotes: 0
Views: 77
Reputation: 933
In the end, I would say that either I din't use the FieldGroup.bind method properly, or that there is a bug in it. This is my original entry
public void setAsset(Item item) {
this.item = item;
FieldGroup binder = new FieldGroup(this.item);
binder.bind(textField1, "name");
binder.bind(textField2, "type");
setVisible(true);
}
which did not work, and this is what works (which to me, is the same)
public void setAsset(Item item) {
this.item = item;
textField1.setPropertyDataSource(item.getItemProperty("name"));
textField2.setPropertyDataSource(item.getItemProperty("type"));
setVisible(true);
}
Upvotes: 0