0

Use case:

  • a Grid is displaying a list of items
  • the user can click a button to edit the selected item
  • when the user clicks "edit" a dialog is displayed for editing the item
  • the user would like to have "edit next" and "edit previous" buttons on the dialog to automatically save the current item and advance to the next or previous item in the Grid

The twist: the Grid is sortable by the user using the standard column-based sorting, which is apparently implemented entirely on the client side.

The problem: the "index" of an item within the Grid returned by Grid.getRowIndex() returns the index within the unsorted list. Implementing the next/previous function off of this index can cause the selection to appear to jump around randomly if the user has sorted the Grid.

The question: Is there a way to get the display row index of the selected item? And conversely, is there a way to get the item at a specified display row index? Or, as an alternative, does the Grid have a way to move the selected row index forward or backwards by a given amount in display order?

Anna Koskinen
  • 1,362
  • 3
  • 22
  • Your first question looks like duplicate of this https://stackoverflow.com/questions/62061435/vaadin-flow-grid-with-row-index – Tatu Lund Dec 15 '21 at 19:19

1 Answers1

1

And conversely, is there a way to get the item at a specified display row index?

Easy API for this has been added in Vaadin 17 and newer.

dataView = grid.setItems(..);
item = dataView.getItem(index);

Which gets the item at the given index from the data available to the component. Data is filtered and sorted the same way as in the component.

In Vaadin 14 it is a bit more complex. There is code that genr

grid.getDataProvider().fetch(createQuery());

You can find here code example with quite suitable createQuery(), just replace 0 with index and max integer with 1.

https://cookbook.vaadin.com/grid-csv-export

Tatu Lund
  • 9,949
  • 1
  • 12
  • 26
  • I see the new DataView API also has handy getNextItem() and getPreviousItem() methods, which complete the puzzle for me. – Kevin Klein Dec 15 '21 at 19:40