Grid SelectionMode.MULTI缺少标题复选框以选择BackEndDataProvider的全部

时间:2017-07-18 21:09:22

标签: vaadin-grid vaadin8

我正在使用一个写入版本8的新应用程序(目前正在使用8.1.0.rc2进行测试)。

使用Grid时,问题会围绕SelectionMode.MULTI标题中显示的“全选”复选框。特别是,问题是当DataProvider实施InMemoryDataProvider时,复选框会按预期显示并按预期运行,但DataProvider实施BackEndDataProvider后,复选框不会显示。

以下代码创建了两个网格,这些网格的区别仅在于它们是使用InMemory还是BackEnd:

public class Test {
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    private String name;
}

public class TestView extends BaseView {
    public TestView() {
        super("Test");
        addComponent(new TestGrid(new TestDataProvider0()));
        addComponent(new TestGrid(new TestDataProvider1()));
    }
}

public class TestGrid extends Grid<Test> {
    public TestGrid(DataProvider<Test, ?> dataProvider) {
        setHeightByRows(4);
        setSelectionMode(SelectionMode.MULTI);
        setDataProvider(dataProvider);
        addColumn(Test::getName).setCaption("Name");
    }
}

public class TestDataProvider0 extends AbstractDataProvider<Test, SerializablePredicate<Test>> implements
        BackEndDataProvider<Test, SerializablePredicate<Test>> {
    public Stream<Test> fetch(Query<Test, SerializablePredicate<Test>> query) {
        List<Test> tests = new ArrayList<>(query.getLimit());
        for (int i = 0; i < query.getLimit(); i++) {
            Test test = new Test();
            test.setName(String.valueOf(query.getOffset() + i));
            tests.add(test);
        }
        return tests.stream();
    }
    public int size(Query<Test, SerializablePredicate<Test>> query) {
        return 100;
    }
    public void setSortOrders(List<QuerySortOrder> sortOrders) {
    }
}

public class TestDataProvider1 extends AbstractDataProvider<Test, SerializablePredicate<Test>> implements
        InMemoryDataProvider<Test> {
    public Stream<Test> fetch(Query<Test, SerializablePredicate<Test>> query) {
        List<Test> tests = new ArrayList<>(query.getLimit());
        for (int i = 0; i < query.getLimit(); i++) {
            Test test = new Test();
            test.setName(String.valueOf(query.getOffset() + i));
            tests.add(test);
        }
        return tests.stream();
    }
    public int size(Query<Test, SerializablePredicate<Test>> query) {
        return 100;
    }
    public SerializablePredicate<Test> getFilter() {
        return null;
    }
    public void setFilter(SerializablePredicate<Test> filter) {
    }
    public SerializableComparator<Test> getSortComparator() {
        return null;
    }
    public void setSortComparator(SerializableComparator<Test> comparator) {
    }
}

以下是网格的渲染方式: Screenshot

我是否错过了设置基于BackEnd的数据提供商/网格的关键步骤? related documentation似乎无法解决此问题。

是否存在与此相关的已知问题?

选择 - 所有设计都不可用吗?显然,这可能与大型数据集上的延迟加载概念非常相互影响......

1 个答案:

答案 0 :(得分:1)

MultiSelectionModelImpl有这个方法:

protected void updateCanSelectAll() {
    switch (selectAllCheckBoxVisibility) {
    case VISIBLE:
        getState(false).selectAllCheckBoxVisible = true;
        break;
    case HIDDEN:
        getState(false).selectAllCheckBoxVisible = false;
        break;
    case DEFAULT:
        getState(false).selectAllCheckBoxVisible = getGrid()
                .getDataProvider().isInMemory();
        break;
    default:
        break;
    }
}

这表示非内存提供程序的默认行为是不显示select-all复选框,但可以通过将可见性设置为VISIBLE来覆盖此行为。

在此调整原始代码:

public class TestGrid extends Grid<Test> {
    public TestGrid(DataProvider<Test, ?> dataProvider) {
        setHeightByRows(4);
        MultiSelectionModel<Test> selectionModel = (MultiSelectionModel<Test>) setSelectionMode(SelectionMode.MULTI);
        selectionModel.setSelectAllCheckBoxVisibility(SelectAllCheckBoxVisibility.VISIBLE);
        setDataProvider(dataProvider);
        addColumn(Test::getName).setCaption("Name");
    }
}

具体来说,为实现BackEndDataProvider的数据提供者显示复选框需要进行此调用:

MultiSelectionModel<Test> selectionModel = (MultiSelectionModel<Test>) setSelectionMode(SelectionMode.MULTI);
selectionModel.setSelectAllCheckBoxVisibility(SelectAllCheckBoxVisibility.VISIBLE);

通过此更改,现在会显示全选复选框: Fixed screenshot