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

标签 java css vaadin vaadin7 vaadin-grid

我正在使用 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/

相关文章:

java - 在 vaadin 框架中没有获取 sessin 值

java - Java中的串行通信

具有前端的 Java 简单分析/事件流处理

java - 如何在线性时间内找到 2-sum?

javascript - 使用 Javascript 更改基于 getMonth() 的链接类

java - vaadin 的 Invient 图表最大数据大小

java - 启动tomcat 7抛出两个异常,End事件和web.xml解析

css - bootstrap 3 中的全高列

jquery - 合并 Foundation 后动画突然不起作用?

java - Vaadin 小部件集编译失败