andriy
andriy

Reputation: 4164

Primefaces datatable: set row color from bean

This question might appear as duplicate at the first time, but I didn't find someone else with the same problem.

We want to set specific row color in datatable. We found that there is only one "pretty" way to do it: using rowStyleClass attribute, which will apply styles from classes defined in CSS file.

But in our case, colors are obtained from the external database view and can be changed any time. It means that we can't define fixed styles in CSS file.

Unfortunately, there is no rowStyle attribute. Any suggestions of how we can set row colors based on generated by bean hexadecimal value?

(we are using PF 5.3)

Upvotes: 0

Views: 2751

Answers (1)

Zaw Than oo
Zaw Than oo

Reputation: 9935

You cannot use rowStyle. But, you can change css of thestyleClass programmatically. Get css style from your backing bean.

Don't put the following style in css file. Just put it into your jsf page which show the datatable.

<style>
    .oddRowStyle {
        #{MyBean.oddRowStyle}
    }
    .evenRowStyle {
        #{MyBean.evenRowStyle}
    }
</style>
<p:dataTable .....  rowIndexVar="index" 
    rowStyleClass="#{(index mod 2) eq 0 ? 'evenRowStyle' : 'oddRowStyle'}"/>

MyBean.java

public String getOddRowStyle() {
    /*Change Your style as programmatically*/
    return "background-color: #F7F7F7!important ;";
}

public String getEvenRowStyle() {
    /*Change Your style as programmatically*/
    return "background-color: #05855F!important ;";
}

Upvotes: 2

Related Questions