Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

How to set alignment in Grid.

Jet Beray
6 years ago May 28, 2015 6:36am
Jens Jansson
6 years ago May 28, 2015 7:17am

You can do it with a CellStyleGenerator shown in the Sampler: http://demo.vaadin.com/sampler/#ui/grids-and-trees/grid/features. I copied the relevant parts of the sample below:

Java:

// Use a custom style for the number cells to align the text to right
sample.setCellStyleGenerator(new CellStyleGenerator() {
@Override
public String getStyle(CellReference cellReference) {
if (cellReference.getPropertyId().toString().contains("H")) {
return "align-right";
}
return null;
}
});

CSS:

.align-right {
  text-align: right;
}

Note that this code does right align on all columns starting on the letter 'H'. Replace the "contains" with a comparison to the name of the column if you just want to align one column.

 

Jet Beray
6 years ago Jun 01, 2015 9:50am
Jens Jansson
6 years ago Jun 01, 2015 10:31am
Jet Beray
6 years ago Jun 01, 2015 11:06am
Accelinn IT
6 years ago Oct 13, 2015 10:12am

Hi,

I'm trying to do the same, but the answer doesn't fit well for me ...

I have standard rules to align columns (right for numeric, center for boolean, etc ...)

Doing a CSS style generator to apply CSS class on each cell is maybe a little tricky ... In addition, the cellStyleGenerator constrain me to parse and detect for each cell against once by column ...

Doing this method today will be another constraint because if one day i need to really apply cell specific style (color, etc ...) i need to extend again and again the align generator ... It's not a very standard way ... 

No way to do the old and efficient column.setAlignment() ???

Ron de Waard
5 years ago Jul 07, 2016 8:46pm
Peter Hansson
5 years ago Oct 16, 2016 9:57am
K.-M. Hansche
5 years ago Feb 27, 2017 5:43pm

In Vaadin 8 the following works:

Column.setStyleGenerator(item -> "v-align-right");
Column.setStyleGenerator(item -> "v-align-left");
Column.setStyleGenerator(item -> "v-align-center");

 

Steven Zaluk
4 years ago Apr 11, 2017 1:51pm
Steven Zaluk
4 years ago Apr 11, 2017 2:59pm
Florian Schneider
4 years ago Oct 30, 2017 5:03pm
Kakuszi Gergely
4 years ago Dec 21, 2017 2:41pm
Karl Ninh
3 years ago Apr 02, 2018 3:39pm
Torsten Börner
3 years ago Aug 15, 2018 8:17am