vaadin 7.75 How to add combobox to a grid?

旧时模样 提交于 2019-12-24 11:47:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!