vaadin-component-factory / selection-grid-flow

Add the functionality to Vaadin Grid to focus on a particular row and column and select a range of rows with SHIFT/CTRL Click
https://incubator.app.fi/selection-grid-flow-demo/
Apache License 2.0
5 stars 8 forks source link

Range starting from the last item gets selected on click #34

Open tomivirkki opened 2 years ago

tomivirkki commented 2 years ago

Version: 2.0.0

When the SelectionGrid is in multi-selection mode and you click any of the visible items, a range starting from the last item gets automatically selected.

https://user-images.githubusercontent.com/1222264/160398734-60fc2d1d-84e0-4c17-97d8-ed0bdd3497a8.mp4

private void initialize() {
    List<Person> personList = getItems();
    Grid<Person> grid = new SelectionGrid<>();
    grid.setItems(personList);

    grid.addColumn(Person::getFirstName).setHeader("First Name");
    grid.addColumn(Person::getAge).setHeader("Age");

    grid.setSelectionMode(Grid.SelectionMode.MULTI);

    grid.asMultiSelect().addValueChangeListener(event -> {
        String message = String.format("Selection changed from %s to %s",
            event.getOldValue(), event.getValue());
        System.out.println(message);
    });

    grid.asMultiSelect().select(personList.get(0), personList.get(1));
    add(grid);
}

private List<Person> getItems() {
    List<Person> persons = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        persons.add(new Person("First Name " + i, i));
    }
    return persons;
}

public static class Person {
    String firstName;
    int age;

    public Person(String firstName, int age) {
        this.firstName = firstName;
        this.age = age;
    }

    public String getFirstName() {
        return firstName;
    }

    public int getAge() {
        return age;
    }
}
TatuLund commented 2 years ago

I tested the add-on with patch https://github.com/vaadin/flow/pull/13476 and I confirm that it fixes the problem. The bug is not in the add-on code.