vaadin - 在 Vaadin 网格中添加子标题行/汇总行

标签 vaadin vaadin7 vaadin-grid

是否可以将汇总行添加到 Vaadin 网格中?

我目前有一个网格,它有一个标题行来连接列,并在顶部提供一个很好的概述。但是,我想在整个网格中添加类似的标题,以标记部分的结尾。似乎只能在标题部分添加标题,这将简单地填充网格的头部。页脚在底部执行相同的操作。

但是,如果我想在网格中添加一个特殊行,而无需创建新的网格组件,该怎么办?一个可见的数据分隔符。

最佳答案

根据您的需求以及应用程序的复杂程度,您可以通过一些(可能很小的)努力来伪造某些内容。您可以在下面找到一个可以帮助您入门的简单示例。

1) 与 BeanItemContainer 一起使用的通用类来显示两个类别的行

public abstract class Row {
    private String name;
    private int amount;

    public Row(String name, int amount) {
        this.name = name;
        this.amount = amount;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    // provide a custom style/type for the current row
    public abstract String getRowType();
}

2) 常规产品行

public class ProductRow extends Row {
    public ProductRow(String name, int amount) {
        super(name, amount);
    }

    @Override
    public String getRowType() {
        return "product-row";
    }
}

3) 特殊行显示上一批产品的总计

public class TotalRow extends Row {
    public TotalRow(int sum) {
        super("Total", sum);
    }

    @Override
    public String getRowType() {
        return "total-row";
    }
}

4)网格本身

public class GridWithIntermediateRowsComponent extends VerticalLayout {
    private static final String[] AVAILABLE_PRODUCTS = new String[]{"Banana", "Apple", "Coconut", "Pineapple", "Melon"};
    private Random random = new Random();

    public GridWithIntermediateRowsComponent() {
        BeanItemContainer<Row> container = new BeanItemContainer<>(Row.class);
        Grid grid = new Grid(container);

        // show only the relevant columns, the style one is used only to change the background
        grid.setColumns("name", "amount");

        // set a style generator so we can draw the "total" rows differently
        grid.setCellStyleGenerator(row -> ((Row) row.getItemId()).getRowType());

        // create some dummy data to display
        for (int i = 0; i < random.nextInt(10) + 1; i++) {
            container.addAll(createItemBatch(random.nextInt(5) + 1));
        }

        addComponent(grid);
    }

    private List<Row> createItemBatch(int total) {
        List<Row> rows = new ArrayList<>(total + 1);

        // add a batch of products
        String product = AVAILABLE_PRODUCTS[random.nextInt(AVAILABLE_PRODUCTS.length)];
        for (int i = 0; i < total; i++) {
            rows.add(new ProductRow(product, random.nextInt(100) + 1));
        }

        // calculate and add a "total row"
        rows.add(calculateTotal(rows));

        return rows;
    }

    private Row calculateTotal(List<Row> rows) {
        return new TotalRow(rows.stream().mapToInt(Row::getAmount).sum());
    }
}

5)主题样式

@mixin mytheme {
  @include valo;
  // Insert your own theme rules here

  .v-grid-row > td.total-row {
    background-color: #c4e7b7;
    font-weight: bold;
  }
}

6) 结果

Custom intermediate total rows

关于vaadin - 在 Vaadin 网格中添加子标题行/汇总行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38634094/

相关文章:

java - Vaadin ComboBox 将文本字段宽度设置为 0(CSS 阴影 dom)

java - 使用水平布局的 Vaadin 图标和标签不在一行中

vaadin - 如何将点击监听器或上下文菜单添加到 Vaadin 的网格列标题

css - 如何摆脱 shadow-dom 中的用户代理样式表

vaadin - 使用设计器创建的单选按钮组不会触发changeValueEvent

java - Vaadin 在填充后设置组合框值

java - Vaadin 浏览器框架不工作

java - 我如何在 Vaadin 7 中使用 dataProvider

css - 瓦丁 8.2.0 : How to vertical align image in grid cell

java - 在不使用 Maven 的情况下在 NetBeans 上使用 Vaadin