简体   繁体   English

rich:dataTable从filterBy列获取总计

[英]rich:dataTable getting the total from filterBy columns

I have a rich datatable with columns that I can filter. 我有一个丰富的数据表,其中包含可以过滤的列。

<!-- 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>

For the total, I can sum all the amounts in sessionScope.paymentsBean.payments to get the total. 对于总数,我可以将sessionScope.paymentsBean.payments中的所有金额相加得出总数。 Which is $355.00 in the following example. 在以下示例中为$ 355.00。

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

Total: $355.00

However, if I filter by "Visa", the table and total would look like: 但是,如果我按“签证”进行过滤,则表和总数将如下所示:

Invoice     Description     Amount
2           Visa            $50.00
3           Visa            $100.00

Total: $355.00

The total should be $150 but getTotal() doesn't know about the filterBy data. 总数应为$ 150,但getTotal()不知道filterBy数据。 Is there a way to dynamically calculate the total based on the filterBy criteria from the rich:datatable? 有没有一种方法可以根据rich:datatable中的filterBy标准动态计算总数?

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 . 您需要将该表绑定到支持bean属性,并且需要使其成为rich:extendedDataTable

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

Binded attribute will be HTMLExtendedDataTable type. Binded属性将为HTMLExtendedDataTable类型。 After that you will need to create an Ajax call when user types something in the filter, to be called. 之后,当用户在过滤器中键入要调用的内容时,您将需要创建一个Ajax调用。 See <a4j:support /> component and use it when a key is pressed, as: 请参阅<a4j:support />组件,并在按下键时使用它,如下所示:

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

Remember calling Ajax method on filterings can be tricky . 记住在过滤上调用Ajax方法可能很棘手 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. 我真诚地建议避免使用表的内置过滤器,并基于h:inputText创建自定义过滤器,并在此处实现ajax调用。 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. 调用该方法后,您只需要获取过滤后的行,计算总量并将其放入一个变量中,当调用完成时,JSF将在该变量上更新该框。 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. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM