Search in sources :

Example 11 with Person

use of com.vaadin.flow.data.bean.Person in project flow-components by vaadin.

the class GridViewSelectionPage method createSingleSelect.

private void createSingleSelect() {
    Div messageDiv = new Div();
    List<Person> people = getItems();
    Grid<Person> grid = new Grid<>();
    grid.setItems(people);
    grid.addColumn(Person::getFirstName).setHeader("Name");
    grid.addColumn(Person::getAge).setHeader("Age");
    grid.asSingleSelect().addValueChangeListener(event -> messageDiv.setText(String.format("Selection changed from %s to %s, selection is from client: %s", event.getOldValue(), event.getValue(), event.isFromClient())));
    NativeButton toggleSelect = new NativeButton("Toggle selection of the first person");
    Person firstPerson = people.get(0);
    toggleSelect.addClickListener(event -> {
        GridSelectionModel<Person> selectionModel = grid.getSelectionModel();
        if (selectionModel.isSelected(firstPerson)) {
            selectionModel.deselect(firstPerson);
        } else {
            selectionModel.select(firstPerson);
        }
    });
    grid.setId("single-selection");
    toggleSelect.setId("single-selection-toggle");
    messageDiv.setId("single-selection-message");
    addCard("Selection", "Grid Single Selection", grid, toggleSelect, messageDiv);
}
Also used : Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) Grid(com.vaadin.flow.component.grid.Grid) Person(com.vaadin.flow.data.bean.Person)

Example 12 with Person

use of com.vaadin.flow.data.bean.Person 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 13 with Person

use of com.vaadin.flow.data.bean.Person in project flow-components by vaadin.

the class GridViewUsingComponentsPage method createColumnComponentRenderer.

private void createColumnComponentRenderer() {
    Grid<Person> grid = new Grid<>();
    grid.setItems(createItems());
    // Use the component constructor that accepts an item ->
    // new PersonComponent(Person person)
    grid.addComponentColumn(PersonComponent::new).setHeader("Person");
    // Or you can use an ordinary function to setup the component
    grid.addComponentColumn(item -> new NativeButton("Remove", evt -> {
        ListDataProvider<Person> dataProvider = (ListDataProvider<Person>) grid.getDataProvider();
        dataProvider.getItems().remove(item);
        dataProvider.refreshAll();
    })).setHeader("Actions");
    // Item details can also use components
    grid.setItemDetailsRenderer(new ComponentRenderer<>(PersonCard::new));
    // When items are updated, new components are generated
    TextField idField = new TextField("", "Person id");
    TextField nameField = new TextField("", "New name");
    NativeButton updateButton = new NativeButton("Update person", event -> {
        String id = idField.getValue();
        String name = nameField.getValue();
        ListDataProvider<Person> dataProvider = (ListDataProvider<Person>) grid.getDataProvider();
        dataProvider.getItems().stream().filter(person -> String.valueOf(person.getId()).equals(id)).findFirst().ifPresent(person -> {
            person.setFirstName(name);
            dataProvider.refreshItem(person);
        });
    });
    grid.setSelectionMode(SelectionMode.NONE);
    grid.setId("component-renderer");
    idField.setId("component-renderer-id-field");
    nameField.setId("component-renderer-name-field");
    updateButton.setId("component-renderer-update-button");
    addCard("Using components", "Grid with columns using component renderer", grid, idField, nameField, updateButton);
}
Also used : ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Person(com.vaadin.flow.data.bean.Person) Grid(com.vaadin.flow.component.grid.Grid) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) SelectionMode(com.vaadin.flow.component.grid.Grid.SelectionMode) TextField(com.vaadin.flow.component.textfield.TextField) Span(com.vaadin.flow.component.html.Span) Route(com.vaadin.flow.router.Route) NativeButton(com.vaadin.flow.component.html.NativeButton) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) Grid(com.vaadin.flow.component.grid.Grid) TextField(com.vaadin.flow.component.textfield.TextField) Person(com.vaadin.flow.data.bean.Person)

Example 14 with Person

use of com.vaadin.flow.data.bean.Person in project flow-components by vaadin.

the class HiddenEditorButtonsPage method createPerson.

private Person createPerson(int index) {
    Person person = new Person();
    person.setFirstName("Person " + index);
    person.setLastName(String.valueOf(index));
    return person;
}
Also used : Person(com.vaadin.flow.data.bean.Person)

Example 15 with Person

use of com.vaadin.flow.data.bean.Person in project flow-components by vaadin.

the class SortingPage method createSortingGrid.

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);
}
Also used : NativeButton(com.vaadin.flow.component.html.NativeButton) Person(com.vaadin.flow.data.bean.Person)

Aggregations

Person (com.vaadin.flow.data.bean.Person)28 Grid (com.vaadin.flow.component.grid.Grid)21 Div (com.vaadin.flow.component.html.Div)13 NativeButton (com.vaadin.flow.component.html.NativeButton)12 Route (com.vaadin.flow.router.Route)12 List (java.util.List)8 Column (com.vaadin.flow.component.grid.Grid.Column)7 ArrayList (java.util.ArrayList)7 TextField (com.vaadin.flow.component.textfield.TextField)6 Checkbox (com.vaadin.flow.component.checkbox.Checkbox)5 Button (com.vaadin.flow.component.button.Button)4 SelectionMode (com.vaadin.flow.component.grid.Grid.SelectionMode)4 Editor (com.vaadin.flow.component.grid.editor.Editor)4 Span (com.vaadin.flow.component.html.Span)4 Binder (com.vaadin.flow.data.binder.Binder)4 EmailValidator (com.vaadin.flow.data.validator.EmailValidator)4 Collection (java.util.Collection)4 Collections (java.util.Collections)4 WeakHashMap (java.util.WeakHashMap)4 HeaderRow (com.vaadin.flow.component.grid.HeaderRow)3