Vaadin 流:网格条件背景颜色

Vaadin flow: grid conditional background color

我想根据条件为网格线着色。 我试试这个:

Java:

gridEtudiant.setClassNameGenerator(t -> {
    if (t.getEtud_numero().startsWith("2")) {
        return "error_row";
    }
    return "";
});

Css:

td.error_row {
  background-color: red;
}

HTML

<td id="vaadin-grid-cell-1" tabindex="0" role="gridcell" part="cell body-cell" first-column="" reorder-status="undefined" aria-selected="false" class="error_row" style="width: 100px; flex-grow: 1; order: 10000000;"><slot name="vaadin-grid-cell-content-1"></slot></td>

我们可以看到 'class="error_row"' 但它不是红色的。

Vaadin 版本为 13.0.1

您的 java 代码看起来不错。

确保您有一个 html 文件,例如 webapp/frontend/styles/shared-styles.html,其中包含以下内容:

<dom-module id="my-grid-theme" theme-for="vaadin-grid">
    <template>
        <style>
            [part~="cell"].error_row {
                background: red;
            }
        </style>
    </template>
</dom-module>

如果您的布局包含用 @HtmlImport("frontend://styles/shared-styles.html") 注释的网格(您似乎已经拥有,因为您的自定义 css class 已经应用)它应该可以工作。

示例:

grid.addColumn(Customer::getFirstname).setHeader("Firstname");
grid.addColumn(Customer::getLastname).setHeader("Lastname");
grid.addColumn(Customer::getEmail).setHeader("Email");
grid.setClassNameGenerator(customer -> {
    if (customer.getFirstname().equals("Marco")) {
       return "error_row";
    } else {
       return "";
    }
});

变为: