限制Vaadin表中选定行的数量

时间:2015-11-13 15:22:27

标签: java vaadin

在我们的一个应用程序中,我们使用惰性查询容器来浏览可能非常大的数据集。这非常有效。但是,当使用多选表时,可以选择任意数量的行。 在我们的例子中,这可能导致选择最多500.000行(Vaadin限制),然后崩溃VM。

有没有办法限制所选行的数量?

以下是显示问题的示例:

public class UIImpl extends UI {
    private int SIZE = 500000;

    @Override
    protected void init(VaadinRequest request) {

        // add a large table
        LazyQueryContainer lqc = new LazyQueryContainer(
            new QueryFactory() {
                public Query constructQuery(QueryDefinition qd) {
                    return new Query() {
                        @Override
                        public int size() {
                            return SIZE;
                        }

                        @Override
                        public void saveItems(List<Item> addedItems, List<Item> modifiedItems, List<Item> removedItems) {   }

                        @Override
                        public List<Item> loadItems(int startIndex, int  count) {
                            List<Item> r = new ArrayList<>(count);
                            for (int i = startIndex; i<startIndex+count;i++) {
                                PropertysetItem item = new PropertysetItem();
                                item.addItemProperty("name", new ObjectProperty(i));
                                r.add(item);
                            }
                            return r;
                        }

                        @Override
                        public boolean deleteAllItems() {
                            return false;
                        }

                        @Override
                        public Item constructItem() {
                            return null;
                        }
                    };
                }
            },
            null,
            20,
            false
        );

        lqc.addContainerProperty("name", Integer.class, null);

        Table table = new Table();
        table.setContainerDataSource(lqc);
        table.setMultiSelect(true);
        table.setSelectable(true);
        table.setImmediate(true);
        table.setVisibleColumns("name");
        table.setSizeFull();
        table.addValueChangeListener(new Property.ValueChangeListener() {
            public void valueChange(Property.ValueChangeEvent event) {
                System.err.println(event.getProperty().getValue());
            }
        });
        setContent(table);
    }
}

1 个答案:

答案 0 :(得分:4)

如果您想限制用户可以选择的行数,您可以使用类似于以下代码的内容:

public class TableWithSelectionLimit extends Table {

    private final int maxSelections= -1;
    private String[] lastSelected;

    public TableWithSelectionLimit(int maxSelections) {
      this.maxSelections = maxSelections;
    }

    @Override
    public void changeVariables(Object source, Map<String, Object> variables) {
      String[] selected = (String[]) variables.get("selected");
      if (selected != null && selected.length > maxSelections) {
        if (lastSelected != null) {
          variables.put("selected", lastSelected);
        } else {
          variables.remove("selected");
        }

        markAsDirty();
      } else {
        lastSelected = selected;
      }

      super.changeVariables(source, variables);
    }
}

这当然是可以优化的,但它可以让你了解如何做到这一点。

<强>更新

用于处理使用&#34; Shift&#34; +单击一个必须在上述方法内另外处理/更新这些选择范围的选择。

可以使用variables.get("selectedRanges")检索这些内容,这些内容会返回包含String[]项的"8-10",而

  • 第一个数字是:选择范围的起始索引
  • 第二个号码是:从此索引开始选择的项目数量

使用此信息,应该可以根据需要更新这些值,并使用variables.put("selectedRanges", updatedRanges)将它们放回变量中。

注意:如果值已更改,请不要忘记调用markAsDirty(),否则更改不会传播到客户端。

相关问题