I'm using Grid
table in Vaadin for data representation.
For that I'm trying to figure out the following two issues:
1.) How to disable the sort function in the Header of each column
2.) How to set the color of one column in a Grid
table
First of all, I find the Vaadin docs a good place to start looking for help. For the rest of the exercise, suppose we have a Grid
with 3 simple columns c1, c2 & c3:
Grid grid = new Grid();
grid.addColumn("c1", String.class);
grid.addColumn("c2", String.class);
grid.addColumn("c3", String.class);
1.) How to disable the sort function in the Header of each column
Iterate through each column and set its sortable property to false:
for (Grid.Column column : grid.getColumns()) {
column.setSortable(false);
}
Or just get that one column you want and set its sortable property:
grid.getColumn("c1").setSortable(false);
2.) How to set the color of one column in a Grid table
Similar to how it was done with the table, you need to define your CSS style in your theme:
@import "../valo/valo.scss";
@mixin mytheme {
@include valo;
// Insert your own theme rules here
.v-grid-cell.green {
background: #33BB00;
}
}
And use a CellStyleGenerator to apply the style to your desired column:
grid.setCellStyleGenerator(new Grid.CellStyleGenerator() {
@Override
public String getStyle(Grid.CellReference cellReference) {
if ("c1".equals(cellReference.getPropertyId())) {
return "green";
} else {
return null;
}
}
});
Which should generate something along the lines of:
Here is a small supplement to Morfic good answer:
1.) How to disable the sort function in the Header of each column
Grid asks the Container if a column is sortable (see appendColumn-method in Grid). If you want to disable sorting for all columns, you can override getSortableContainerPropertyIds in your Container and return an empty collection:
container = new BeanContainer<String, Dive>(Dive.class) {
@Override
public Collection<?> getSortableContainerPropertyIds() {
LinkedList<Object> sortables = new LinkedList<Object>();
return sortables;
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With