Search in sources :

Example 1 with Column

use of com.vaadin.flow.component.grid.Grid.Column in project flow-components by vaadin.

the class GridColumnTest method createColumn_returnsNonNullAndBasicType.

@Test
public void createColumn_returnsNonNullAndBasicType() {
    Column column = new Grid<Person>().createColumn(TemplateRenderer.of(""), "");
    assertNotNull(column);
    Assert.assertEquals(Column.class, column.getClass());
}
Also used : Column(com.vaadin.flow.component.grid.Grid.Column) Test(org.junit.Test)

Example 2 with Column

use of com.vaadin.flow.component.grid.Grid.Column in project flow-components by vaadin.

the class GridViewBasicFeaturesPage method createBasicFeatures.

private void createBasicFeatures() {
    final int baseYear = 2015;
    final int numberOfYears = 5;
    DecimalFormat dollarFormat = new DecimalFormat("$#,##0.00");
    Grid<CompanyBudgetHistory> grid = new Grid<>();
    ListDataProvider<CompanyBudgetHistory> list = CompanyBudgetHistory.getBudgetDataProvider(baseYear, numberOfYears);
    grid.setDataProvider(list);
    grid.setColumnReorderingAllowed(true);
    Column<CompanyBudgetHistory> companyNameColumn = grid.addColumn(CompanyBudgetHistory::getCompany).setHeader("Company");
    companyNameColumn.setWidth("200px");
    grid.setSelectionMode(SelectionMode.SINGLE);
    HeaderRow topHeader = grid.prependHeaderRow();
    IntStream.range(baseYear, baseYear + numberOfYears).forEach(year -> {
        BigDecimal firstHalfSum = list.fetch(new Query<>()).collect(Collectors.toList()).stream().map(budgetHistory -> budgetHistory.getFirstHalfOfYear(year)).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal secondHalfSum = list.fetch(new Query<>()).collect(Collectors.toList()).stream().map(budgetHistory -> budgetHistory.getSecondHalfOfYear(year)).reduce(BigDecimal.ZERO, BigDecimal::add);
        Column<?> firstHalfColumn = grid.addColumn(new NumberRenderer<>(budgetHistory -> budgetHistory.getFirstHalfOfYear(year), dollarFormat)).setHeader("H1").setTextAlign(ColumnTextAlign.END).setFooter(dollarFormat.format(firstHalfSum)).setComparator((p1, p2) -> p1.getFirstHalfOfYear(year).compareTo(p2.getFirstHalfOfYear(year)));
        Column<?> secondHalfColumn = grid.addColumn(new NumberRenderer<>(budgetHistory -> budgetHistory.getSecondHalfOfYear(year), dollarFormat)).setHeader("H2").setTextAlign(ColumnTextAlign.END).setFooter(dollarFormat.format(secondHalfSum)).setComparator((p1, p2) -> p1.getSecondHalfOfYear(year).compareTo(p2.getSecondHalfOfYear(year)));
        topHeader.join(firstHalfColumn, secondHalfColumn).setText(year + "");
    });
    HeaderRow filteringHeader = grid.appendHeaderRow();
    TextField filteringField = new TextField();
    filteringField.addValueChangeListener(event -> {
        list.setFilter(CompanyBudgetHistory::getCompany, company -> {
            if (company == null) {
                return false;
            }
            String companyLower = company.toLowerCase(Locale.ENGLISH);
            String filterLower = event.getValue().toLowerCase(Locale.ENGLISH);
            return companyLower.contains(filterLower);
        });
    });
    filteringField.setPlaceholder("Filter");
    filteringField.setWidth("100%");
    filteringHeader.getCell(companyNameColumn).setComponent(filteringField);
    grid.setId("grid-basic-feature");
    addCard("Basic Features", "Grid Basic Features Demo", grid);
}
Also used : IntStream(java.util.stream.IntStream) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) Grid(com.vaadin.flow.component.grid.Grid) Query(com.vaadin.flow.data.provider.Query) Collection(java.util.Collection) DecimalFormat(java.text.DecimalFormat) HashMap(java.util.HashMap) SelectionMode(com.vaadin.flow.component.grid.Grid.SelectionMode) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) NumberRenderer(com.vaadin.flow.data.renderer.NumberRenderer) BigDecimal(java.math.BigDecimal) Column(com.vaadin.flow.component.grid.Grid.Column) HeaderRow(com.vaadin.flow.component.grid.HeaderRow) Locale(java.util.Locale) Map(java.util.Map) ColumnTextAlign(com.vaadin.flow.component.grid.ColumnTextAlign) TextField(com.vaadin.flow.component.textfield.TextField) DecimalFormat(java.text.DecimalFormat) Grid(com.vaadin.flow.component.grid.Grid) BigDecimal(java.math.BigDecimal) HeaderRow(com.vaadin.flow.component.grid.HeaderRow) TextField(com.vaadin.flow.component.textfield.TextField)

Example 3 with Column

use of com.vaadin.flow.component.grid.Grid.Column 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)

Example 4 with Column

use of com.vaadin.flow.component.grid.Grid.Column in project flow-components by vaadin.

the class GridViewEditorPage method createNotBufferedDynamicEditor.

