2016-07-28 108 views
2

是否可以將彙總行添加到Vaadin Grid中?在Vaadin Grid中添加子標題行/彙總行

我目前有一個網格,有一個標題行來加入列,並在頂部給出一個很好的概述。不過,我想在整個網格中添加類似的標題以標記節的結尾。它似乎只能在標題部分添加標題,這將簡單地填滿網格的頭部。頁腳底部也是這樣。

但是,如果我想在網格內的特殊行而不必創建新的網格組件?一個將是一個可見的數據分隔符。

回答

2

根據你的需要和你的應用程序的複雜程度,你可以用一些(可能很小的)努力來做某件事。你可以在下面找到一個應該讓你開始的簡單例子。

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

+0

感謝您的詳細的解答。 因此,基本上我唯一的選擇是將額外的行添加所需的信息+作爲特殊行的CSS樣式? 我真的希望能夠像在標題中一樣進行列連接。我寧願在多個網格下互相做 – LML

+0

從我讀過的你也不能在網格中做colspan?雖然我喜歡網格的簡單編輯設置,但對顯示的操作似乎非常有限。該表格基本上已被棄用,或者它是否允許更好的選擇呢? – LML

+0

@LML據我所知,[spanning只是頁眉/頁腳中的支持者](https://vaadin.com/docs/-/part/framework/components/components-grid.html#components.grid.headerfooter。加入)暫時。你是正確的,[表格已被棄用,因爲網格](http://stackoverflow.com/questions/30134479/vaadin-grid-vs-table),但我不認爲它提供了更好的選擇,它'幾乎是一樣的。 – Morfic