Home > front end >  Add header over one column in grid (Vaadin)
Add header over one column in grid (Vaadin)

Time:09-16

I am trying to joining columns in grid header. But the one feature for me is missing. I have created the grid for employees which are divided into the specific departments, but there are employees, which are in department alone.

Vaadin grid does not allow add header over one column.

So the result should looks like:

enter image description here

Is there any solution how can I do this even though Vaadin doesn't allow it directly?

Because if I tried it, it throws an exception

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.example.application.views.dashboard.DashboardView': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.application.views.dashboard.DashboardView]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Cannot join less than 2 cells

My code:

Grid<Person> grid = new Grid<>(Person.class, false);
Grid.Column<Person> john = grid.addColumn(..).setHeader("John");
Grid.Column<Person> fred = grid.addColumn(..).setHeader("Fred");
Grid.Column<Person> marcus = grid.addColumn(..).setHeader("Marcus");
Grid.Column<Person> adam = grid.addColumn(..).setHeader("Adam");
Grid.Column<Person> peter = grid.addColumn(..).setHeader("Peter");
Grid.Column<Person> eve = grid.addColumn(..).setHeader("Eve");

HeaderRow headerRow = grid.prependHeaderRow();
headerRow.join(john, fred).setText("engineers");
headerRow.join(marcus, adam, peter).setText("economists");
headerRow.join(eve).setText("charwoman"); //this is problematic row

CodePudding user response:

The point of joining columns is actually that. If you just want to put text in a prepended header row, get the cell for that column and set the text or the component on it.

new Grid<Map>(Map, false).tap {
    ['a', 'b', 'c', 'd'].each { c ->
        addColumn({ it.get(c) }).setKey(c).setHeader(c)
    }
    prependHeaderRow().tap {
        getCell(getColumnByKey('a')).setText('A')
        join(
            getColumnByKey('b'), 
            getColumnByKey('c')
        ).setText('B&C')
        getCell(getColumnByKey('d')).setText('D')
    }
    setItems([[a: 1, b: 2, c: 3],])
}
  • Related