I have a grid with one column named “Object”. I have one row/item under the column with the value of “Location”. When I try to get the string value of the row, I get an int value of 1. I need the string value of the row which is “Location”, not the number of the row:
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:
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 ?
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!
Hello all, new writing in this forums and i didn’t wanted to open a new thread for something similar asked.
In my case we have selection model.NONE and when the user right clicks an item we should do X, but it seems imposible to get itemId as for example in table where things like these were possible.
we work with 7.6.6 could be there a solution for this version and for when we change to vaadin 8?
Hello Olli,
I think I asked in the wrong thread ( had multiple open sorry), but continuing on the topic,
I already tried it, i’m migrating table to grid and grid doesn’t seem to recognise te right click as an clicking event, there is no contextlistener, contextmenu but i tried with it too but the event doesn’t returns the same information as in itemclick