RE: How to know row index of selected item in Grid

Hi,

I believe this GitHub issue is relevant to your question:
https://github.com/vaadin/framework/issues/8820

(note also the included workaround there)

-Olli

Vaadin Grid has:

public void scrollTo(int row, ScrollDestination destination)

What way to know “zero based index of the item” which selected?

In this Issue and workaround no solution to get “row index”.

Right, the workaround is only for opening the editor. But as the original text message reads,
This would require also scrollToItem(T item) to work also
. That’s something that is currently missing (and the ticket is still open). You might want to comment or thumbs-up on the ticket to let people know that it’s needed.

-Olli

If items for grid from ArrayList (grid.setItems), this way to scroll is working:

ListDataProvider listDataProvider = (ListDataProvider) grid.getDataProvider();
ArrayList items = (new ArrayList(listDataProvider.getItems()));
int index = items.indexOf(item);
grid.scrollTo(index, ScrollDestination.END);

Good thinking. However, there should probably be a better way to do it, it doesn’t feel good to need to rely on implementation details like that.

-Olli

Problem to know row index for Grid with “com.vaadin.data.provider.CallbackDataProvider”:


grid.setDataProvider(
    (sortOrders, offset, limit) ->
            service.findAll(offset, limit).stream(),
    () -> service.count()
);
const gridSelectedColumn = parentElement.querySelector('vaadin-grid-selection-column'); gridSelectedColumn.renderer = (cell, column, rowData) => {
                let checkbox = cell.firstElementChild;
                if (!checkbox) {
                    checkbox = document.createElement('vaadin-checkbox');
                    checkbox.setAttribute('aria-label', 'Select Row');
                    checkbox.addEventListener('change', (e) => {
                        let operation;
                        if (e.target.checked === false) {
                            operation = 'CANCEL';
                            this.rootElement.deselectItem(checkbox.__item);
                        } else {
                            operation = 'SELECT';
                            this.rootElement.selectItem(checkbox.__item);
                        }
                        this.rootElement.render();
                        let selectedLength = this.rootElement.selectedItems.length;
                    });
                    cell.appendChild(checkbox);
                }
                checkbox.__item = rowData.item;
                checkbox.checked = rowData.selected;
                rowData.item.index = rowData.index;
            };

for (let selectedItem of grid.selectedItems) {
// selectedItem.index is selected index
}

ListDataProvider listDataProvider = (ListDataProvider) grid.getDataProvider();
ArrayList items = (new ArrayList(listDataProvider.getItems()));
int index = items.indexOf(item);
grid.scrollTo(index, ScrollDestination.END);

This does not work correctly if you sort or filter your grid.