From cbc6c7cbe4b97c2160822ee63c557649d04fc2e9 Mon Sep 17 00:00:00 2001 From: Yuriy Yevstihnyeyev Date: Sun, 20 Dec 2020 11:35:44 +0200 Subject: [PATCH] fix: ensure first page is loaded; sorting not breaking on hidden grid (cp #529) (#531) --- .../flow/component/grid/it/SortingPage.java | 48 +++++++++++++++---- .../it/GridTestScrollingOver100kLinesIT.java | 4 +- .../flow/component/grid/it/SortingIT.java | 32 ++++++++----- .../resources/frontend/gridConnector.js | 5 ++ 4 files changed, 65 insertions(+), 24 deletions(-) diff --git a/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/main/java/com/vaadin/flow/component/grid/it/SortingPage.java b/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/main/java/com/vaadin/flow/component/grid/it/SortingPage.java index cde652b7679..1b8c19be933 100644 --- a/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/main/java/com/vaadin/flow/component/grid/it/SortingPage.java +++ b/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/main/java/com/vaadin/flow/component/grid/it/SortingPage.java @@ -33,33 +33,63 @@ public class SortingPage extends Div { public SortingPage() { + createSortingGrid(); + createInitiallyHiddenGrid(); + } + + private void createSortingGrid() { + Grid grid = createGrid("sorting-grid", "sort-by-age"); + + NativeButton btRm = new NativeButton("detach", evt -> remove(grid)); + btRm.setId("btn-detach"); + NativeButton btattach = new NativeButton("attach", + evt -> add(grid)); + btattach.setId("btn-attach"); + add(btRm, btattach, grid); + + } + + private void createInitiallyHiddenGrid() { + Grid grid = createGrid("hidden-grid", "sort-hidden-by-age"); + + grid.setMaxHeight("0px"); + grid.getStyle().set("display", "none"); + + NativeButton showGridBtn = new NativeButton("Show grid", e -> { + grid.getStyle().set("display", "block"); + grid.getStyle().remove("max-height"); + grid.getElement().executeJs("$0.notifyResize();"); + }); + showGridBtn.setId("show-hidden-grid"); + + add(grid, showGridBtn); + } + + private Grid createGrid(String gridId, String sortBtnId) { Grid grid = new Grid<>(); grid.setMultiSort(true); - grid.setId("sorting-grid"); + grid.setId(gridId); grid.setItems(new Person("B", 20), new Person("A", 30)); + Column nameColumn = grid.addColumn(Person::getFirstName) .setHeader("Name"); Column ageColumn = grid.addColumn(Person::getAge) .setHeader("Age"); - NativeButton btRm = new NativeButton("detach", evt -> remove(grid)); - btRm.setId("btn-detach"); - NativeButton btattach = new NativeButton("attach", - evt -> add(grid)); - btattach.setId("btn-attach"); - add(btRm, btattach, grid); List> sortByName = new GridSortOrderBuilder() .thenAsc(nameColumn).build(); grid.sort(sortByName); - NativeButton button = new NativeButton("Sort by age", e -> { + NativeButton button = new NativeButton(sortBtnId.equals("sort-hidden-by-age") ? "Sort hidden by age" : "Sort by age", e -> { List> sortByAge = new GridSortOrderBuilder() .thenAsc(ageColumn).build(); grid.sort(sortByAge); }); - button.setId("sort-by-age"); + button.setId(sortBtnId); add(button); + + return grid; } } diff --git a/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/grid/it/GridTestScrollingOver100kLinesIT.java b/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/grid/it/GridTestScrollingOver100kLinesIT.java index 7afb251ff81..27a75ec0666 100644 --- a/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/grid/it/GridTestScrollingOver100kLinesIT.java +++ b/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/grid/it/GridTestScrollingOver100kLinesIT.java @@ -25,8 +25,6 @@ import com.vaadin.tests.AbstractComponentIT; import com.vaadin.flow.testutil.TestPath; import com.vaadin.testbench.TestBenchElement; -import com.vaadin.testbench.annotations.RunLocally; -import com.vaadin.testbench.parallel.Browser; @TestPath("vaadin-grid/scroll-over-100k") public class GridTestScrollingOver100kLinesIT extends AbstractComponentIT { @@ -51,7 +49,7 @@ public void toStringIsUsedForObjectSerialization() { allCellContents.forEach(vgcc -> { String slotName = vgcc.getAttribute("slot") .replace("vaadin-grid-cell-content-", ""); - if (Integer.parseInt(slotName) <= 180) { + if (Integer.parseInt(slotName) < 7 || Integer.parseInt(slotName) > 20) { Assert.assertTrue( "A grid cell was expected to have text content but had none.", StringUtils.isNotBlank(vgcc.getText())); diff --git a/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/grid/it/SortingIT.java b/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/grid/it/SortingIT.java index e9fda36f8d0..15cc66903ee 100644 --- a/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/grid/it/SortingIT.java +++ b/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/grid/it/SortingIT.java @@ -36,7 +36,7 @@ public class SortingIT extends AbstractComponentIT { @Before public void init() { open(); - grid = $(GridElement.class).first(); + grid = $(GridElement.class).id("sorting-grid"); } @Test @@ -57,6 +57,14 @@ public void setInitialSortOrder_changeOrderFromServer_dataSorted() { Assert.assertEquals("A", grid.getCell(1, 0).getText()); } + @Test + public void setInitialSortOrderGridHidden_showGrid_dataPresentAndSorted() { + findElement(By.id("sort-hidden-by-age")).click(); + findElement(By.id("show-hidden-grid")).click(); + Assert.assertEquals("B", $(GridElement.class).id("hidden-grid").getCell(0, 0).getText()); + Assert.assertEquals("A", $(GridElement.class).id("hidden-grid").getCell(1, 0).getText()); + } + @Test public void setInitialSortOrder_changeOrderFromServer_sortIndicatorsUpdated() { findElement(By.id("sort-by-age")).click(); @@ -67,38 +75,38 @@ public void setInitialSortOrder_changeOrderFromServer_sortIndicatorsUpdated() { public void indicatorsSortStateNumbersAndDirectionsAndContentOfRow() { WebElement btnAttach = findElement(By.id("btn-attach")); WebElement btnRemove = findElement(By.id("btn-detach")); - GridElement sortingGridElement = $(GridElement.class).first(); + GridElement sortingGridElement = $(GridElement.class).id("sorting-grid"); findElement(By.id("sort-by-age")).click(); - findElements(By.tagName("vaadin-grid-sorter")) + sortingGridElement.findElements(By.tagName("vaadin-grid-sorter")) .get(0).click(); String textAgeColumnBeforeReattch = sortingGridElement.getCell(0, 1).getText(); - Assert.assertEquals("asc", findElements(By.tagName("vaadin-grid-sorter")) + Assert.assertEquals("asc", sortingGridElement.findElements(By.tagName("vaadin-grid-sorter")) .get(0).getAttribute("direction")); String sortStateNumberNameColumn - = findElements(By.tagName("vaadin-grid-sorter")).get(0).getAttribute("_order"); - Assert.assertEquals("asc", findElements(By.tagName("vaadin-grid-sorter")) + = sortingGridElement.findElements(By.tagName("vaadin-grid-sorter")).get(0).getAttribute("_order"); + Assert.assertEquals("asc", sortingGridElement.findElements(By.tagName("vaadin-grid-sorter")) .get(1).getAttribute("direction")); String sortStateNumberAgeColumn - = findElements(By.tagName("vaadin-grid-sorter")).get(1).getAttribute("_order"); + = sortingGridElement.findElements(By.tagName("vaadin-grid-sorter")).get(1).getAttribute("_order"); // Detach btnRemove.click(); // Reattach btnAttach.click(); - sortingGridElement = $(GridElement.class).first(); + sortingGridElement = $(GridElement.class).id("sorting-grid"); - Assert.assertEquals("asc", findElements(By.tagName("vaadin-grid-sorter")) + Assert.assertEquals("asc", sortingGridElement.findElements(By.tagName("vaadin-grid-sorter")) .get(0).getAttribute("direction")); - Assert.assertEquals("asc", findElements(By.tagName("vaadin-grid-sorter")) + Assert.assertEquals("asc", sortingGridElement.findElements(By.tagName("vaadin-grid-sorter")) .get(1).getAttribute("direction")); String sortStateNumberAgeColumnAfterDetach - = findElements(By.tagName("vaadin-grid-sorter")).get(1).getAttribute("_order"); + = sortingGridElement.findElements(By.tagName("vaadin-grid-sorter")).get(1).getAttribute("_order"); String sortStateNumberNameColumnAfterDetach - = findElements(By.tagName("vaadin-grid-sorter")).get(0).getAttribute("_order"); + = sortingGridElement.findElements(By.tagName("vaadin-grid-sorter")).get(0).getAttribute("_order"); String textAgeColumnAfterReattch = sortingGridElement.getCell(0, 1).getText(); Assert.assertEquals(textAgeColumnBeforeReattch, textAgeColumnAfterReattch); Assert.assertEquals(sortStateNumberAgeColumn, sortStateNumberAgeColumnAfterDetach); diff --git a/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/resources/META-INF/resources/frontend/gridConnector.js b/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/resources/META-INF/resources/frontend/gridConnector.js index a6a5489f5f6..7ad5d6288c6 100644 --- a/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/resources/META-INF/resources/frontend/gridConnector.js +++ b/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/resources/META-INF/resources/frontend/gridConnector.js @@ -367,6 +367,11 @@ } }) + // Need to flush FlattenedNodesObserver in order to update `grid._columnTree` + // before `_dataProviderChanged` is called. Otherwise, the first page won't + // be correctly loaded because of `_canPopulate` returns incorrect result. + grid._observer.flush(); + grid.dataProvider = tryCatchWrapper(function(params, callback) { if (params.pageSize != grid.pageSize) { throw 'Invalid pageSize';