Skip to content

Grid is not refreshed correctly after Multi-Selection with SHIFT #42

@finaris-cs

Description

@finaris-cs

Affected Version: 3.0.1 / Vaadin 24.0

  1. Select the first row
  2. Hold SHIFT down and select the 4th row
  3. Click on 5th row without any modifier keys

=> The first row remains visually selected even though its is not selected according to model or selection event (see status line in example below grid)
The code below can be used for reproduction.
Calling a refreshAll() on the grid's data provider can be used as workaround to let the wrong selection disappear

My expectation would be that the grid handles this internally.

image

package com.finaris.sqace.frontend.ui.views;

import com.vaadin.componentfactory.selectiongrid.SelectionGrid;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;

import java.util.*;
import java.util.stream.Collectors;

@Route(value = "test3")
public class TestView3 extends VerticalLayout {

//    private Set<Item> lastSelection = Collections.emptySet();

    public TestView3() {
        List<Item> itemList = new ArrayList<>();
        itemList.add(new Item(1, "Carl", "Allisson"));
        itemList.add(new Item(2, "Margot", "Fine"));
        itemList.add(new Item(3, "Christian", "Black"));
        itemList.add(new Item(4, "Tom", "Greene"));
        itemList.add(new Item(5, "Anna", "Smith"));
        itemList.add(new Item(6, "Ashley", "Parker"));
        itemList.add(new Item(7, "Greg", "van Helsing"));
        itemList.add(new Item(8, "Michael", "Washington"));
        itemList.add(new Item(9, "Hanna", "Walker"));
        itemList.add(new Item(10, "Anthony", "King"));
        setSizeFull();
        Grid<Item> grid = new SelectionGrid<>();
        grid.setSizeFull();
        grid.setSelectionMode(Grid.SelectionMode.MULTI);

        grid.addColumn(Item::getId).setHeader("ID");
        grid.addColumn(Item::getFirstName).setHeader("First Name");
        grid.addColumn(Item::getLastName)
                .setHeader("Last Name");
        grid.setItems(itemList);

        Span info = new Span();
        grid.addSelectionListener(e -> {
            Set<Item> allSelectedItems = e.getAllSelectedItems();
            info.setText("Selection according to model: " + allSelectedItems.stream().map(Object::toString).collect(Collectors.joining(", ")));

//            if (allSelectedItems.size() < lastSelection.size()) {
//                grid.getDataProvider().refreshAll();
//            }
//            lastSelection = allSelectedItems;
        });
        add(grid, info);
    }


    private static class Item {
        private final int id;
        private final String firstName;
        private final String lastName;

        public Item(int id, String firstName, String lastName) {
            this.id = id;
            this.firstName = firstName;
            this.lastName = lastName;
        }

        @Override
        public String toString() {
            return firstName + " " + lastName;
        }

        public int getId() {
            return id;
        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Item item = (Item) o;
            return Objects.equals(id, item.id);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions