3
    this.grid.asSingleSelect().addValueChangeListener(event -> {
        if (!Objects.isNull(event.getValue())) {
            this.editor.showEditor(event.getValue());
        }
    });

The problem is that editor covers half of grid when it's displayed. How can i scroll to selected row before showing it?

I know that you can this.grid.scrollTo(rowId);, but i can't find a way to get id of currently selected row.

Hex
  • 242
  • 6
  • 21

2 Answers2

2

grid#scrollTo

grid.scrollTo(rowForValue, ScrollDestination.START);
Mika
  • 1,256
  • 13
  • 18
  • Yes, i know this method, but i can't find a way to get currently selected row id. – Hex Nov 10 '17 at 16:31
  • 1
    @Hex that depends on how the data is stored in the DataProvider that is in use. For ListDataProvider it might be OK to iterate the items and find the index it that way. For larger data sets more efficient way of finding the index should be used; for example, Map with some identifier as key and position as value. – Mika Nov 10 '17 at 18:32
  • 1
    @Mika the grid sort order might have been changed by the user in which case rowId != item index in ListDataProvider, or am I missing something? – Dominic Mar 14 '18 at 07:39
0

In Vaadin 8 you should able to use grid#getSelectedItems

public Set<T> getSelectedItems() 

method to get selected items, if your selection mode is single select, it returns set of one item.

Tatu Lund
  • 9,949
  • 1
  • 12
  • 26
  • Using `getSelectedItems()`, how do you get the rowId for the `grid.scrollTo(rowId)` method? – Dominic Mar 14 '18 at 07:31
  • Since also programmatic selections are possible, in all cases it is not easy to resolve the row indexes of the selected items. The only bullet proof way is to use grid.getDataCommunicator().fetchItemsWithRange(1,gridGetDataCommunicator().etDataProviderSize()) which returns list of items in right order, and where you can search them. This is naturally heavy, some case really heavy. There the row index is always known in item click, thus we made improvement From version 8.4. onwards there is ItemClickEvent.getRowIndex() method. https://github.com/vaadin/framework/pull/10754 – Tatu Lund Apr 13 '18 at 18:44
  • Why isn't this .getRowIndex() method present also in the grid's SelectionListener? That would be also very useful. – Tomask Nov 24 '18 at 15:24
  • 1
    how do I get the rowIndex using Vaadin 22? `ItemClickEvent` does not have it, `SelectionEvent` also does not have it, but `grid.scrollToIndex(firstSelectedItemIdx)` still needs it – sk_dev Feb 28 '22 at 12:58