Good morning to all,
I started from a few days to use Vaadin and I’m a beginner. Here’s my problem:
I can not make it editable a single record in a table. Basically I created a table from a datasource and I added this to a column containing a checkbox for each row. I wish that after selecting the checkbox corresponding record become editable.
I post the code:
public void enter(ViewChangeEvent event) {
ContactDao dao = new ContactDao();
SQLContainer contacts= dao.selectAllContcts(TestUI.getDb());
final Table t = new Table("contacts");
t.setContainerDataSource(contacts);
t.setWidth("50%");
t.setSelectable(true);
t.setImmediate(true);
t.addGeneratedColumn("edit",new CheckBoxColumnGenerator());
t.setColumnHeaders(new String[] { "Id",
"Name","Surname", "Number of telephone", "Edit"});
t.setPageLength(5);
t.setColumnReorderingAllowed(true);
// final CheckBox switchEditable = new CheckBox("Editable");
// switchEditable.addValueChangeListener(
// new Property.ValueChangeListener() {
// public void valueChange(ValueChangeEvent event) {
// t.setEditable(((Boolean)event.getProperty()
// .getValue()).booleanValue());
// }
// });
// switchEditable.setImmediate(true);
// layout.addComponent(switchEditable);
layout.addComponent(t);
addComponent(layout);
//
}
[code]
public class CheckBoxColumnGenerator implements ColumnGenerator {
@SuppressWarnings("rawtypes")
@Override
public Object generateCell(final Table source, final Object itemId, Object columnId) {
Property prop = source.getItem(itemId).getItemProperty("edit");
CheckBox c =new CheckBox(null, prop);
return c;
}
}
[/code]In this way I built my table but I can not figure out how to make a single row editable.
In Table, you can only set the entire table to be editable with setEditable(). However, you can implement row-based editing by having a TableFieldFactory that generates editable fields only for the selected row, or it generates read-only fields and you set the fields writable when a row is selected/clicked.
Grid, on the other hand, only supports row-based editing.
Mmm, yeah, that looks like one way to do it. Returning null in the field factory results in read-only text, so no need to return a read-only field there, as I suggested earlier. The Edit/Save button’s functionality also looks good. Using the component’s data field is a one way to communicate that sort of things, although it if course reserves it for that use; using a member variable is probably more type-safe way.