Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

How to get selected item (row) value in Vaadin grid?

Michael Ruiz
5 years ago Oct 27, 2016 6:26pm
Goran Atanasovski
5 years ago Oct 27, 2016 7:14pm

Hi Michael,

With "grid.getSelectedRow()" you will get the item id of the currently selected row (assuming single selection mode) or null if nothing is selected. So, to get the value, you need to retrieve the item from the grid data source, such as:

Object selected = ((SingleSelectionModel) grid.getSelectionModel()).getSelectedRow();
grid.getContainerDataSource().getItem(selected).getItemProperty("Object")).getValue();

Please checkout the grid component documentation for further details.

Regards,
Goran

Michael Ruiz
5 years ago Oct 27, 2016 8:58pm
Goran Atanasovski
5 years ago Oct 28, 2016 5:22am
Nick Papas
4 years ago May 11, 2017 6:35pm

Sorry to bring back an old thread but while this worked fine in vaadin 7 it doesn't seen to work any more with the new grid in vaadin 8. Is there some way to do this now ?

Teemu Suo-Anttila
4 years ago Jul 26, 2017 9:54am

Hi Nick,

I'd recommend using the SelectionModel API for finding the selected item like this:

grid.getSelectionModel().getFirstSelectedItem()

The returned value is an Optional of Grid data type. In the case of Grid in single selection mode, the optional either contains the currently selected row or value is not present. The behaviour is a bit less defined for the multiple selection and grid.getSelectedItems() should be used instead.

The Columns in Grid are defined with a ValueProvider that you can re-use for getting the column value for a specific row at any point:

ValueProvider<MyBean, String> locationProvider = myBean -> myBean.getLocation().toString();
grid.addColumn(locationProvider).setCaption("Location");

/* And later when you need the Location of a selected item: */
grid.getSelectionModel().getFirstSelectedItem().ifPresent(item -> {
    String location = locationProvider.apply(item);
    /* Do what you want with the location */
});

Hope this helps you forward. If you have any further questions or if I did not answer your question, don't hesitate to ask more!

//Teemu

sergio nande
4 years ago Aug 17, 2017 3:59pm
Olli Tietäväinen
4 years ago Aug 18, 2017 5:54am
Olli Tietäväinen
4 years ago Aug 18, 2017 5:55am
sergio nande
4 years ago Aug 18, 2017 8:11am