我是 vaadin 新手。我有一个电子表格组件,在 Springboot (Java) 的 A 列和 B 列中有两个组合框。我通过电子表格 setSpreadsheetComponentFactory 添加了这些组合框。我想要的是根据用户在 A 列中选择的值进行 db/api 调用来获取并加载 B 列中的组合框数据。根据在中选择的值,每行中的数据可能会有所不同该特定行的 A 列。如何更新/监听 A 列组合框中的值更改以更新 B 列中的值?
一种选择是在 SpreadsheetComponentFactory 中执行类似以下操作。由于您已经在使用它,因此您已经实现了 getCustomComponentForCell。在该方法中,您可以创建组合框并将其存储为引用。例如下面这样。
@Override
public Component getCustomComponentForCell(Cell cell,
int rowIndex, int columnIndex,
Spreadsheet spreadsheet, Sheet sheet) {
if (spreadsheet.getActiveSheetIndex() == 0
&& rowIndex == 2 && columnIndex == 1) {
if (cb1 == null) {
initCombobox1();
}
return cb1;
}
if (spreadsheet.getActiveSheetIndex() == 0
&& rowIndex == 2 && columnIndex == 2) {
if (cb2 == null) {
cb2 = new ComboBox<>();
}
return cb2;
}
return null;
}
然后在第一个 ComboBox (cb1) 的 init 方法中,您可以分配一个值更改侦听器,它将根据第一个 ComboBox (cb2) 的值填充另一个 ComboBox (cb2) 的项目:
private void initCombobox1() {
cb1 = new ComboBox<>();
cb1.setItems("value1", "value2");
cb1.addValueChangeListener(e -> {
if ("value1".equals(e.getValue())) {
cb2.setItems("value1_1", "value1_2");
} else {
cb2.setItems("value2_1", "value2_2");
}
});
}
这显然是带有硬编码值的简化示例。在现实生活中,您可以调用 API 来获取这些项目。