Search in sources :

Example 1 with Person

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

the class GridViewBasicPage method createCallBackDataProvider.

private void createCallBackDataProvider() {
    Grid<Person> grid = new Grid<>();
    /*
         * This Data Provider doesn't load all items into the memory right away.
         * Grid will request only the data that should be shown in its current
         * view "window". The Data Provider will use callbacks to load only a
         * portion of the data.
         */
    PeopleGenerator generator = new PeopleGenerator();
    grid.setItems(DataProvider.fromCallbacks(query -> IntStream.range(query.getOffset(), query.getOffset() + query.getLimit()).mapToObj(index -> generator.createPerson(index + 1)), query -> 100 * 1000 * 1000));
    grid.addColumn(Person::getFirstName).setHeader("Name");
    grid.addColumn(Person::getAge).setHeader("Age");
    grid.setId("lazy-loading");
    addCard("Grid with lazy loading", grid);
}
Also used : IntStream(java.util.stream.IntStream) PeopleGenerator(com.vaadin.flow.data.bean.PeopleGenerator) List(java.util.List) Person(com.vaadin.flow.data.bean.Person) Grid(com.vaadin.flow.component.grid.Grid) NativeButtonRenderer(com.vaadin.flow.data.renderer.NativeButtonRenderer) DataProvider(com.vaadin.flow.data.provider.DataProvider) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) SelectionMode(com.vaadin.flow.component.grid.Grid.SelectionMode) Route(com.vaadin.flow.router.Route) Grid(com.vaadin.flow.component.grid.Grid) Person(com.vaadin.flow.data.bean.Person) PeopleGenerator(com.vaadin.flow.data.bean.PeopleGenerator)

Example 2 with Person

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

the class GridViewBasicPage method createDisabledGrid.

private void createDisabledGrid() {
    Grid<Person> grid = new Grid<>();
    List<Person> people = createItems(500);
    grid.setItems(people);
    grid.addColumn(Person::getFirstName).setHeader("Name");
    grid.addColumn(Person::getAge).setHeader("Age");
    grid.addColumn(new NativeButtonRenderer<>("Button")).setHeader("Action");
    grid.setSelectionMode(SelectionMode.SINGLE);
    // The selection and action button won't work, but the scrolling will
    grid.setEnabled(false);
    NativeButton toggleEnable = new NativeButton("Toggle enable", evt -> grid.setEnabled(!grid.isEnabled()));
    toggleEnable.setId("disabled-grid-toggle-enable");
    Div div = new Div(toggleEnable);
    grid.setId("disabled-grid");
    addCard("Disabled grid", grid, div);
}
Also used : Div(com.vaadin.flow.component.html.Div) NativeButtonRenderer(com.vaadin.flow.data.renderer.NativeButtonRenderer) NativeButton(com.vaadin.flow.component.html.NativeButton) Grid(com.vaadin.flow.component.grid.Grid) Person(com.vaadin.flow.data.bean.Person)

Example 3 with Person

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

the class GridViewClickListenersPage method createClickListener.

private void createClickListener() {
    Div message = new Div();
    message.setId("clicked-item");
    Grid<Person> grid = new Grid<>();
    grid.setItems(getItems());
    grid.addColumn(Person::getFirstName).setHeader("Name");
    grid.addColumn(Person::getAge).setHeader("Age");
    // Disable selection: will receive only click events instead
    grid.setSelectionMode(SelectionMode.NONE);
    grid.addItemClickListener(event -> message.setText("Clicked Item: " + event.getItem().getFirstName()));
    grid.setId("item-click-listener");
    message.addClickListener(event -> message.setText(""));
    addCard("Click Listeners", "Item Click Listener", message, grid);
}
Also used : Div(com.vaadin.flow.component.html.Div) Grid(com.vaadin.flow.component.grid.Grid) Person(com.vaadin.flow.data.bean.Person)

Example 4 with Person

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

the class GridViewConfiguringColumnsPage method createColumnApiExample.

