[英]JSF Expression Language Operations and sum values fields
我需要计算jsf数据表中收到的金额,并在这些字段的总和中显示。
A)我需要对在value1的EL表达式中计算的所有项目求和,并在total1字段中显示。
B)我需要对在value2的EL表达式中计算的所有项目求和,并在total2字段中显示。
C)我需要总和total1 + total2并显示在字段Total中。
观察:我从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>
最安全的策略是使用bean来计算和保存您需要在UI上显示的所有值
-你听起来像个绿色的程序员,所以我会帮你的忙。 但是,如果您仔细考虑,下面的代码仍然可以使用一些改进。
-我已经在下面将Item bean定义为内部类,但是如果您愿意的话,可以随时将其分解为自己的类。
-此外,该策略还允许您在需要的地方添加检查,并在需要的地方添加错误处理(我没有包括在内)。
-我还假设初始数据来自一个DatabaseBean列表,该列表在一个数组中包含一些2个值。
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);
}
}
用户界面将如下所示
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.