【问题标题】:How can I display checkboxes instead of boolean in vaadin grid?如何在 vaadin 网格中显示复选框而不是布尔值?
【发布时间】:2022-01-07 13:10:24
【问题描述】:
【问题讨论】:
标签:
java
vaadin
vaadin-flow
vaadin21
【解决方案1】:
我正在使用一些超旧版本的 vaadin,但我想相同的解决方案也应该适用于最新版本,这是我通常的做法:
-
String 类型的列
- 列添加了带有事件监听器的
ButtonRenderer (column.setRenderer) 渲染器,它允许我处理用户的点击操作
- 值将是一个带有 FontAwesome 字体的字符串(添加了自定义 CSS 样式
font-family 并使用 setCellStyleGenerator 在网格中配置它)
- 检查时使用值
String.valueOf((char) FontAwesome.CHECK_SQUARE_O.getCodepoint())
- 未选中时使用值
String.valueOf((char) FontAwesome.SQUARE_O.getCodepoint())
使用较新版本的 vaadin 可能有更简单的方法。但是,这种方法应该仍然有效。
【解决方案2】:
对于 Vaadin 14 及更高版本,您可以这样做。我没有使用旧版本 Vaadin 的经验。
grid.addComponentColumn(myBean -> {
Checkbox checkbox = new Checkbox();
checkbox.setValue(true); // this you must handle
checkbox.addClickListener(e -> {
// todo
}
return checkbox;
});
lambda 可以替换为方法引用
grid.addComponentColumn(this::createCheckboxComponent);
private Component createCheckboxComponent(MyBean myBean) {
Checkbox checkbox = new Checkbox();
// code here
return checkbox;
}