use of com.vaadin.flow.component.grid.GridSortOrder in project flow-components by vaadin.
the class GridViewSortingPage method createSorting.
private void createSorting() {
Div messageDiv = new Div();
Grid<Person> grid = new Grid<>();
grid.setItems(getItems());
grid.setSelectionMode(SelectionMode.NONE);
grid.addColumn(Person::getFirstName, "firstName").setHeader("Name");
grid.addColumn(Person::getAge, "age").setHeader("Age");
grid.addColumn(TemplateRenderer.<Person>of("<div>[[item.street]], number [[item.number]]<br><small>[[item.postalCode]]</small></div>").withProperty("street", person -> person.getAddress().getStreet()).withProperty("number", person -> person.getAddress().getNumber()).withProperty("postalCode", person -> person.getAddress().getPostalCode()), "street", "number").setHeader("Address");
Checkbox multiSort = new Checkbox("Multiple column sorting enabled");
multiSort.addValueChangeListener(event -> grid.setMultiSort(event.getValue()));
grid.addSortListener(event -> {
String currentSortOrder = grid.getDataCommunicator().getBackEndSorting().stream().map(querySortOrder -> String.format("{sort property: %s, direction: %s}", querySortOrder.getSorted(), querySortOrder.getDirection())).collect(Collectors.joining(", "));
messageDiv.setText(String.format("Current sort order: %s. Sort originates from the client: %s.", currentSortOrder, event.isFromClient()));
});
// you can set the sort order from server-side with the grid.sort method
NativeButton invertAllSortings = new NativeButton("Invert all sort directions", event -> {
List<GridSortOrder<Person>> orderList = grid.getSortOrder();
List<GridSortOrder<Person>> newOrderList = new ArrayList<>(orderList.size());
for (GridSortOrder<Person> sort : orderList) {
newOrderList.add(new GridSortOrder<>(sort.getSorted(), sort.getDirection().getOpposite()));
}
grid.sort(newOrderList);
});
NativeButton resetAllSortings = new NativeButton("Reset all sortings", event -> grid.sort(null));
grid.setId("grid-sortable-columns");
multiSort.setId("grid-multi-sort-toggle");
invertAllSortings.setId("grid-sortable-columns-invert-sortings");
resetAllSortings.setId("grid-sortable-columns-reset-sortings");
messageDiv.setId("grid-sortable-columns-message");
addCard("Sorting", "Grid with sortable columns", grid, multiSort, invertAllSortings, resetAllSortings, messageDiv);
}
use of com.vaadin.flow.component.grid.GridSortOrder in project furms by unity-idm.
the class ProjectsView method createProjectGrid.
private Grid<ProjectGridModel> createProjectGrid() {
Grid<ProjectGridModel> grid = new DenseGrid<>(ProjectGridModel.class);
Grid.Column<ProjectGridModel> firstColumn = grid.addComponentColumn(project -> {
Component component = new Span(project.name);
if (project.status.equals(ACTIVE))
component = new RouterLink(project.name, ProjectView.class, project.id);
return component;
}).setHeader(getTranslation("view.user-settings.projects.grid.column.1")).setSortable(true).setComparator(comparing(project -> project.name)).setComparator(project -> project.name.toLowerCase());
grid.addColumn(project -> project.description).setHeader(getTranslation("view.user-settings.projects.grid.column.2")).setSortable(true);
grid.addColumn(project -> getTranslation(project.status.gridText)).setHeader(getTranslation("view.user-settings.projects.grid.column.3")).setTextAlign(ColumnTextAlign.END).setSortable(true);
grid.addComponentColumn(this::createLastColumnContent).setHeader(getTranslation("view.user-settings.projects.grid.column.4")).setTextAlign(ColumnTextAlign.END);
grid.sort(ImmutableList.of(new GridSortOrder<>(firstColumn, SortDirection.ASCENDING)));
return grid;
}
use of com.vaadin.flow.component.grid.GridSortOrder in project furms by unity-idm.
the class AuditLogView method createCommunityGrid.
private Grid<AuditLogGridModel> createCommunityGrid() {
Grid<AuditLogGridModel> grid = new DenseGrid<>(AuditLogGridModel.class);
Column<AuditLogGridModel> timestamp = grid.addComponentColumn(model -> {
if (model.data.isEmpty())
return new Div(new Label(model.timestamp.format(dateTimeFormatter)));
Icon icon = grid.isDetailsVisible(model) ? ANGLE_DOWN.create() : ANGLE_RIGHT.create();
return new Div(icon, new Label(model.timestamp.format(dateTimeFormatter)));
}).setHeader(getTranslation("view.fenix-admin.audit-log.grid.1")).setSortable(true).setComparator(model -> model.timestamp);
grid.addColumn(model -> model.originator).setHeader(getTranslation("view.fenix-admin.audit-log.grid.2")).setSortable(true).setComparator(model -> model.originator);
grid.addColumn(model -> getTranslation("view.fenix-admin.audit-log.operation." + model.operation)).setHeader(getTranslation("view.fenix-admin.audit-log.grid.3")).setSortable(true).setComparator(comparing(model -> model.operation));
grid.addColumn(model -> getTranslation("view.fenix-admin.audit-log.action." + model.action)).setHeader(getTranslation("view.fenix-admin.audit-log.grid.4")).setSortable(true).setComparator(comparing(model -> model.action));
grid.addColumn(model -> model.name).setHeader(getTranslation("view.fenix-admin.audit-log.grid.5")).setSortable(true).setComparator(comparing(model -> model.name));
grid.addColumn(model -> model.id).setHeader(getTranslation("view.fenix-admin.audit-log.grid.6")).setSortable(true).setComparator(comparing(model -> model.id));
grid.sort(ImmutableList.of(new GridSortOrder<>(timestamp, SortDirection.DESCENDING)));
grid.setItemDetailsRenderer(new ComponentRenderer<>(c -> AuditLogDetailsComponentFactory.create(c.data)));
grid.setSelectionMode(Grid.SelectionMode.NONE);
return grid;
}
use of com.vaadin.flow.component.grid.GridSortOrder 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