Skip to content

Commit

Permalink
fix: ensure first page is loaded; sorting not breaking on hidden grid…
Browse files Browse the repository at this point in the history
… (cp #529) (#531)
  • Loading branch information
yuriy-fix authored Dec 20, 2020
1 parent f75aa43 commit cbc6c7c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,63 @@
public class SortingPage extends Div {

public SortingPage() {
createSortingGrid();
createInitiallyHiddenGrid();
}

private void createSortingGrid() {
Grid<Person> 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<Person> 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<Person> grid = new Grid<>();
grid.setMultiSort(true);
grid.setId("sorting-grid");
grid.setId(gridId);
grid.setItems(new Person("B", 20), new Person("A", 30));

Column<Person> nameColumn = grid.addColumn(Person::getFirstName)
.setHeader("Name");
Column<Person> 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<GridSortOrder<Person>> sortByName = new GridSortOrderBuilder<Person>()
.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<GridSortOrder<Person>> sortByAge = new GridSortOrderBuilder<Person>()
.thenAsc(ageColumn).build();
grid.sort(sortByAge);
});
button.setId("sort-by-age");
button.setId(sortBtnId);

add(button);

return grid;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit cbc6c7c

Please sign in to comment.