问题
I'm on Vaadin 7.7 and I switch tables to the grid. Only I can not personalize my cells as I would like. Here I would like to add comboboxes on a column from an arraylist and retrieve the chosen value.
Here's some of my code:
Here I create my IndexedContainer
IndexedContainer indexedContainer = new IndexedContainer();
indexedContainer.addContainerProperty("Type de véhicule",String.class,"");
Here I add my items:
indexedContainer.addItem(listValue);
indexedContainer.getContainerProperty(listValue,
key.get(0)).setValue(
String.valueOf(listValue.get(0)));
Finally I put my object in editable and I use this function to do actions during the backup:
grid.getEditorFieldGroup().addCommitHandler(new FieldGroup.CommitHandler() {
@Override
public void preCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
}
@Override
public void postCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
If you have any ideas or suggestions do not hesitate :)
Good night
回答1:
You could use something like this:
List<String> values = obtainValues();
IndexedContainer container = new IndexedContainer();
//Add other properties...
container.addContainerProperty("comboBox", ComboBox.class, null);
//Do more stuff
ComboBox cb = new ComboBox();
cb.addItems(values);
item.getItemProperty("comboBox").setValue(cb);
And, in grid declaration, you may use an addon that allows grid to render components.
Grid grid = new Grid();
//Even more stuff
grid.setContainerDataSource(container);
grid.getColumn("comboBox").setRenderer(new ComponentRenderer());
To obtain the value of the comboBox:
Item item = container.getItem(itemId);
ComboBox cb = item.getItemProperty("comboBox").getValue();
String value = (String) cb.getValue();
回答2:
Try this:
grid.getColumn("state").setEditorField(getComboState());
where getComboState is:
private Field<?> getComboState() {
ComboBox comboBox = new ComboBox();
comboBox.addItem("approve");
comboBox.addItem("no-approve");
comboBox.setImmediate(true);
comboBox.setNullSelectionAllowed(false);
return comboBox;
}
来源:https://stackoverflow.com/questions/44310660/vaadin-7-75-how-to-add-combobox-to-a-grid