Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
vursen committed Dec 19, 2024
1 parent b224e44 commit 175154e
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class GridRangeSelection extends Div {
private Person startItem;

public GridRangeSelection() {
// tag::snippet[]
Grid<Person> grid = new Grid<>(Person.class, false);
grid.addColumn(Person::getFirstName).setHeader("First name");
grid.addColumn(Person::getLastName).setHeader("Last name");
Expand All @@ -27,7 +28,6 @@ public GridRangeSelection() {
List<Person> people = DataService.getPeople();
grid.setItems(people);

// tag::snippet[]
grid.setSelectionMode(Grid.SelectionMode.MULTI);
((GridMultiSelectionModel<Person>) grid.getSelectionModel())
.addClientItemToggleListener(event -> {
Expand All @@ -39,9 +39,10 @@ public GridRangeSelection() {
if (event.isShiftKey()) {
// Calculcate the range of items between the anchor point and
// the current item
int startIndex = grid.getListDataView().getItemIndex(startItem).get();
int endIndex = grid.getListDataView().getItemIndex(endItem).get();
Set<Person> rangeItems = grid.getListDataView().getItems().skip(Math.min(startIndex, endIndex)).limit(Math.abs(startIndex - endIndex) + 1).collect(Collectors.toSet());
GridListDataView<Person> dataView = grid.getListDataView();
int startIndex = dataView.getItemIndex(startItem).get();
int endIndex = dataView.getItemIndex(endItem).get();
Set<Person> rangeItems = dataView.getItems().skip(Math.min(startIndex, endIndex)).limit(Math.abs(startIndex - endIndex) + 1).collect(Collectors.toSet());

// Update the selection state of the items within the range
// based on the state of the current item
Expand All @@ -56,6 +57,7 @@ public GridRangeSelection() {
startItem = item;
});
// end::snippet[]

add(grid);
}

Expand Down

0 comments on commit 175154e

Please sign in to comment.