我在JSF中有一个数据表,在页脚中有一个发票总额。我使用一种方法来计算页面加载的总和,并且工作正常。数据表中的每一行都有一个复选框。当用户选中或取消选中复选框时,如果未选中该复选框,则页面应通过减去该行的成本来重新计算总计,或者如果选中该成本则将成本添加到总计中。我已经尝试添加另一个outputtext来保存新的total并在同一个outputtext中更改值。问题是新的总数没有被渲染到outputtext。
我有一个静态变量来保持总数变化。我也试过用一种方法。
private static BigDecimal saveInvoiceTotal;
bean中的InvoiceTotal方法
public BigDecimal invoiceTotal() throws Exception {
List<OrderDetail> selected = getDetails();
BigDecimal invoiceTotal = new BigDecimal(0);
for(OrderDetail d : selected) {
if(d.isSelected())
invoiceTotal = invoiceTotal.add(d.getExtCost());
else
invoiceTotal = invoiceTotal.subtract(d.getExtCost());
}
saveInvoiceTotal = invoiceTotal;
return invoiceTotal;
}
此方法用于将新总计返回到页面。
public BigDecimal invoiceSelectedTotal() throws Exception {
return saveInvoiceTotal;
}
此方法根据复选框是选中还是取消选中来增加或减去成本
public void selectedRow(OrderDetail d) throws Exception {
if(d.isSelected())
saveInvoiceTotal = saveInvoiceTotal.add(d.getExtCost());
else
saveInvoiceTotal = saveInvoiceTotal.subtract(d.getExtCost());
}
数据
on page load <rich:dataTable id="detailTable" var="_detail" value="#{orderDetails.details}" rendered="#{not empty orderDetails.details}" style="width : 100%">
<rich:column id="detail_itemnumber">
<f:facet name="header">Item Number</f:facet>
#{_detail.itemNumber}
<f:facet name="footer">
<h:outputText value="Invoiced Total: " />
<h:outputText id="invTotal" value="#{orderDetails.invoiceTotal()}" rendered="#{_detail.invoiceRender}"/>
<h:outputText id="invSelectedTotal" value="#{orderDetails.invoiceSelectedTotal()}" rendered="#{not _detail.invoiceRender}"/>
</f:facet>
</rich:column>
<rich:column id="detail_extcost">
<f:facet name="header">Ext Cost</f:facet>
#{_detail.extCost}
<f:facet name="footer">
<h:outputText value="Order Total: " />
<h:outputText value="#{orderDetails.orderTotal()}" />
</f:facet>
</rich:column>
<rich:column id="detail_selected">
<f:facet name="header">Select</f:facet>
<h:selectBooleanCheckbox value="#{_detail.selected}" >
<f:param name="orderNo" value="#{order.id}"></f:param>
<a4j:ajax listener="#{orderDetailController.selectedRow(_detail)}" />
</h:selectBooleanCheckbox>
</rich:column>
</rich:dataTable>
答案 0 :(得分:0)
每次用户检查/取消确认
时,您都可以尝试使用Ajax并调用该方法答案 1 :(得分:0)
The solution was to use <f:ajax render="invTotal"/>
<rich:panel header="Order Detail">
<h:form>
<rich:dataTable value="#{data.details}" var="abc">
<rich:column id="detail_itemnumber">
<f:facet name="header">
<h:outputText value="Item Number"/>
</f:facet>
<h:outputText value="#{abc.itemNumber}"/>
<f:facet name="footer">
<h:outputText value="Invoiced Total: " />
<h:outputText id="invTotal" value="#{data.invoiceTotal()}" />
<!-- <h:outputText id="invSelectedTotal" value="#{abc.invoiceAmount}" rendered="#{not abc.invoiceRender}"/> -->
</f:facet>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="Item Description"/>
</f:facet>
<h:outputText value="#{abc.itemDesc}" />
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="Order Qty"/>
</f:facet>
<h:outputText value="#{abc.orderQty}" />
</rich:column>
<rich:column id="detail_invoiceqty" style="width : 10%">
<f:facet name="header">
<h:outputText value="Invoice Qty"/>
</f:facet>
<h:inputText value="#{abc.invoiceQty}" >
<f:ajax render="invTotal" />
<a4j:ajax listener="#{data.calculateExtCost()}" render="extCost"/>
</h:inputText>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="Item Cost"/>
</f:facet>
<h:outputText value="#{abc.itemCost}" />
</rich:column>
<rich:column id="detail_extcost">
<f:facet name="header">
<h:outputText value="Ext Cost"/>
</f:facet>
<h:outputText id="extCost" value="#{abc.extCost}" />
<f:facet name="footer">
<h:outputText value="Order Total: " />
<h:outputText value="#{data.orderTotal()}" />
</f:facet>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="Selected"/>
</f:facet>
<h:selectBooleanCheckbox value="#{abc.selected}" >
<!-- <a4j:ajax listener="#{data.invoiceTotal()}" /> -->
<f:ajax render="invTotal"/>
</h:selectBooleanCheckbox>
</rich:column>
</rich:dataTable>
public BigDecimal invoiceTotal() throws Exception {
List<OrderDetail> selected = getDetails();
BigDecimal newInvoiceTotal = new BigDecimal(0);
for(OrderDetail d : selected) {
if(d.isSelected())
newInvoiceTotal = newInvoiceTotal.add(d.getExtCost());
}
return newInvoiceTotal;
}