How to apply a @RowStyle to a range of rows (or other complex condition) in OpenXava?

105 Views Asked by At

I understand that using @RowStyle I can determine the style depending on specific values of a field. But how can I do it if I want to differentiate the five first records (id >= 1 and id <= 5) from the other ones?

For example, in my case an Activity entity starts with 5 activites by default. The goal is to show those first 5 rows with a different color or style and the rest of the records, added by the user, with regular style. Like this:

List with first 5 rows remarked

How can I apply the style just to the first 5 rows?

1

There are 1 best solutions below

0
javierpaniza On

@RowStyle does not allow you to define complex logic, but just compares the value of a property. However, you can achieve it defining a calculated property where you can put that logic. Something like this:

import javax.persistence.*;
import org.openxava.annotations.*;
import lombok.*;

@Entity @Getter @Setter
@Tab(
    rowStyles={
        @RowStyle(style="row-highlight", property="type", value="default"),
        @RowStyle(style="row-red", property="type", value="extra")
    }
)
public class Activity {
    
    @Id
    int id;
    
    @Column(length=50) @Required
    String description;
    
    public enum Type { DEFAULT, NORMAL, EXTRA };
    public Type getType() {
        if (id >= 1 && id <= 5) return Type.DEFAULT;
        if (id > 100) return Type.EXTRA;
        return Type.NORMAL;
    }
    
}

Note the type property where we put the logic that determines the style.