Friday, March 16, 2012

Displaying the row count for an af:table

Problem

Say you have an af:table based on a view object.  You want to display the number of rows in the table.  Below I describe a couple ways to do it.  The solutions apply to ADF 11.1.1.3.

Solution 1

Create an af:outputText on the page.  Set the value to an EL expression that returns the estimated row count.  Use partialTriggers to refresh the row count when needed.

    <af:outputText value="  Row count: #{bindings.MyVOIterator.estimatedRowCount}"
                              id="ot4" partialTriggers="cb1 cb2"/>

Thanks to the unknown commenter for suggesting this solution.

Solution 2

1. Create a method in a managed bean to return the number of rows.  E.g.

    public int getLineCount() {
        BindingContainer bc = BindingContext.getCurrent().getCurrentBindingsEntry();
        DCBindingContainer bindings = (DCBindingContainer)bc;
        DCIteratorBinding iter = bindings.findIteratorBinding("MyVOIterator");
        return iter.getRowSetIterator().getRowCount();
    }


2.  Make sure there is a binding for the VO iterator ("MyVOIterator") in the page definition.

3. Create an af:outputText on the page.  Set the value to an EL expression that points to the managed bean method.  Use partialTriggers to refresh the row count when needed.

    <af:outputText value="Row count: #{backingBeanScope.MyBean.lineCount}"
                   id="ot2" partialTriggers="cb1 cb2"/>


Note on getRowCount() vs getEstimatedRowCount()

The ViewObjectImpl class has two methods that return the row count: getRowCount() and getEstimatedRowCount().  See this post for a good explanation of the differences.  Briefly:
  • getRowCount() is more accurate, but less performant.
  • getEstimatedRowCount() is potentially less accurate, but more performant. 

Thanks to Richard Ver Steeg for his insights on this topic.

    8 comments:

    1. What about count() field in MyVO ? Best perfomant :). Anyway nice post.

      ReplyDelete
      Replies
      1. I'm not familiar with the count() field. Can you get to it within a managed bean?

        Thanks for the comment.

        Delete
      2. create VO as :
        select h.*,count(*) from employee h

        Delete
    2. You can make it with EL (at least estimatedRowCount) with no need for Java.

      ReplyDelete
      Replies
      1. Good point. I've updated the post with your solution. Thanks for contributing!

        Delete
    3. how can I use same to calculate total of sum numeric field?

      ReplyDelete