Programmatically select a row in Grid in Vaadin 7?

早过忘川 提交于 2019-12-23 01:14:07

问题


In the Grid widget in Vaadin 7.5.3, we can determine the current selection of rows by calling SelectionEvent::getSelected or Grid::getSelectedRows.

So how do we set the selection programmatically?


回答1:


While that's true that official documentation for Grid class doesn't have this method stated, still you can do it programmatically. I won't argue whether it's a bug or not. Firstly you need to know what is your SelectionMode. Then you can select a row (or rows):

@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);
}



回答2:


In newer Vaadin (in my case 7.5.6) there is select(Object) method directly in Grid interface.

Example:

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

The row object for example could be taken from SelectionListener event or from added before object (as in @kukis answer).




回答3:


Setter Method Missing (bug?)

The Book of Vaadin mentions the setter method Grid::setSelectedRows along with a getter.

The currently selected rows can be set with setSelectedRows() by a collection of item IDs, and read with getSelectedRows().

However, the Grid class doc does not list that method. Nor does NetBeans 8.0.2 suggest that method in its auto-complete.

So apparently a bug. See Ticket # 18,580.



来源:https://stackoverflow.com/questions/31900677/programmatically-select-a-row-in-grid-in-vaadin-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!