I need to make calculations of amounts received in the jsf datatable and display in columns sum of these fields.
A) I need sum all items calculated in EL Expression of value1 and show in total1 field.
B) I need sum all items calculated in EL Expression of value2 and show in total2 field.
C) I need sum total1 + total2 and show in field Total.
Obs.: I receive values from DatabaseBean.
<p:dataTable id="table" value="#{valueBean.values}" var="item">
<p:column >
<h:outputText id="value1" value="#{(item[0]*160)/100}" />
<f:facet name="footer">
<h:outputText id="total1" value="#...????" />
</f:facet>
</p:column >
<p:column >
<h:outputText id="value2" value="#{(item[1]*160)/100}" />
<f:facet name="footer">
<h:outputText id="total2" value="#...????" />
</f:facet>
</p:column >
<h:outputText id="total" value="#...????" />
...
</p:dataTable>
Safest Strategy is to employ beans to use to calculate and to hold all the values you need to display on the UI
-You sound like a green programmer so I will give you a leg up. But the code below can still use some improvements if you think carefully.
-I have defined the Item bean as an inner class below but you can always break it out into its own class if you feel comfortable with that.
-Also this strategy allows you to add checks where needed and error handling where needed which i have not included.
-I have also assumed that the initial data comes from a list of DatabaseBean that holds some 2 values in an array.
import java.util.ArrayList;
import java.util.List;
public class BagOfItems {
private static int COMMON_MULTIPLIER = 160;//here document where this comes from
private static int COMMON_DIVISOR = 100; //here document where this comes from
private class Item{
private int value1;
private int value2;
private int calculatedValue1;
private int calculatedValue2;
public Item(int[] values){
value1 = values[0];
value2 = values[1];
calculateValues();
}
public int getValue1() {
return value1;
}
public void setValue1(int value1) {
this.value1 = value1;
}
public int getValue2() {
return value2;
}
public void setValue2(int value2) {
this.value2 = value2;
}
public int getCalculatedValue1() {
return calculatedValue1;
}
public void setCalculatedValue1(int calculatedValue1) {
this.calculatedValue1 = calculatedValue1;
}
public int getCalculatedValue2() {
return calculatedValue2;
}
public void setCalculatedValue2(int calculatedValue2) {
this.calculatedValue2 = calculatedValue2;
}
public void calculateValues(){
calculatedValue1 = (value1 * COMMON_MULTIPLIER)/COMMON_DIVISOR;
calculatedValue2 = (value2 * COMMON_MULTIPLIER)/COMMON_DIVISOR;
}
}
private int totalValues1 = 0;
private int totalValues2 = 0;
private List<Item> items = new ArrayList<Item>();
public BagOfItems(List<DatabaseBean> databaseBeans){
initItems(databaseBeans);
}
private void initItems(List<DatabaseBean> databaseBeans){
for(DatabaseBean databaseBean : databaseBeans){
Item item = new Item(databaseBean.values);
items.add(item);
}
}
protected void sum(){
for(Item item : items){
totalValues1 += item.getCalculatedValue1();
totalValues2 += item.getCalculatedValue2();
}
}
public int getTotalValues1() {
return totalValues1;
}
public void setTotalValues1(int totalValues1) {
this.totalValues1 = totalValues1;
}
public int getTotalValues2() {
return totalValues2;
}
public void setTotalValues2(int totalValues2) {
this.totalValues2 = totalValues2;
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
public int getTotal(){
return (totalValues1 + totalValues2);
}
}
The UI would then look something like this
<p:dataTable id="table" value="#{bagOfItems.items}" var="item">
<p:column >
<h:outputText id="value1" value="#{item.calculatedValue1}" />
<f:facet name="footer">
<h:outputText id="total1" value="#{bagOfItems.totalValues1}" />
</f:facet>
</p:column >
<p:column >
<h:outputText id="value2" value="#{item.calculatedValue2}" />
<f:facet name="footer">
<h:outputText id="total2" value="#{bagOfItems.totalValues2}" />
</f:facet>
</p:column >
<h:outputText id="total" value="#{bagOfItems.total}" />
...
</p:dataTable>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With