Search in sources :

Example 1 with GridSortOrder

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);
}
Also used : Checkbox(com.vaadin.flow.component.checkbox.Checkbox) List(java.util.List) Person(com.vaadin.flow.data.bean.Person) Grid(com.vaadin.flow.component.grid.Grid) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) SelectionMode(com.vaadin.flow.component.grid.Grid.SelectionMode) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) GridSortOrder(com.vaadin.flow.component.grid.GridSortOrder) Route(com.vaadin.flow.router.Route) NativeButton(com.vaadin.flow.component.html.NativeButton) GridSortOrder(com.vaadin.flow.component.grid.GridSortOrder) Grid(com.vaadin.flow.component.grid.Grid) ArrayList(java.util.ArrayList) Div(com.vaadin.flow.component.html.Div) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) Person(com.vaadin.flow.data.bean.Person)

Example 2 with GridSortOrder

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;
}
Also used : VaadinExceptionHandler.handleExceptions(io.imunity.furms.ui.utils.VaadinExceptionHandler.handleExceptions) SortDirection(com.vaadin.flow.data.provider.SortDirection) Component(com.vaadin.flow.component.Component) NotificationUtils.showErrorNotification(io.imunity.furms.ui.utils.NotificationUtils.showErrorNotification) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) FurmsDialog(io.imunity.furms.ui.components.FurmsDialog) MenuButton(io.imunity.furms.ui.components.MenuButton) PageTitle(io.imunity.furms.ui.components.PageTitle) Route(com.vaadin.flow.router.Route) FlexComponent(com.vaadin.flow.component.orderedlayout.FlexComponent) HashSet(java.util.HashSet) PLUS_CIRCLE(com.vaadin.flow.component.icon.VaadinIcon.PLUS_CIRCLE) ImmutableList(com.google.common.collect.ImmutableList) DenseGrid(io.imunity.furms.ui.components.DenseGrid) FurmsViewComponent(io.imunity.furms.ui.components.FurmsViewComponent) PIE_CHART(com.vaadin.flow.component.icon.VaadinIcon.PIE_CHART) RouterLink(com.vaadin.flow.router.RouterLink) UserSettingsMenu(io.imunity.furms.ui.views.user_settings.UserSettingsMenu) Comparator.comparing(java.util.Comparator.comparing) TextField(com.vaadin.flow.component.textfield.TextField) SEARCH(com.vaadin.flow.component.icon.VaadinIcon.SEARCH) ValueChangeMode(com.vaadin.flow.data.value.ValueChangeMode) CheckboxGroupVariant(com.vaadin.flow.component.checkbox.CheckboxGroupVariant) MINUS_CIRCLE(com.vaadin.flow.component.icon.VaadinIcon.MINUS_CIRCLE) REQUESTED(io.imunity.furms.ui.views.user_settings.projects.UserStatus.REQUESTED) Grid(com.vaadin.flow.component.grid.Grid) ApplicationNotExistingException(io.imunity.furms.api.validation.exceptions.ApplicationNotExistingException) TRASH(com.vaadin.flow.component.icon.VaadinIcon.TRASH) RouterGridLink(io.imunity.furms.ui.components.RouterGridLink) ACTIVE(io.imunity.furms.ui.views.user_settings.projects.UserStatus.ACTIVE) Set(java.util.Set) GridActionMenu(io.imunity.furms.ui.components.GridActionMenu) GridActionsButtonLayout(io.imunity.furms.ui.components.GridActionsButtonLayout) ProjectApplicationsService(io.imunity.furms.api.applications.ProjectApplicationsService) GridSortOrder(com.vaadin.flow.component.grid.GridSortOrder) UserAlreadyInvitedException(io.imunity.furms.api.validation.exceptions.UserAlreadyInvitedException) Tooltip(com.vaadin.componentfactory.Tooltip) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ColumnTextAlign(com.vaadin.flow.component.grid.ColumnTextAlign) ProjectService(io.imunity.furms.api.projects.ProjectService) CheckboxGroup(com.vaadin.flow.component.checkbox.CheckboxGroup) UserWithoutFenixIdValidationError(io.imunity.furms.api.validation.exceptions.UserWithoutFenixIdValidationError) Dialog(com.vaadin.flow.component.dialog.Dialog) Collections(java.util.Collections) Span(com.vaadin.flow.component.html.Span) ViewHeaderLayout(io.imunity.furms.ui.components.ViewHeaderLayout) NotificationUtils.showSuccessNotification(io.imunity.furms.ui.utils.NotificationUtils.showSuccessNotification) RouterLink(com.vaadin.flow.router.RouterLink) GridSortOrder(com.vaadin.flow.component.grid.GridSortOrder) DenseGrid(io.imunity.furms.ui.components.DenseGrid) DenseGrid(io.imunity.furms.ui.components.DenseGrid) Grid(com.vaadin.flow.component.grid.Grid) Component(com.vaadin.flow.component.Component) FlexComponent(com.vaadin.flow.component.orderedlayout.FlexComponent) FurmsViewComponent(io.imunity.furms.ui.components.FurmsViewComponent) Span(com.vaadin.flow.component.html.Span)

