Editable table with generated column

Hi,
i’m using a table with setEditable(true).
All the fields are visualized as input box. All seems to work fine. When an user click on “save” button i get all the data from the table and i write them in a database:


		    IndexedContainer ic = (IndexedContainer) tbl.getContainerDataSource();
		    Iterator itmIds = ic.getItemIds().iterator();

		    while (itmIds.hasNext()) {
			Object id = itmIds.next();
			Item itm = ic.getItem(id);
                        //SAVE value in Database
                        //get value in this way:  itm.getItemProperty(" MY COLUMN ID ").getValue()
                   }

The problem is that i want to use a combo box instead of text field, so that the user can choose a value from a list.
So i generate a custom colum:


	// table on top
	tbl = new Table();
	tbl.setSortDisabled(true);
	tbl.setPageLength(7);
	tbl.setImmediate(true);
	tbl.setEditable(true);
	tbl.setContainerDataSource(getPoiTimeList(poiId)); //generate IndexedContainer reading data from DB
	
	tbl.addGeneratedColumn(" MY COLUMN ID ", new Table.ColumnGenerator() {
	    public Component generateCell(Table source, Object itemId, Object columnId) {
		return getNameComponent(itemId, columnId);
	    }
	});

.....
.....

   private Component getNameComponent(Object itemId, Object columnId) {
	Item itm = tbl.getContainerDataSource().getItem(itemId);
	TimeTypeEnum[] tts = TimeTypeEnum.values();

	ListSelect l = new ListSelect();
	for (int i = 0; i < tts.length; i++) {
	    l.addItem(tts[i]
);
	}
	l.setRows(1);
	l.setMultiSelect(false);
	l.setImmediate(true);
	l.setNullSelectionAllowed(false);

	[b]
[size=5]
l.setPropertyDataSource(itm.getItemProperty(columnId));  // WITH this enabled change in combobox are updated to data source. Without NO!
[/size]
[/b]

	Object colVal = itm.getItemProperty(columnId).getValue();
	if (colVal != null) {
	    l.setValue(TimeTypeEnum.valueOf((String.valueOf(colVal))));
	} else {
	    l.setValue(TimeTypeEnum.TUTTI);
	}

	return l;

    }

The problem is that:

a) if i
don’t
set the propertyDataSource (above in bold) the table correctly present the data from IndexedContainer, so when it appears it is ok. But when i change a value in the combo, the data are not saved to datasource. In fact, when i get value from it this is “freezed” to the initial value.

b) if i set the propertyDataSource, all changes to the combobox are saved to datasource, but the inital value are not the right ones. The combo are all displayed with the “first” value.

My question is… how i can correctly connect my custom generated colum to the table datasource, so that the table always present the right values?

Thanks

Generated columns are not editable as they are “calculated” from other columns. This is the reason why the changes are not stored to the datasource. What you should do is to use setTableFieldFactory() to set your own field factory which creates combobox instead of textfield.

Hi Jarno,
thanks for answer!
I solved my problem creating this custom class:

public class TableBeanItemContainer implements Container, Indexed

This custom class manage a list of beanItem so i can bind my beans (business logic) with the datasource of the table.

Also for generated column i must set the property datasource:

l.setPropertyDataSource(itm.getItemProperty(columnId));

In this way all works fine!

:slight_smile: