【问题标题】:Programmatically select a row in Grid in Vaadin 7?在 Vaadin 7 中以编程方式在 Grid 中选择一行?
【发布时间】:2015-11-01 06:04:21
【问题描述】:

在Vaadin 7.5.3 的Grid 小部件中,我们可以通过调用SelectionEvent::getSelected 或Grid::getSelectedRows 来确定当前选择的行。

那么我们如何以编程方式设置选择?

【问题讨论】:

标签: vaadin vaadin7 vaadin-grid


【解决方案1】:

在较新的 Vaadin(在我的情况下为 7.5.6)中,select(Object) 方法直接在 Grid interface 中。

例子:

Grid grid = new Grid(container);
grid.setSelectionMode(Grid.SelectionMode.SINGLE);
grid.select(row);

例如,row 对象可以取自 SelectionListener 事件或添加到对象之前(如 @kukis 回答)。

【讨论】:

    【解决方案2】:

    虽然Grid class 的官方文档确实没有说明这种方法,但您仍然可以通过编程方式进行。我不会争论它是否是一个错误。首先你需要知道你的SelectionMode 是什么。然后你可以选择一行(或多行):

    @Override
    protected void init(VaadinRequest request) {
        final VerticalLayout layout = new VerticalLayout();
        Customer c = new Customer(1);
        container = new BeanItemContainer<>(Customer.class, Arrays.asList(c, new Customer(2)));
        grid = new Grid(container);
        grid.setSelectionMode(SelectionMode.SINGLE);
        SingleSelectionModel m  = (SingleSelectionModel) grid.getSelectionModel();
        m.select(c);
        layout.addComponents(grid);
        setContent(layout);
    }
    

    【讨论】:

      【解决方案3】:

      Setter 方法缺失(错误?)

      Vaadin 之书提到了 setter 方法 Grid::setSelectedRows 以及 getter。

      当前选中的行可以用 setSelectedRows() 通过项 ID 的集合来设置,并用 getSelectedRows() 读取。

      但是,Grid 类文档没有列出该方法。 NetBeans 8.0.2 也没有在其自动完成中建议该方法。

      所以显然是一个错误。请参阅票证#18,580。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-08
        • 2018-01-23
        • 2020-11-08
        • 1970-01-01
        • 1970-01-01
        • 2011-09-10
        • 1970-01-01
        • 2019-01-13
        相关资源
        最近更新 更多