Example 3 with GridSortOrder

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;
}
Also used : ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) UTCTimeUtils.convertToZoneTime(io.imunity.furms.utils.UTCTimeUtils.convertToZoneTime) SortDirection(com.vaadin.flow.data.provider.SortDirection) ZonedDateTime(java.time.ZonedDateTime) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) Div(com.vaadin.flow.component.html.Div) Label(com.vaadin.flow.component.html.Label) HashMap(java.util.HashMap) PageTitle(io.imunity.furms.ui.components.PageTitle) Route(com.vaadin.flow.router.Route) FlexComponent(com.vaadin.flow.component.orderedlayout.FlexComponent) AuditLogService(io.imunity.furms.api.audit_log.AuditLogService) FlexLayout(com.vaadin.flow.component.orderedlayout.FlexLayout) ImmutableList(com.google.common.collect.ImmutableList) DenseGrid(io.imunity.furms.ui.components.DenseGrid) MultiselectComboBox(org.vaadin.gatanaso.MultiselectComboBox) FurmsViewComponent(io.imunity.furms.ui.components.FurmsViewComponent) Map(java.util.Map) Operation(io.imunity.furms.domain.audit_log.Operation) ANGLE_RIGHT(com.vaadin.flow.component.icon.VaadinIcon.ANGLE_RIGHT) Comparator.comparing(java.util.Comparator.comparing) TypeReference(com.fasterxml.jackson.core.type.TypeReference) UserService(io.imunity.furms.api.users.UserService) Icon(com.vaadin.flow.component.icon.Icon) Action(io.imunity.furms.domain.audit_log.Action) Grid(com.vaadin.flow.component.grid.Grid) UIContext(io.imunity.furms.ui.user_context.UIContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AuditLogDetailsComponentFactory(io.imunity.furms.ui.components.AuditLogDetailsComponentFactory) FURMSUser(io.imunity.furms.domain.users.FURMSUser) SearchLayout(io.imunity.furms.ui.components.administrators.SearchLayout) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) ANGLE_DOWN(com.vaadin.flow.component.icon.VaadinIcon.ANGLE_DOWN) GridSortOrder(com.vaadin.flow.component.grid.GridSortOrder) FurmsDateTimePicker(io.imunity.furms.ui.components.FurmsDateTimePicker) FenixAdminMenu(io.imunity.furms.ui.views.fenix.menu.FenixAdminMenu) Column(com.vaadin.flow.component.grid.Grid.Column) AuditLog(io.imunity.furms.domain.audit_log.AuditLog) DateTimeFormatter(java.time.format.DateTimeFormatter) Div(com.vaadin.flow.component.html.Div) GridSortOrder(com.vaadin.flow.component.grid.GridSortOrder) DenseGrid(io.imunity.furms.ui.components.DenseGrid) Label(com.vaadin.flow.component.html.Label) Icon(com.vaadin.flow.component.icon.Icon)

Example 4 with GridSortOrder

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;
}
Also used : NativeButton(com.vaadin.flow.component.html.NativeButton) GridSortOrder(com.vaadin.flow.component.grid.GridSortOrder) Grid(com.vaadin.flow.component.grid.Grid) ArrayList(java.util.ArrayList) Person(com.vaadin.flow.data.bean.Person) Span(com.vaadin.flow.component.html.Span) GridSortOrderBuilder(com.vaadin.flow.component.grid.GridSortOrderBuilder)

Aggregations

Grid (com.vaadin.flow.component.grid.Grid)4 GridSortOrder (com.vaadin.flow.component.grid.GridSortOrder)4 Route (com.vaadin.flow.router.Route)3 ImmutableList (com.google.common.collect.ImmutableList)2 Div (com.vaadin.flow.component.html.Div)2 NativeButton (com.vaadin.flow.component.html.NativeButton)2 Span (com.vaadin.flow.component.html.Span)2 FlexComponent (com.vaadin.flow.component.orderedlayout.FlexComponent)2 HorizontalLayout (com.vaadin.flow.component.orderedlayout.HorizontalLayout)2 Person (com.vaadin.flow.data.bean.Person)2 SortDirection (com.vaadin.flow.data.provider.SortDirection)2 DenseGrid (io.imunity.furms.ui.components.DenseGrid)2 FurmsViewComponent (io.imunity.furms.ui.components.FurmsViewComponent)2 List (java.util.List)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Tooltip (com.vaadin.componentfactory.Tooltip)1 Component (com.vaadin.flow.component.Component)1 Checkbox (com.vaadin.flow.component.checkbox.Checkbox)1