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);
}
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);
}
Aggregations