7

how to calculate cell values of B column and how to change their css style dynamically how to calculate the values of column B and how to change his color

my java object :

public class MyObject{
   private Date date;
   private int A;
   private int C;

   //Getters & Setters
}

my managed bean :

public class MyBean(){
    List<MyObject> List = myObjectDao.FindAll();

    //Getters & Setters
}

my jsf code :

<p:dataTable id="idList" var="list" value="#{myBean.list}" >
    <p:column headerText="DATE">
        <h:outputText value="#{list.date}"  />
    </p:column>
        <p:column headerText="A">
        <h:outputText value="#{list.A}"  />
    </p:column>
        <p:column headerText="B">
        <h:outputText value="????????" style="???????"  //>
    </p:column>
        <p:column headerText="C">
        <h:outputText value="#{list.C} />
    </p:column>
</p:dataTable> 
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Karim Oukara
  • 2,638
  • 8
  • 38
  • 51

3 Answers3

14

You can just use the conditional operator ?: in EL.

E.g.

<c:set var="B" value="#{(list.A / list.C) * 100}" />
<h:outputText value="#{B}" style="display: block; background-color: #{B lt 50 ? 'red' : (B lt 90 ? 'blue' : 'green')}" />

If B is also used elsewhere in the model or controller, then you could add a public int getB() method which just contains return (A/C) * 100; and then use #{list.B} instead of #{B}.

Note that proper design is to use a CSS class instead. E.g.

<h:outputText value="#{B}" styleClass="percentage #{B lt 50 ? 'poor' : (B lt 90 ? 'average' : 'good')}" />

with

td .percentage {
    display: block;
}

.percentage.poor {
    background-color: red;
}

.percentage.average {
    background-color: blue;
}

.percentage.good {
    background-color: green;
}

You can of course also perform the determination of CSS style/class in a getter method as suggested by the other answer, but that's a poor separation of concerns.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thank you for your answer. but the result of is always =0 ???? – Karim Oukara Oct 23 '12 at 09:05
  • 1
    Sorry, remove `scope="request"`. – BalusC Oct 23 '12 at 10:14
  • but i have this warning "Could not make numeric conversion for less than operation", caused by #{B lt 50 ? 'red' : (**B lt 90** ? 'blue' : 'green')}" – Karim Oukara Oct 23 '12 at 10:33
  • 1
    EL syntax validation in the average IDE is poor. I have turned it off in Eclipse. If it was invalid, it would surely have thrown an exception during runtime. – BalusC Oct 23 '12 at 10:34
  • one last question :( how to change the color of the cell not the color of – Karim Oukara Oct 23 '12 at 15:21
  • 1
    That's not possible this way. It's exactly the reason why I've set the output text to `display: block` so that it spans the entire cell. If you have another content which you'd also like to put in same cell with the same background color, then better replace `` by `
    ` on which the style class is set and put everything therein (and now you can remove `display:block` style because the `
    ` is that already by default).
    – BalusC Oct 23 '12 at 15:24
  • I found how to do it. I just change the style of instead it works very well ... thank you friend !!!! – Karim Oukara Oct 23 '12 at 15:32
1

I would recommend doing the logic within your MyObject class

So I would call it in JSF like this:

<p:column headerText="B">
    <h:outputText value="#{list.calculateB()}" styleClass="list.createLabel()">
</p:column>

Inside MyObject

public double calculateB() {
   return (A/C)*100;
}


public String createLabel() {
   if(calculateB() > 90) {
       return "GREEN";
   //DO YOUR LOGIC

}

in CSS

.GREEN {
background-color: #00FF00 !important;

}
gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
-1

Below code has resolved my issue.

style="#{event.eventStatusDesc eq 'Completed' ? 'color: #32cd32;font-weight: bold;':(event.eventStatusDesc eq 'Rescheduled till Further Notice' ? 'color: blue;font-weight: bold;' : 'color: black;')}" />
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116