Friday, February 22, 2013

Restrict rows in a table (ADF Table Pagination)

So the user will select a department from the navigation list, and the employees will be displayed in group of N. User can click on the buttons below to traverse through the table.

You need to be aware of some of the properties of a table.


  1. AutoHeightRows: By defining this attribute, the table height is adjusted according to the content. The fetchSize and autoHeightRows attributes together control the number of rows displayed in the table. Please note that the value of the autoHeightRows attribute cannot exceed the value set for the fetchSize attribute.
  2. RangeSize: It controls the number of transactions that are required to populate the table as the user scrolls up and down.


This is the code for the af:table:

autoheightrows="5" contentdelivery="immediate" emptytext="#{bindings.EmployeesView3.viewable ? 'No data to display.' : 'Access Denied.'}" fetchsize="#{bindings.EmployeesView3.rangeSize}" id="t1" partialtriggers="::cb1 ::cb2 ::cb3 ::cb4" rowbandinginterval="0" rows="#{bindings.EmployeesView3.rangeSize}" rowselection="single" selectedrowkeys="#{bindings.EmployeesView3.collectionModel.selectedRow}" selectionlistener="#{PagingBean.rowSelected}" value="#{bindings.EmployeesView3.rangeSet}" var="row" width="100%"

This is the code in the bean:
package view;

import oracle.adf.model.BindingContext;
import oracle.adf.share.logging.ADFLogger;

import oracle.binding.BindingContainer;

import oracle.jbo.uicli.binding.JUCtrlRangeBinding;

import org.apache.myfaces.trinidad.event.SelectionEvent;

public class PagingBean {

    private int pageNumber;
    private static final int RANGE_SIZE = 5;
    private static ADFLogger _logger = ADFLogger.createADFLogger(PagingBean.class);

    /**
     * Invoked when user select a row in the table.
     * @param se
     */
    public void rowSelected(SelectionEvent se) {
        JUCtrlRangeBinding staffView = getEmployeeView();
        staffView.getIteratorBinding().setCurrentRowIndexInRange((Integer)se.getAddedSet().toArray()[0]);
    }

    /**
     * Invoked when user click the Go button to go to the target page
     * @return
     */
    public String goToPage() {
        JUCtrlRangeBinding staffView = getEmployeeView();
        int prePage = staffView.getIteratorBinding().getNavigatableRowIterator().getRangeStart() / RANGE_SIZE + 1;
        _logger.info("prePage: " + prePage);
        staffView.getIteratorBinding().getNavigatableRowIterator().scrollRange(RANGE_SIZE *
                                                                               (this.pageNumber - prePage));
        return null;
    }

    /**
     * Invoked when user click to go to the last page
     * @return
     */
    public String goLastPage() {
        JUCtrlRangeBinding staffView = this.getEmployeeView();
        long lastPageNum = Math.max(1, (staffView.getIteratorBinding().getEstimatedRowCount() - 1) / RANGE_SIZE + 1);
        _logger.info("lastPageNum: " + lastPageNum);
        if (lastPageNum <= 1)
            return null;
        int prePage = staffView.getIteratorBinding().getNavigatableRowIterator().getRangeStart() / RANGE_SIZE + 1;
        _logger.info("prePage: " + prePage);
        staffView.getIteratorBinding().getNavigatableRowIterator().scrollRange((int)(RANGE_SIZE *
                                                                                     (lastPageNum - prePage)));
        return null;
    }

    /**
     * Is go to last button enabled
     * @return
     */
    public boolean isLastEnabled() {
        JUCtrlRangeBinding staffView = this.getEmployeeView();
        long lastPageNum = Math.max(1, (staffView.getIteratorBinding().getEstimatedRowCount() - 1) / RANGE_SIZE + 1);
        _logger.info("lastPageNum: " + lastPageNum);
        long currentPage = staffView.getIteratorBinding().getNavigatableRowIterator().getRangeStart() / RANGE_SIZE + 1;
        _logger.info("currentPage: " + currentPage);
        _logger.info("Boolean" + (currentPage < lastPageNum));
        return currentPage < lastPageNum;
    }

    public void setPageNumber(int pageNumber) {
        _logger.info("pageNumber: " + pageNumber);
        this.pageNumber = pageNumber;
    }

    public int getPageNumber() {
        int i = getEmployeeView().getIteratorBinding().getNavigatableRowIterator().getRangeStart() / RANGE_SIZE + 1;
        _logger.info("i: " + i);
        return pageNumber =
                getEmployeeView().getIteratorBinding().getNavigatableRowIterator().getRangeStart() / RANGE_SIZE + 1;
    }

    private JUCtrlRangeBinding getEmployeeView() {
        BindingContext bindingCtx = BindingContext.getCurrent();
        BindingContainer bindings = bindingCtx.getCurrentBindingsEntry();
        return (JUCtrlRangeBinding)bindings.getControlBinding("EmployeesView3");
    }
}

Download the sample application from File Cabinet: TablePagination.rar

No comments:

Post a Comment