Form setWriteThrough(false) - How to update datasource?

Hello Vaadiners,

I have a class that extends a table and another class that extends a form.

Both are added to the same layout and the table is connected to my datasource.

When I select a row on the table, the form gets populated with its contents:

public void valueChange(Property.ValueChangeEvent event) {
		
		Property property = event.getProperty();
		if (property == table) {
			Item item = table.getItem(table.getValue());
			if (item != form.getItemDataSource()) {
				form.setItemDataSource(item);
				form.setReadOnly(true);
			}
		}
	}

So, when I change an attribute on the form the table is updated automatically.

But I need to update the datasource only after a button “Save” gets clicked:

public void buttonClick(ClickEvent event) {
	
		final Button source = event.getButton();

		if (source == editButton) {
			setReadOnly(false);
		} 
		else if (source == cancelButton) {
			setReadOnly(true);
			discard();
		} 
		else if (source == saveButton) {
			logger.debug("isModified(): " + isModified());

			setReadOnly(true);
			
			try {
				commit();
			} catch (InvalidValueException invException) { 
				logger.error("Trade contains invalid values: " + invException.getMessage());
			} catch (SourceException srcException) {
				logger.error("Error committing changes to trade: " + srcException.getMessage());
			}
		}
	}

To achieve this i used form.setWriteThrough(false).

:what: The problem is that modifications in the form are not being passed to the table.

Is this approach correct? What should I do in order to update data on the table?

Thanks in advance,
Diogo

Form.commit() does not seem to work.

If I use the method Form.getItemDataSource().getItemProperty(“PropertyName”).getValue() after calling Form.commit(), the old value is returned.

Form.getItemDataSource().getItemProperty(“PropertyName”).setValue(“New Value”) updates the value automatically on the table.

What is preventing Form.commit() from updating the data source?

Solved:

Turns out that commit() is not called if Form is setReadOnly(true).

I changed the order of my save button click to commit() first and then set the form to read-only.

		else if (source == saveButton) {
			logger.debug("isModified(): " + isModified());

			try {
				this.commit();               // <-- This must be called before setting read only
				setReadOnly(true);
			} catch (InvalidValueException invException) { 
				logger.error("Trade contains invalid values: " + invException.getMessage());
			} catch (SourceException srcException) {
				logger.error("Error committing changes to trade: " + srcException.getMessage());
			}
		}

Now everything is working fine…