/** * generates a simple totalCount footer * * @param grid * @param items */ private void initFooterRow(final Grid<Inhabitants> grid, List<Inhabitants> items) { final FooterRow footerRow = grid.appendFooterRow(); footerRow.getCell("id") .setHtml("total:"); final FooterCell footerCell = footerRow.join("gender", "name", "bodySize", "birthday", "onFacebook", "country"); // inital total count footerCell.setHtml("<b>" + items.size() + "</b>"); // filter change count recalculate grid.getDataProvider().addDataProviderListener(event -> { List<Inhabitants> data = event.getSource() .fetch(new Query<>()).collect(Collectors.toList()); footerCell.setHtml("<b>" + data.size() + "</b>"); }); }
@SuppressWarnings("unchecked") @Override public FooterCell join(P... propertyIdsToMerge) { ObjectUtils.argumentNotNull(propertyIdsToMerge, "Property ids to merge must be not null"); return row.join(Arrays.asList(propertyIdsToMerge).stream().map(id -> converter.apply(id)) .collect(Collectors.toList()).toArray(new String[0])); }
@SuppressWarnings("unchecked") private <ITEM, RENDERER_TYPE, PROPERTY_TYPE> void refreshColumnSummary(Grid<ITEM> grid, ColumnDefinition definition) { FooterCell footerCell = grid.getFooterRow(0).getCell(definition.getPropertyName()); footerCell.setStyleName(definition.getColumnAlignment().getAlignmentName()); if (definition.isStaticTextSummarizable()) { String translatedStaticText = gridConfigurationProvider .resolveTranslationKey(definition.getStaticTextSummarizerTranslationKey()); footerCell.setText(translatedStaticText); } else { Summarizer<PROPERTY_TYPE> summarizerFor = getSummarizerFor(definition); if (ListDataProvider.class.isAssignableFrom(grid.getDataProvider().getClass())) { ListDataProvider<ITEM> dataProvider = ListDataProvider.class.cast(grid.getDataProvider()); List<PROPERTY_TYPE> propertyValues = (List<PROPERTY_TYPE>) dataProvider.getItems().stream() .map(item -> invokeRead(definition.getReadMethod(), item)).collect(Collectors.toList()); if (summarizerFor.canSummarize(definition, propertyValues)) { String summaryText = summarizerFor.summarize(definition, propertyValues); footerCell.setText(summaryText); } else { footerCell.setText("-"); } } } }
/** * Adds the generic grid footer row configured in the footer configs * * @param gridHeaderCell * the grid header cell * @param myCell * the my cell */ public static void addGenericGridFooterRow(final FooterCell gridHeaderCell, final Cell myCell) { if (gridHeaderCell.getCellType() .equals(GridStaticCellType.TEXT)) { myCell.setCellValue(gridHeaderCell.getText()); } else if (gridHeaderCell.getCellType() .equals(GridStaticCellType.HTML)) { myCell.setCellValue(gridHeaderCell.getHtml()); } else if (gridHeaderCell.getCellType() .equals(GridStaticCellType.WIDGET)) { myCell.setCellValue(gridHeaderCell.getComponent() .toString()); } }
@Override public FooterCell getCell(P propertyId) { return row.getCell(converter.apply(propertyId)); }
@Override public FooterCell join(Set<FooterCell> cellsToMerge) { return row.join(cellsToMerge); }
@Override public FooterCell join(FooterCell... cellsToMerge) { return row.join(cellsToMerge); }
@Override protected void init(final VaadinRequest request) { // Creating the Export Tool Bar MenuBar exportToolBar = createToolBar(); final VerticalLayout layout = new VerticalLayout(); layout.setSizeFull(); // Adding the Export Tool Bar to the Layout layout.addComponent(exportToolBar); /********* * Adding Components to the Layout namely Tables, Grids and Tree Table *******/ this.gridDefault = new Grid<>(DataModel.class); this.gridDefault.setDataProvider(new ListDataProvider<>(DataModelGenerator.generate(20))); this.gridDefault.setSizeFull(); this.gridDefault.setColumns(this.visibleColumns); this.gridMergedCells = new Grid<>(DataModel.class); this.gridMergedCells.setDataProvider(new ListDataProvider<>(DataModelGenerator.generate(20))); this.gridMergedCells.setColumns(this.visibleColumns); this.gridMergedCells.setSizeFull(); HeaderRow headerRow = this.gridMergedCells.addHeaderRowAt(0); HeaderCell joinHeaderColumns1 = headerRow.join("country", "productType"); joinHeaderColumns1.setText("mergedCell"); HeaderCell joinHeaderColumns2 = headerRow.join("cheapest", "contractor"); joinHeaderColumns2.setText("mergedCell"); FooterRow footerRow1 = this.gridMergedCells.addFooterRowAt(0); FooterCell joinFooterColumns1 = footerRow1.join("country", "productType"); joinFooterColumns1.setText("mergedCell"); FooterCell joinFooterColumns2 = footerRow1.join("cheapest", "contractor"); joinFooterColumns2.setText("mergedCell"); FooterRow footerRow2 = this.gridMergedCells.addFooterRowAt(0); for (int i = 0; i < this.visibleColumns.length; i++) { footerRow2.getCell(this.visibleColumns[i]) .setText(this.columnHeaders[i]); } this.gridFrozenColumns = new Grid<>(DataModel.class); this.gridFrozenColumns.setDataProvider(new ListDataProvider<>(DataModelGenerator.generate(20))); this.gridFrozenColumns.setColumns(this.visibleColumns); this.gridFrozenColumns.getColumn("country") .setWidth(300); this.gridFrozenColumns.getColumn("productType") .setWidth(300); this.gridFrozenColumns.getColumn("catalogue") .setWidth(300); this.gridFrozenColumns.setSizeFull(); this.gridFrozenColumns.setFrozenColumnCount(3); TabSheet tabSheet = new TabSheet(); tabSheet.setSizeFull(); tabSheet.addTab(this.gridDefault, "Grid (Default)"); tabSheet.addTab(this.gridMergedCells, "Grid (Merged Cells)"); tabSheet.addTab(this.gridFrozenColumns, "Grid (Frozen Columns&Rows)"); layout.addComponent(tabSheet); layout.setExpandRatio(tabSheet, 1); /********* * Adding Components to the Layout namely Tables, Grids and Tree Table *******/ /********* * Adding the above data to the containers or components *******/ setContent(layout); }
/** * Returns the cell on this row corresponding to the given property id. * @param propertyId the id of the property/column whose footer cell to get, not null * @return the footer cell * @throws IllegalArgumentException if there is no such column in the grid */ FooterCell getCell(P propertyId);
/** * Merges cells corresponding to the given property ids in the row. Original cells are hidden, and new merged * cell is shown instead. The cell has a width of all merged cells together, inherits styles of the first merged * cell but has empty caption. * @param propertyIdsToMerge the ids of the property/column of the cells that should be merged. The cells should * not be merged to any other cell set. * @return the remaining visible cell after the merge */ @SuppressWarnings("unchecked") FooterCell join(P... propertyIdsToMerge);
/** * Merges column cells in the row. Original cells are hidden, and new merged cell is shown instead. The cell has * a width of all merged cells together, inherits styles of the first merged cell but has empty caption. * @param cellsToMerge the cells which should be merged. The cells should not be merged to any other cell set. * @return the remaining visible cell after the merge */ FooterCell join(Set<FooterCell> cellsToMerge);
/** * Merges column cells in the row. Original cells are hidden, and new merged cell is shown instead. The cell has * a width of all merged cells together, inherits styles of the first merged cell but has empty caption. * @param cellsToMerge the cells which should be merged. The cells should not be merged to any other cell set. * @return the remaining visible cell after the merge */ FooterCell join(FooterCell... cellsToMerge);