【问题标题】:Add sub header row / summarizing row in Vaadin Grid在 Vaadin Grid 中添加子标题行/汇总行
【发布时间】:2016-12-02 17:04:47
【问题描述】:

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

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

但是,如果我想在网格中添加一个特殊的行而不需要创建一个新的网格组件呢?一种是可见的数据分隔符。

【问题讨论】:

    标签: vaadin vaadin7 vaadin-grid


    【解决方案1】:

    根据您的需求和您的应用程序的复杂程度,您可以fake 一些(可能很小的)努力。您可以在下面找到一个可以帮助您入门的简单示例。

    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) 结果

    【讨论】:

    • 感谢您的详细回答。所以基本上我唯一的选择是添加一个额外的行,其中包含所需的信息 + CSS 样式作为特殊行?我真的希望能够像在标题中那样进行列连接。我更喜欢在一个下做多个网格
    • 从我读到的你也不能在网格内做colspan?虽然我喜欢网格的简单编辑设置,但显示的操作似乎受到很大限制。自 Grid 以来,该表基本上已被弃用,还是允许更好的选择?
    • @LML 据我所知,暂时是spanning is only supporter in headers/footers。你是对的,table is being deprecated since the grid,但我不认为它为此提供了更好的选择,它几乎是一样的。
    • 这对我的用例来说非常不幸。在显示分组段时,为了清晰起见,我将看到样式是如何工作的。感谢您的反馈
    • @LML 如果你还不熟悉它们,你可以先看看Vaadin docs to understand how themes are built are built and how they work。祝你好运
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    相关资源
    最近更新 更多