gpt4 book ai didi

java - 瓦丁。多选网格在空时显示选中的复选框

转载 作者:太空宇宙 更新时间:2023-11-03 22:51:40 25 4
gpt4 key购买 nike

我正在使用 7th Vaadin 并看到以下问题:当网格为空时,它的多选复选框被打开:

enter image description here

如何解决?

谢谢。

最佳答案

你应该能够在没有元素时隐藏它,并在有一点hackingtheme时显示它。更新。黑客是必需的,因为即使复选框被隐藏,点击该空间时仍然会触发监听器,因此在删除所有元素、点击它并添加新元素后,即使没有真正的选择,它也可能看起来被选中。

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) 结果

Grid hack for multi row selection

关于java - 瓦丁。多选网格在空时显示选中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39787549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com