我正在使用 7th Vaadin 并看到以下问题:当网格为空时,它的多选复选框被打开:
如何解决?
谢谢。
你应该能够在没有元素时隐藏它,并在有一点hacking和theme时显示它。更新。黑客是必需的,因为即使复选框被隐藏,点击该空间时仍然会触发监听器,因此在删除所有元素、点击它并添加新元素后,即使没有真正的选择,它也可能看起来被选中。
1) 主题
.invisible-select-all-checkbox .v-grid-select-all-checkbox {
visibility: hidden;
}
2) 代码
public class GridWithDisabledSelectAllCheckbox extends VerticalLayout {
private static final Logger log = LoggerFactory.getLogger(GridWithDisabledSelectAllCheckbox.class);
public GridWithDisabledSelectAllCheckbox() {
// basic grid setup with multiple selection
BeanItemContainer<Person> container = new BeanItemContainer<>(Person.class);
Grid grid = new Grid();
grid.setContainerDataSource(container);
grid.setSelectionMode(Grid.SelectionMode.MULTI);
// listen for changes in the container
container.addItemSetChangeListener(event -> {
if (event.getContainer().size() == 0) {
// no more items, hide the checkbox
grid.addStyleName("invisible-select-all-checkbox");
} else {
// oo, items, show the checkbox
grid.removeStyleName("invisible-select-all-checkbox");
}
if (grid.getSelectedRows().isEmpty()) {
// although the checkbox is hidden, the listener is still triggered when clicking the header
// thus, when adding items after removing all of them, it may appear checked which is not nice.
// to bypass this we can reset the selection state, if there was nothing previously selected
// if there were items in the grid but they were not selected, it won't change anything so we should be safe
grid.getSelectionModel().reset();
}
});
// add some dummy data
for (int i = 0; i < 10; i++) {
container.addBean(new Person(i));
}
HorizontalLayout buttonsLayout = new HorizontalLayout();
// add some other dummy data
buttonsLayout.addComponent(new Button("Add", event -> container.addItem(new Person(container.size()))));
buttonsLayout.addComponent(new Button("Remove", event -> {
// remove selected items, if any
if (!grid.getSelectedRows().isEmpty()) {
new ArrayList<>(grid.getSelectedRows()).forEach(row -> {
// unfortunately, it seems that removing a row doesn't also deselect it
// so the hack for resetting the selection is not complete without this part
grid.deselect(row);
container.removeItem(row);
});
}
}));
addComponent(grid);
addComponent(buttonsLayout);
}
// basic bean
public static class Person {
private String name;
private String surname;
public Person(int index) {
this.name = "name " + index;
this.surname = "surname" + index;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
}
3) 结果
我是一名优秀的程序员,十分优秀!