Framework: Feature: Grid: get row index

Created on 21 May 2018  路  1Comment  路  Source: vaadin/framework

I would like to propose new feature for Grid.

Ability to read row index.
The grid rows are ordered by selected ordering and can be reordered by user by clicking on header cells of sortable columns. It would be nice for programmer to be able to get index by row in current ordering or to get row by index.

int i = grid.getRowIndex(row);
T row = grid.getRow(index);

There are already such methods for header and footer rows (getFooterRow, getHeaderRow).

Example: We use Grid with data from DataProvider. Some of rows are of special type. We are implementing functionality that allow user to jump over these special rows using keyboard shortcuts. Jump to next or previous special row is done using grid.scrollTo(index) which takes the integer index.
Now we need to handle ordering/reordering of rows and mapping to index by ourself, but this information is already inside the Grid, so it would be fine when user can read it through public interface.

enhancement

Most helpful comment

Grid.getDataCommunicator() can be used to get DataCommunicator. There is fetchItemsWithRange method there, which fetches items after sorting and filtering

https://vaadin.com/download/release/8.4/8.4.2/docs/api/com/vaadin/data/provider/DataCommunicator.html#fetchItemsWithRange-int-int-

Hence you can use this for T row = grid.getRow(index);

public T getRow(Grid<T> grid, int index) {
   return grid.getDataCommunicator().fetchItemsWithRange(index,1).get(0);
}

And the int i = grid.getRowIndex(row); is

public int getRowIndex(Grid<T> grid, T row) {
   List<T> items = grid.getDataCommunicator().fetchItemsWithRange(0,grid.getDataCommunicator().getDataProviderSize());
   return items.indexOf(row);
}

These are simplistic examples, and you need to add necessary null pointer checks etc.

Especially notable is the second one, since the dataset can be larger, it could be resource consuming. In some cases it could be possible to get index of the item from database / backend more efficiently, but that is highly application and data source specific. This is also the reason why this operation is not in framework, because without application specific knowledge the operation is suboptimal.

Note also, that Grid client side knows only the indeces of the rows that are currently being displayed. Hence in Vaadin 8.4.0 getRowIndex() method was added to ItemClickEvent.

>All comments

Grid.getDataCommunicator() can be used to get DataCommunicator. There is fetchItemsWithRange method there, which fetches items after sorting and filtering

https://vaadin.com/download/release/8.4/8.4.2/docs/api/com/vaadin/data/provider/DataCommunicator.html#fetchItemsWithRange-int-int-

Hence you can use this for T row = grid.getRow(index);

public T getRow(Grid<T> grid, int index) {
   return grid.getDataCommunicator().fetchItemsWithRange(index,1).get(0);
}

And the int i = grid.getRowIndex(row); is

public int getRowIndex(Grid<T> grid, T row) {
   List<T> items = grid.getDataCommunicator().fetchItemsWithRange(0,grid.getDataCommunicator().getDataProviderSize());
   return items.indexOf(row);
}

These are simplistic examples, and you need to add necessary null pointer checks etc.

Especially notable is the second one, since the dataset can be larger, it could be resource consuming. In some cases it could be possible to get index of the item from database / backend more efficiently, but that is highly application and data source specific. This is also the reason why this operation is not in framework, because without application specific knowledge the operation is suboptimal.

Note also, that Grid client side knows only the indeces of the rows that are currently being displayed. Hence in Vaadin 8.4.0 getRowIndex() method was added to ItemClickEvent.

Was this page helpful?
0 / 5 - 0 ratings