empty SQLContainer form

Hi:

I have a form with field factory linked to an SQLContainer that works perfect. Now, when a push a button “add new item”, I want the form to appear empty…how can I do this?

thanks!!!

any idea on this?

Seems like there are some defaults that turn the values to something else than empty. Either go through field by field after and empty the values, or set the defaults so that empty is used.

Hi Jens:

Thank you for your answer…I tried your solution but I have a small problem, what do I set as item data source??..since it is a new item, I want to assign it after the last item of the data source, but I not know that number and I don´t now if it is too efficient to get it in a large table…can I create a “fake” item and let mysql add it?? (the table primary index is “auto increment”)

thanks!!!

SQLContainer.addItem() creates an item with a temporary id (and then stores it if autocommit is on), if storing it later it gets the next id from the DB sequence or such.
Take note of its return value, get the item from the container with getItem(Object) and modify and save it when you want.

Thank you for your answer Henri…

I’m trying this:

Object a = container.addItem();
form.setItemDataSource(container.getItem(a));
addComponent(form);

and it shows only the form footer buttons, since the properties of that item have no value at all…

so I tryed also this:

Object a = container.addItem();
Collection<?> properties = ((Item) a).getItemPropertyIds();
Iterator itr = properties.iterator();
while(itr.hasNext()){	
	container.getItem(a).getItemProperty(itr.next()).setValue(null);
}
form.setItemDataSource(container.getItem(a));
addComponent(form);

and I get a class cast exception:
com.vaadin.data.util.sqlcontainer.TemporaryRowId cannot be cast to com.vaadin.data.Item

any idea on how should I do this?

regards, and thank you very much…

Hugo

The first code snippet was handling the ID correctly, the second isn’t. See the bolded part below for a correction.

Object a = container.addItem();
Collection<?> properties = [b]
container.getItem(a)
[/b].getItemPropertyIds();
Iterator itr = properties.iterator();
while(itr.hasNext()){	
	container.getItem(a).getItemProperty(itr.next()).setValue(null);
}
form.setItemDataSource(container.getItem(a));
addComponent(form);

You’ll probably want to use a temporary variable for container.getItem(a) to avoid calling it many times unnecessarily.

I did it…My problem was that the container I was trying to add the item to was filtered, so the form didn’t show the new item since it didn’t has the filtered value… here’s is what I did:

Object a = container.addItem();
		container.removeAllContainerFilters();
		form.setFormFieldFactory(new PackagesFieldFactory(contenedor.getItem(a)));
		form.setItemDataSource(container.getItem(a));
		addComponent(form);
		container.addContainerFilter("module", "filter1", true, true); //the previous filter
	}

…what I’m trying to do now is that the fields come with no value in them instead of null…

Text fields? If you cannot do this with default values, use a FormFieldFactory and call setNullRepresentation(“”) on the text fields.

just did that and worked perfect…thank you very much for your help henri…

regards,

Hugo