private void createColumnApiExample() {
    Grid<Person> grid = new Grid<>();
    GridSelectionModel<Person> selectionMode = grid.setSelectionMode(SelectionMode.MULTI);
    grid.setItems(getItems());
    Column<Person> idColumn = grid.addColumn(Person::getId).setHeader("ID").setFlexGrow(0).setWidth("75px");
    grid.addColumn(Person::getFirstName).setHeader("Name").setResizable(true);
    // Setting a column-key allows fetching the column later
    grid.addColumn(Person::getAge).setHeader("Age").setKey("age");
    grid.getColumnByKey("age").setResizable(true);
    NativeButton idColumnVisibility = new NativeButton("Toggle visibility of the ID column");
    idColumnVisibility.addClickListener(event -> idColumn.setVisible(!idColumn.isVisible()));
    NativeButton userReordering = new NativeButton("Toggle user reordering of columns");
    userReordering.addClickListener(event -> grid.setColumnReorderingAllowed(!grid.isColumnReorderingAllowed()));
    NativeButton freezeIdColumn = new NativeButton("Toggle frozen state of ID column");
    freezeIdColumn.addClickListener(event -> idColumn.setFrozen(!idColumn.isFrozen()));
    NativeButton freezeSelectionColumn = new NativeButton("Toggle frozen state of selection column");
    GridMultiSelectionModel<?> multiSlection = (GridMultiSelectionModel<?>) selectionMode;
    freezeSelectionColumn.addClickListener(event -> multiSlection.setSelectionColumnFrozen(!multiSlection.isSelectionColumnFrozen()));
    RadioButtonGroup<ColumnTextAlign> alignments = new RadioButtonGroup<>();
    alignments.setItems(ColumnTextAlign.values());
    alignments.setLabel("Text alignment for the Age column");
    alignments.setValue(ColumnTextAlign.START);
    alignments.addValueChangeListener(event -> grid.getColumnByKey("age").setTextAlign(event.getValue()));
    grid.setId("column-api-example");
    idColumnVisibility.setId("toggle-id-column-visibility");
    userReordering.setId("toggle-user-reordering");
    freezeIdColumn.setId("toggle-id-column-frozen");
    freezeSelectionColumn.setId("toggle-selection-column-frozen");
    alignments.setId("toggle-text-align");
    addCard("Configuring columns", "Column API example", grid, new VerticalLayout(idColumnVisibility, userReordering, freezeIdColumn, freezeSelectionColumn, alignments));
}
Also used : NativeButton(com.vaadin.flow.component.html.NativeButton) GridMultiSelectionModel(com.vaadin.flow.component.grid.GridMultiSelectionModel) Grid(com.vaadin.flow.component.grid.Grid) RadioButtonGroup(com.vaadin.flow.component.radiobutton.RadioButtonGroup) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) Person(com.vaadin.flow.data.bean.Person) ColumnTextAlign(com.vaadin.flow.component.grid.ColumnTextAlign)

Example 5 with Person

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

the class GridViewEditorPage method createBufferedEditor.

private void createBufferedEditor() {
    Div message = new Div();
    message.setId("buffered-editor-msg");
    Grid<Person> grid = new Grid<>();
    List<Person> persons = getItems();
    grid.setItems(persons);
    Column<Person> nameColumn = grid.addColumn(Person::getFirstName).setHeader("Name");
    Column<Person> subscriberColumn = grid.addColumn(Person::isSubscriber).setHeader("Subscriber");
    Binder<Person> binder = new Binder<>(Person.class);
    Editor<Person> editor = grid.getEditor();
    editor.setBinder(binder);
    editor.setBuffered(true);
    Div validationStatus = new Div();
    validationStatus.setId("validation");
    TextField field = new TextField();
    binder.forField(field).withValidator(name -> name.startsWith("Person"), "Name should start with Person").withStatusLabel(validationStatus).bind("firstName");
    nameColumn.setEditorComponent(field);
    Checkbox checkbox = new Checkbox();
    binder.bind(checkbox, "subscriber");
    subscriberColumn.setEditorComponent(checkbox);
    Collection<Button> editButtons = Collections.newSetFromMap(new WeakHashMap<>());
    Column<Person> editorColumn = grid.addComponentColumn(person -> {
        Button edit = new Button("Edit");
        edit.addClassName("edit");
        edit.addClickListener(e -> {
            editor.editItem(person);
            field.focus();
        });
        edit.setEnabled(!editor.isOpen());
        editButtons.add(edit);
        return edit;
    });
    editor.addOpenListener(e -> editButtons.stream().forEach(button -> button.setEnabled(!editor.isOpen())));
    editor.addCloseListener(e -> editButtons.stream().forEach(button -> button.setEnabled(!editor.isOpen())));
    Button save = new Button("Save", e -> editor.save());
    save.addClassName("save");
    Button cancel = new Button("Cancel", e -> editor.cancel());
    cancel.addClassName("cancel");
    // Add a keypress listener that listens for an escape key up event.
    // Note! some browsers return key as Escape and some as Esc
    grid.getElement().addEventListener("keyup", event -> editor.cancel()).setFilter("event.key === 'Escape' || event.key === 'Esc'");
    Div buttons = new Div(save, cancel);
    editorColumn.setEditorComponent(buttons);
    editor.addSaveListener(event -> message.setText(event.getItem().getFirstName() + ", " + event.getItem().isSubscriber()));
    grid.setId("buffered-editor");
    addCard("Grid Editor", "Editor in Buffered Mode", message, validationStatus, grid);
}
Also used : Grid(com.vaadin.flow.component.grid.Grid) EmailValidator(com.vaadin.flow.data.validator.EmailValidator) Collection(java.util.Collection) Editor(com.vaadin.flow.component.grid.editor.Editor) Binder(com.vaadin.flow.data.binder.Binder) Div(com.vaadin.flow.component.html.Div) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) List(java.util.List) Button(com.vaadin.flow.component.button.Button) Column(com.vaadin.flow.component.grid.Grid.Column) Person(com.vaadin.flow.data.bean.Person) Collections(java.util.Collections) TextField(com.vaadin.flow.component.textfield.TextField) WeakHashMap(java.util.WeakHashMap) Grid(com.vaadin.flow.component.grid.Grid) Div(com.vaadin.flow.component.html.Div) Binder(com.vaadin.flow.data.binder.Binder) Button(com.vaadin.flow.component.button.Button) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) TextField(com.vaadin.flow.component.textfield.TextField) 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