1

我有一个丰富的数据表,其中包含可以过滤的列。

<!-- Data table with filters -->
<h:form>
<rich:dataTable value="#{sessionScope.paymentsBean.payments}" var="payment"
   id="table">
   <rich:column filterBy="#{sessionScope.payment.invoice}" filterEvent="onkeyup">
      <f:facet name="header">Invoice</f:facet>
      <h:outputText value="#{sessionScope.payment.invoice}" />
   </rich:column>
   <rich:column filterBy="#{sessionScope.payment.description}" filterEvent="onkeyup">
      <f:facet name="header">Description</f:facet>
      <h:outputText value="#{sessionScope.payment.description}" />
   </rich:column>
   <rich:column filterBy="#{sessionScope.payment.amount}" filterEvent="onkeyup">
      <f:facet name="header">Amount</f:facet>
      <h:outputText value="#{sessionScope.payment.amount}" />
    </rich:column>
</rich:dataTable>
</h:form>

<!-- Total -->
<h:outputText id="total" value="#{sessionScope.paymentsBean.total}" >
    <f:convertNumber maxFractionDigits="2" type="currency" currencySymbol="$"/>
</h:outputText>

对于总数,我可以将 sessionScope.paymentsBean.payments 中的所有金额相加得到总数。在以下示例中为 355.00 美元。

Invoice     Description     Amount
1           Cash            $5.00
2           Visa            $50.00
3           Visa            $100.00
4           MasterCard      $200.00 

Total: $355.00

但是,如果我按“签证”过滤,表格和总数将如下所示:

Invoice     Description     Amount
2           Visa            $50.00
3           Visa            $100.00

Total: $355.00

总额应该是 150 美元,但 getTotal() 不知道 filterBy 数据。有没有办法根据rich:datatable中的filterBy标准动态计算总数?

4

1 回答 1

0

There's a way to recover the filtered data. You need the table to be binded to a backing bean property and you need it to be a rich:extendedDataTable.

<rich:extendedDataTable value="#{sessionScope.paymentsBean.payments}" var="payment" id="table" binding="sessionScope.paymentsBean.table">

Binded attribute will be HTMLExtendedDataTable type. After that you will need to create an Ajax call when user types something in the filter, to be called. See <a4j:support /> component and use it when a key is pressed, as:

<a4j:support event="onkeyup" reRender="countBox" action="#{sessionScope.paymentsBean.actionCalculateTotalValue}"/>

Remember calling Ajax method on filterings can be tricky. I sincerely suggest to avoid the table's built-in filters and create your custom ones based in h:inputText and implement your ajax calls here. You can take a look into this link.

Once the method is called you just need to obtain the filtered rows, calculate the total amount and put in in a variable, over which JSF will update the box when the call is finished. What's the way to get the filtered rows? There you have:

public List<E> filteredList() {
    List<E> list = new java.util.ArrayList<E>();

    int i = 0;
    boolean loop = true;

    while (loop) {
        table.setRowKey(new Integer(i));
        if (table.isRowAvailable()) {
            list.add((E) table.getRowData());
            i += 1;
        } else {
            loop = false;
        }
    }

    table.setRowKey(null);
    return list;
}

Basically it makes a loop until there are no more available rows in the table. Good luck.

于 2013-01-11T07:09:19.650 回答