From 4786732d33f2d7fc1be7268a1033707776f3a925 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mart=C3=ADn=20L=C3=B3pez?= Date: Mon, 11 May 2020 04:00:35 -0300 Subject: [PATCH] Refresh DataProvider only once when reordering the grid using GridRowDragger (#11981) Fixes #10844 --- .../ui/components/grid/GridRowDragger.java | 6 +++++- .../grid/GridRowDraggerOneGridTest.java | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/com/vaadin/ui/components/grid/GridRowDragger.java b/server/src/main/java/com/vaadin/ui/components/grid/GridRowDragger.java index 56ebe406ca..6b21981f24 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/GridRowDragger.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/GridRowDragger.java @@ -381,7 +381,11 @@ public class GridRowDragger implements Serializable { } sourceItems.removeAll(droppedItems); - listDataProvider.refreshAll(); + + // if reordering the same grid, DataProvider's refresh will be done later + if (getGridDragSource().getGrid() != getGridDropTarget().getGrid()) { + listDataProvider.refreshAll(); + } } private void handleTargetGridDrop(GridDropEvent event, final int index, diff --git a/server/src/test/java/com/vaadin/tests/server/component/grid/GridRowDraggerOneGridTest.java b/server/src/test/java/com/vaadin/tests/server/component/grid/GridRowDraggerOneGridTest.java index 08b647cdde..6c522abcf9 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/grid/GridRowDraggerOneGridTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/grid/GridRowDraggerOneGridTest.java @@ -127,6 +127,26 @@ public class GridRowDraggerOneGridTest { verifyDataProvider("1", "2", "0"); } + @Test + public void listDataProvider_calledOnlyOnce() { + + final int[] times = new int[1]; + + source.setItems("0", "1", "2"); + + source.getDataProvider().addDataProviderListener(ev -> times[0]++); + + dragger.setDropIndexCalculator(event -> { + return Integer.MAX_VALUE; + }); + + drop("1", DropLocation.ABOVE, "0"); + + verifyDataProvider("1", "2", "0"); + + Assert.assertArrayEquals("DataProvider should be invoked only once", new int[] { 1 }, times); + } + @Test public void noopSourceUpdater() { source.setItems("0", "1", "2"); -- 2.39.5