首页 > 解决方案 > 如何根据 OpenXava 的引用值定义每一行的颜色/样式?

问题描述

我有一个引用实体优先级的实体问题。我希望在我的 OpenXava 模块的列表模式中,根据优先级的值,为每一行提供不同的颜色和视觉样式。

这是我的问题实体的代码:

package com.yourcompany.tracker.model;

import java.time.*;

import javax.persistence.*;

import org.openxava.annotations.*;
import org.openxava.calculators.*;
import org.openxava.model.*;

import lombok.*;

@Entity @Getter @Setter
public class Issue extends Identifiable {

    @Column(length=100) @Required
    String title;
            
    @Stereotype("SIMPLE_HTML_TEXT") 
    String description;
    
    @ReadOnly 
    @DefaultValueCalculator(CurrentLocalDateCalculator.class) 
    LocalDate createdOn;
    
    @ManyToOne(fetch=FetchType.LAZY, optional=true)
    @DescriptionsList
    Priority priority; 
        
}

这是优先级:

package com.yourcompany.tracker.model;

import javax.persistence.*;
import javax.validation.constraints.*;

import org.openxava.annotations.*;

import lombok.*;

@Entity @Getter @Setter
public class Priority {
    
    @Id @Max(9)
    int level;
    
    @Column(length=40) @Required
    String description;

}

这是我想要达到的效果:

每种颜色都有不同样式的列表

请注意,低优先级的行是灰色的,高优先级的行是粗体的。

我知道有一个@RowStyle 注释,但似乎它是针对简单属性的。如何根据引用 (@ManyToOne) 值为每一行定义不同的样式?

标签: javauser-interfaceopenxava

解决方案


@RowStyle 允许您使用限定属性,即引用的属性。因此,您可以通过以下方式在实体的 @Tab 注释内使用 @RowStyle:

@Entity @Getter @Setter
@Tab(
    rowStyles= {
        @RowStyle(style="high-priority", property="priority.level", value="7"),
        @RowStyle(style="low-priority", property="priority.level", value="3"),
        
    }
)
public class Issue extends Identifiable {

请注意@RowStyle 中的属性属性我们如何使用“priority.level”,因此我们引用了优先级引用的属性。如上所述,当 priority.level 为 7 时,我们应用高优先级样式,当它为 3 时,我们应用低优先级。这些样式是在您的 custom.css 文件中定义的 CSS 类,因此:

.high-priority {
    font-weight: bold;
}

.low-priority {
    color: lightgray !important;
}

要了解更多信息,请阅读有关 @RowStyle 的 OpenXava 参考指南


推荐阅读