Get next row in grid

When I delete a row in a grid, I want to select the next one. If the order of the rows has been modified by the user clicking on the header of one column, the order of the elements in the ListDataProvider of the grid doesn’t match the order of the elements displayed in the grid.

This code works only if the grid has not been reordered:

GenericModel bean = myGrid.getSelectedRow();
ListDataProvider<GenericModel> dataProvider=(ListDataProvider<GenericModel>) myGrid.getDataProvider();
List<GenericModel> ItemsList=(List<GenericModel>) dataProvider.getItems();
int index=ItemsList.indexOf(bean);//index of the selected item
GenericModel newSelectedBean=ItemsList.get(index+1);
dataProvider.getItems().remove(bean);
dataProvider.refreshAll();
myGrid.select(newSelectedBean);
myGrid.scrollTo(index+1);

if the grid has been reordered, newSelectedBean contains the next item in the original list. Then, given a selected row, what is the way to get the next element in a reordered list?
Thanks, Francesco

I recommend to get the list of items via DataCommunicator instead of ListDataProvider. fetchItemsWithRange returns the list in the sort order after filtering.

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

Thank you Tatu, it works very well!

@francesco can you share the code you came up with? I don’t see how you can get the current offset and limit in order to select the next item that is being displayed in the client?

private void deleteRecord() {
	//get the bean from grid
	GenericModel bean = lookupGrid.getSelectedRow();
	myService.delete((GenericModel) bean,getFunzione().getSecurityObject());
	DataCommunicator<GenericModel> dc=lookupGrid.getDataCommunicator();
	int size=dc.getDataProviderSize();
	List<GenericModel> listaItems=dc.fetchItemsWithRange(0, size);
	ListDataProvider<GenericModel> dp=(ListDataProvider<GenericModel>) lookupGrid.getDataProvider();
	int indice=listaItems.indexOf(bean);//index of the deleted element, starting from 0
	int newSelected;
	if (listaItems.size()==indice+1){
		//has been deleted the last record, select the previous
		newSelected=indice-1;
	}
	else
		newSelected=indice+1;
	if (newSelected>=0){
		//there are more records
		GenericModel newSelectedBean=listaItems.get(newSelected);
		dp.getItems().remove(bean);
		dp.refreshAll();
		lookupGrid.select(newSelectedBean);
		lookupGrid.scrollTo(newSelected);
	}
	else{
		//has been deleted the last record, enter in new record mode
		dp.getItems().remove(bean);
		dp.refreshAll();
		newClicked();
	}
	Notification.show(app.getMessage("deleted"));
	if (nuovo!=null)
		nuovo.setEnabled(true);			
}

@francesco much appreciated!
It would be nice to not have to materialize the entire dataset though. This works fine for small lists but could be bad if there was a very large dataset.

if there was an alternative to this dc.fetchItemsWithRange(0, size). We could maybe just use what was already materialized instead of grabbing the entire dataset.