如何根据其他列值更改在生成的列中更改select的选定值

时间:2013-03-13 16:06:06

标签: java select listener vaadin

在表格中,有两列,每一列都有一个下拉列表。

final Table archivesTable = new Table();
archivesTable.setTableFieldFactory(new TableFieldFactory() {
    @Override
    public Field createField(Container container, Object itemId,
                             Object propertyId, Component uiContext) {
        Select sel = new Select();
        try (SSEConnector c = CisApplication
                              .generateSseConnector((SseConnection) sseSetupSelect
                              .getValue())) {
            for (String s : c.getListOfArchives())
                sel.addItem(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("yes 2");
        return sel;
    }
});

archivesTable.addGeneratedColumn("reportName",
    new Table.ColumnGenerator() {
        @Override
        public Object generateCell(Table source, final Object itemId, Object columnId) {
            reportSelect = new Select();
            try (SSEConnector c = CisApplication
                                  .generateSseConnector((SseConnection) sseSetupSelect
                                  .getValue())) {
                for (String s : c.getListOfArchives())
                    reportSelect.addItem(s);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("yes 3");
            return reportSelect;
        }
    }
);

正如我们所看到的,我想要从表格第1列中的第一个“选择”进行任何值更改,以影响表格第二列中第二个“选择”的值。怎么样?

1 个答案:

答案 0 :(得分:0)

生成的列不会影响vaadin表的基础数据容器。它们是在飞行中生成的,并且没有为它们创建属性。因此,没有明显的方法可以直接访问它们。

实现此行为的丑陋方法是为两个列预生成所有控件并将它们绑定在一起。将它们存储在绑定到itemId的地图中:

Collection itemIds = archivesTable.getItemIds();
final HashMap<Object,Select> firstColumnSelects = new HashMap<Object, Select>();
final HashMap<Object,Select> secondColumnSelects = new HashMap<Object, Select>();
for(Object itemId : itemIds) {
    final Select secondColumnSelect = new Select();
    //initialize select for the second column
    secondColumnSelect.setImmediate(true);
    Select firstColumnSelect = new Select();
    //initialize select for the first column
    firstColumnSelect.addListener(new ValueChangeListener() {

    @Override
    public void valueChange(ValueChangeEvent event) {
        //setting value to the list in second column
        secondColumnSelect.setValue(event.getProperty().getValue());
    }
    });
    firstColumnSelect.setImmediate(true);
    firstColumnSelects.put(itemId, firstColumnSelect);
    secondColumnSelects.put(itemId, secondColumnSelect);
}

然后,不要动态生成控件,只需返回预先生成的列表:

//...
@Override
public Field createField(Container container, final Object itemId, Object propertyId, Component uiContext) {
    return firstColumnSelects.get(itemId);
}
//...
@Override
public Object generateCell(Table source, final Object itemId, Object columnId) {
    return secondColumnSelects.get(itemId);
}

清洁方法根本不使用生成的列。生成的列并不意味着大量参与数据操作。相反,您可以创建一个单独的类来表示表记录并实现其中的所有交互:

public class TableDisplayBean {
    private Select firstSelect;
    private Select secondSelect;

    public TableDisplayBean() {
        //initialization of controls, value change handling goes here
    }

    public Select getFirstColumn() {
        return firstSelect;
    }
    public Select getSecondColumn() {
        return secondSelect;
    }
}

剩下的就是使用BeanItemContainer将一组bean绑定到表。