private void createNotBufferedDynamicEditor() {
    Div message = new Div();
    message.setId("not-buffered-dynamic-editor-msg");
    Grid<Person> grid = new Grid<>();
    List<Person> persons = new ArrayList<>();
    persons.addAll(createItems());
    grid.setItems(persons);
    Column<Person> nameColumn = grid.addColumn(Person::getFirstName).setHeader("Name");
    Column<Person> subscriberColumn = grid.addColumn(Person::isSubscriber).setHeader("Subscriber");
    Column<Person> emailColumn = grid.addColumn(Person::getEmail).setHeader("E-mail");
    Binder<Person> binder = new Binder<>(Person.class);
    Editor<Person> editor = grid.getEditor();
    editor.setBinder(binder);
    TextField field = new TextField();
    // Close the editor in case of backward navigation between components
    field.getElement().addEventListener("keydown", event -> grid.getEditor().closeEditor()).setFilter("event.key === 'Tab' && event.shiftKey");
    binder.bind(field, "firstName");
    nameColumn.setEditorComponent(field);
    Checkbox checkbox = new Checkbox();
    binder.bind(checkbox, "subscriber");
    subscriberColumn.setEditorComponent(checkbox);
    // Close the editor in case of forward navigation between components
    checkbox.getElement().addEventListener("keydown", event -> {
        if (!checkbox.getValue()) {
            grid.getEditor().closeEditor();
        }
    }).setFilter("event.key === 'Tab' && !event.shiftKey");
    TextField emailField = new TextField();
    emailColumn.setEditorComponent(item -> {
        if (item.isSubscriber()) {
            binder.bind(emailField, "email");
            return emailField;
        } else {
            return null;
        }
    });
    // Close the editor in case of forward navigation between components
    emailField.getElement().addEventListener("keydown", event -> grid.getEditor().closeEditor()).setFilter("event.key === 'Tab' && !event.shiftKey");
    grid.addItemDoubleClickListener(event -> {
        grid.getEditor().editItem(event.getItem());
        field.focus();
    });
    // Re-validates the editors every time something changes on the Binder.
    // This is needed for the email column to turn into nothing when the
    // checkbox is deselected, for example.
    binder.addValueChangeListener(event -> {
        // Only updates from the client-side should be taken into account
        if (event.isFromClient()) {
            grid.getEditor().refresh();
        }
    });
    grid.addItemClickListener(event -> {
        if (binder.getBean() != null) {
            message.setText(binder.getBean().getFirstName() + ", " + binder.getBean().isSubscriber() + ", " + binder.getBean().getEmail());
        }
    });
    grid.setId("not-buffered-dynamic-editor");
    addCard("Grid Editor", "Dynamic Editor in Not Buffered Mode", message, 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) ArrayList(java.util.ArrayList) Div(com.vaadin.flow.component.html.Div) Binder(com.vaadin.flow.data.binder.Binder) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) TextField(com.vaadin.flow.component.textfield.TextField) Person(com.vaadin.flow.data.bean.Person)

Example 5 with Column

use of com.vaadin.flow.component.grid.Grid.Column in project flow-components by vaadin.

the class GridViewEditorPage method createNotBufferedEditor.

private void createNotBufferedEditor() {
    Div message = new Div();
    message.setId("not-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);
    grid.getEditor().setBinder(binder);
    TextField field = new TextField();
    // Close the editor in case of backward between components
    field.getElement().addEventListener("keydown", event -> grid.getEditor().closeEditor()).setFilter("event.key === 'Tab' && event.shiftKey");
    binder.bind(field, "firstName");
    nameColumn.setEditorComponent(field);
    Checkbox checkbox = new Checkbox();
    binder.bind(checkbox, "subscriber");
    subscriberColumn.setEditorComponent(checkbox);
    // Close the editor in case of forward navigation between
    checkbox.getElement().addEventListener("keydown", event -> grid.getEditor().closeEditor()).setFilter("event.key === 'Tab' && !event.shiftKey");
    grid.addItemDoubleClickListener(event -> {
        grid.getEditor().editItem(event.getItem());
        field.focus();
    });
    grid.addItemClickListener(event -> {
        if (binder.getBean() != null) {
            message.setText(binder.getBean().getFirstName() + ", " + binder.getBean().isSubscriber());
        }
    });
    grid.setId("not-buffered-editor");
    addCard("Grid Editor", "Editor in Not Buffered Mode", message, grid);
}
Also used : Div(com.vaadin.flow.component.html.Div) 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) Binder(com.vaadin.flow.data.binder.Binder) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) Grid(com.vaadin.flow.component.grid.Grid) TextField(com.vaadin.flow.component.textfield.TextField) Person(com.vaadin.flow.data.bean.Person)

Aggregations

Column (com.vaadin.flow.component.grid.Grid.Column)13 Route (com.vaadin.flow.router.Route)11 Grid (com.vaadin.flow.component.grid.Grid)10 Person (com.vaadin.flow.data.bean.Person)7 Div (com.vaadin.flow.component.html.Div)6 TextField (com.vaadin.flow.component.textfield.TextField)6 Button (com.vaadin.flow.component.button.Button)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Checkbox (com.vaadin.flow.component.checkbox.Checkbox)4 Editor (com.vaadin.flow.component.grid.editor.Editor)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 HeaderRow (com.vaadin.flow.component.grid.HeaderRow)3 Span (com.vaadin.flow.component.html.Span)3 WeakHashMap (java.util.WeakHashMap)3 SelectionMode (com.vaadin.flow.component.grid.Grid.SelectionMode)2 HeaderCell (com.vaadin.flow.component.grid.HeaderRow.HeaderCell)2