Hi,
To change the style of a cell, you need to use a
CellStyleGenerator
. There is no way to only style items added by “Table#addItem” - it’s all or nothing.
Here’s an example (based on the book of vaadin), that highlights the rows that a firstname that starts with “G” in green, or that have “a” in the firstName in red.
Note the CSS has to cope with even and odd rows, in order
Cheers,
Charles.
/* Create the table with a caption. */
final Table table = new Table("This is my Table");
/* Define the names and data types of columns.
* The "default value" parameter is meaningless here. */
table.addContainerProperty("First Name", String.class, null);
table.addContainerProperty("Last Name", String.class, null);
table.addContainerProperty("Year", Integer.class, null);
/* Add a few items in the table. */
table.addItem(new Object[]{"Nicolaus", "Copernicus", 1473}, 1);
table.addItem(new Object[]{"Tycho", "Brahe", 1546}, 2);
table.addItem(new Object[]{"Giordano", "Bruno", 1548}, 3);
table.addItem(new Object[]{"Galileo", "Galilei", 1564}, 4);
table.addItem(new Object[]{"Johannes", "Kepler", 1571}, 5);
table.addItem(new Object[]{"Isaac", "Newton", 1643}, 6);
table.setCellStyleGenerator(new Table.CellStyleGenerator() {
@Override
public String getStyle(Object itemId, Object propertyId) {
if (propertyId == null) {
// Styling for row
Item item = table.getItem(itemId);
String firstName = (String) item.getItemProperty("First Name").getValue();
if (firstName.toLowerCase().startsWith("g")) {
return "highlight-green";
} else if (firstName.contains("o")) {
return "highlight-red";
} else {
return null;
}
} else {
// styling for column propertyId
return null;
}
}
});
CSS
.v-table-row.v-table-row-highlight-green,
.v-table-row-odd.v-table-row-highlight-green {
background-color: #00ff00;
}
.v-table-row.v-table-row-highlight-red,
.v-table-row-odd.v-table-row-highlight-red {
background-color: #ff0000;
}