Basil Bourque
Basil Bourque

Reputation: 338604

Programmatically select a row in Grid in Vaadin 7?

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?

Upvotes: 3

Views: 5667

Answers (3)

jsosnowski
jsosnowski

Reputation: 1590

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).

Upvotes: 4

kukis
kukis

Reputation: 4634

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

Upvotes: 7

Basil Bourque
Basil Bourque

Reputation: 338604

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.

Upvotes: 0

Related Questions