use of com.vaadin.flow.component.grid.GridSortOrderBuilder in project flow-components by vaadin.
the class TreeGridPreloadPage method setParameter.
@Override
public void setParameter(BeforeEvent event, @OptionalParameter String parameter) {
Location location = event.getLocation();
QueryParameters queryParameters = location.getQueryParameters();
// query parameter: pageSize
List<String> pageSize = queryParameters.getParameters().get("pageSize");
if (pageSize != null) {
grid.setPageSize(Integer.parseInt(pageSize.get(0)));
}
// query parameter: nodesPerLevel
List<String> nodesPerLevel = queryParameters.getParameters().get("nodesPerLevel");
// query parameter: depth
List<String> depth = queryParameters.getParameters().get("depth");
int dpNodesPerLevel = nodesPerLevel == null ? 3 : Integer.parseInt(nodesPerLevel.get(0));
int dpDepth = depth == null ? 4 : Integer.parseInt(depth.get(0));
setDataProvider(dpNodesPerLevel, dpDepth);
// query parameter: expandedRootIndexes
List<String> expandedRootIndexes = queryParameters.getParameters().get("expandedRootIndexes");
if (expandedRootIndexes != null) {
List<HierarchicalTestBean> expandedRootItems = Arrays.stream(expandedRootIndexes.get(0).split(",")).map(Integer::parseInt).map(expandedRootIndex -> new HierarchicalTestBean(null, 0, expandedRootIndex)).collect(java.util.stream.Collectors.toList());
grid.expandRecursively(expandedRootItems, Integer.MAX_VALUE);
}
// query parameter: sortDirection
List<String> sortDirection = queryParameters.getParameters().get("sortDirection");
if (sortDirection != null) {
SortDirection direction = SortDirection.valueOf(sortDirection.get(0).toUpperCase());
GridSortOrderBuilder<HierarchicalTestBean> sorting = new GridSortOrderBuilder<HierarchicalTestBean>();
Column<HierarchicalTestBean> column = grid.getColumns().get(0);
if (direction == SortDirection.ASCENDING) {
grid.sort(sorting.thenAsc(column).build());
} else {
grid.sort(sorting.thenDesc(column).build());
}
}
}
use of com.vaadin.flow.component.grid.GridSortOrderBuilder in project flow-components by vaadin.
the class SortingPage method createGrid.
private Grid<Person> createGrid(String gridId, String sortBtnId) {
Grid<Person> grid = new Grid<>();
grid.setMultiSort(true);
grid.setId(gridId);
grid.setItems(new Person("B", 20), new Person("A", 30));
Column<Person> nameColumn = grid.addColumn(Person::getFirstName).setHeader(new Span("Name"));
Column<Person> ageColumn = grid.addColumn(Person::getAge).setHeader("Age");
// Needed to check that sorter is rendered in component header after
// adding new header row
grid.appendHeaderRow();
List<GridSortOrder<Person>> sortByName = new GridSortOrderBuilder<Person>().thenAsc(nameColumn).build();
grid.sort(sortByName);
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(sortBtnId);
NativeButton reOrder = new NativeButton("Re-order", e -> {
grid.setColumnOrder(ageColumn, nameColumn);
});
reOrder.setId("reorder-button");
NativeButton changeHeaderText = new NativeButton("Change header text", e -> {
ageColumn.setHeader("Age (updated)");
});
changeHeaderText.setId("change-header-text");
NativeButton changeHeaderTextComponent = new NativeButton("Change header text component", e -> {
ageColumn.setHeader(new Span("Age (updated)"));
});
changeHeaderTextComponent.setId("change-header-text-component");
NativeButton clearButton = new NativeButton("Clear items", e -> grid.setItems(new ArrayList<Person>()));
clearButton.setId("clear-items");
add(button, reOrder, changeHeaderText, changeHeaderTextComponent, clearButton);
return grid;
}
Aggregations