I am using Vaadin and I would like to set backgroung color to specific cell in my grid/table or if there is no possible to set background color to specific cell I would like to at least set a font color to specific cell in grid/table. The code TableView where I have got a grid/table is below:
package com.trading.scraper;
import com.vaadin.navigator.View;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Grid;
import com.vaadin.ui.VerticalLayout;
import java.util.Arrays;
import java.util.List;
class TableView extends CustomComponent implements View {
static final String NAME = "Stock table";
TableView() {
final VerticalLayout layout = new VerticalLayout();
List<Stock> people = Arrays.asList(
new Stock("1", "2", "1"),
new Stock("3", "5", "2"),
new Stock("1", "3", "4"));
Grid<Stock> grid = new Grid<>();
grid.setWidth(100, Unit.PERCENTAGE);
grid.setItems(people);
grid.addColumn(Stock::getValue1).setCaption("Value1");
grid.addColumn(Stock::getValue2).setCaption("Value2");
grid.addColumn(Stock::getValue3).setCaption("Value3");
layout.addComponents(grid);
setCompositionRoot(layout);
}
}
The content class for grid/table is:
package com.trading.scraper;
public class Stock {
private String value1;
private String value2;
private String value3;
public String getValue1() {
return value1;
}
public void setValue1(String value1) {
this.value1 = value1;
}
public String getValue2() {
return value2;
}
public void setValue2(String value2) {
this.value2 = value2;
}
public String getValue3() {
return value3;
}
public void setValue3(String value3) {
this.value3 = value3;
}
public Stock() {
}
Stock(String value1, String value2, String value3) {
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
}
}
If it is possible to set background color to specific cell or at least set font color and you know how to do it please write. Eg where cell's value in grid/table is "1" I would like to make it red but if eg cell's value is "5" I would like to make it green and if cell's value is "3" I would like to make it yellow. Thank you very much.
You have two options to style the content of a Grid
in Vaadin.
First, to set the style of a row, you can do the following:
grid.setStyleGenerator(stockRow ->
"1".equals(stockRow.getValue1()) ? "highlighted" : null);
The css class highlighted
will be added to each grid row, were the condition applies. You can then style the row in SCSS using the following selector:
.v-grid-row.highlighted {
color: red;
}
To select and style the cells, you need to select the td's:
.v-treegrid-row.highlighted > td {
color: red;
}
I guess you'd want to style the cells directly so it would be more appropriate to set the style generator on a per-column mode as in following example:
grid
.addColumn(Stock::getValue1)
.setCaption("Value1")
.setStyleGenerator(stockRow -> {
switch (stockRow.getValue1()) {
case "1": return "red";
case "3": return "yellow";
case "5": return "green";
default: return null;
}
});
You can then style the cells in SCSS:
.v-grid-cell.red {
color: red;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.