Search in sources :

Example 1 with NumberRenderer

use of com.vaadin.flow.data.renderer.NumberRenderer 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 2 with NumberRenderer

use of com.vaadin.flow.data.renderer.NumberRenderer in project flow-components by vaadin.

the class GridViewUsingRenderersPage method createBasicRenderers.

private void createBasicRenderers() {
    Grid<Item> grid = new Grid<>();
    grid.setItems(getShoppingCart());
    grid.addColumn(Item::getName).setHeader("Name");
    // NumberRenderer to render numbers in general
    grid.addColumn(new NumberRenderer<>(Item::getPrice, "$ %(,.2f", Locale.US, "$ 0.00")).setHeader("Price");
    // LocalDateTimeRenderer for date and time
    grid.addColumn(new LocalDateTimeRenderer<>(Item::getPurchaseDate, DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.MEDIUM))).setHeader("Purchase date and time").setFlexGrow(2);
    // LocalDateRenderer for dates
    grid.addColumn(new LocalDateRenderer<>(Item::getEstimatedDeliveryDate, DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM))).setHeader("Estimated delivery date");
    // Icons
    grid.addColumn(new IconRenderer<>(item -> item.getPrice() > 50 ? new Span("$$$") : new Span("$"), item -> ""));
    // NativeButtonRenderer for an easy clickable button,
    // without creating a component
    grid.addColumn(new NativeButtonRenderer<>("Remove", item -> {
        ListDataProvider<Item> dataProvider = (ListDataProvider<Item>) grid.getDataProvider();
        dataProvider.getItems().remove(item);
        dataProvider.refreshAll();
    })).setWidth("100px").setFlexGrow(0);
    grid.setId("grid-basic-renderers");
    addCard("Using renderers", "Using basic renderers", grid);
}
Also used : ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) LocalDateRenderer(com.vaadin.flow.data.renderer.LocalDateRenderer) Grid(com.vaadin.flow.component.grid.Grid) FormatStyle(java.time.format.FormatStyle) ItemGenerator(com.vaadin.flow.data.bean.ItemGenerator) IconRenderer(com.vaadin.flow.data.renderer.IconRenderer) Route(com.vaadin.flow.router.Route) NumberRenderer(com.vaadin.flow.data.renderer.NumberRenderer) LocalDateTimeRenderer(com.vaadin.flow.data.renderer.LocalDateTimeRenderer) List(java.util.List) Item(com.vaadin.flow.data.bean.Item) NativeButtonRenderer(com.vaadin.flow.data.renderer.NativeButtonRenderer) Locale(java.util.Locale) DateTimeFormatter(java.time.format.DateTimeFormatter) Span(com.vaadin.flow.component.html.Span) Item(com.vaadin.flow.data.bean.Item) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) Grid(com.vaadin.flow.component.grid.Grid) LocalDateRenderer(com.vaadin.flow.data.renderer.LocalDateRenderer) Span(com.vaadin.flow.component.html.Span) NumberRenderer(com.vaadin.flow.data.renderer.NumberRenderer)

Aggregations

Grid (com.vaadin.flow.component.grid.Grid)2 ListDataProvider (com.vaadin.flow.data.provider.ListDataProvider)2 NumberRenderer (com.vaadin.flow.data.renderer.NumberRenderer)2 Route (com.vaadin.flow.router.Route)2 Locale (java.util.Locale)2 ColumnTextAlign (com.vaadin.flow.component.grid.ColumnTextAlign)1 Column (com.vaadin.flow.component.grid.Grid.Column)1 SelectionMode (com.vaadin.flow.component.grid.Grid.SelectionMode)1 HeaderRow (com.vaadin.flow.component.grid.HeaderRow)1 Span (com.vaadin.flow.component.html.Span)1 TextField (com.vaadin.flow.component.textfield.TextField)1 Item (com.vaadin.flow.data.bean.Item)1 ItemGenerator (com.vaadin.flow.data.bean.ItemGenerator)1 Query (com.vaadin.flow.data.provider.Query)1 IconRenderer (com.vaadin.flow.data.renderer.IconRenderer)1 LocalDateRenderer (com.vaadin.flow.data.renderer.LocalDateRenderer)1 LocalDateTimeRenderer (com.vaadin.flow.data.renderer.LocalDateTimeRenderer)1 NativeButtonRenderer (com.vaadin.flow.data.renderer.NativeButtonRenderer)1 BigDecimal (java.math.BigDecimal)1 DecimalFormat (java.text.DecimalFormat)1