0 Replies Latest reply on Aug 16, 2007 4:45 PM by 3l30n0r

    Rich dataScroller and dataTable

    3l30n0r

      Hello guys, i have a problem with dataTable's pagination. I want to create searchable ajax data table. The problem is consist of that, the number of pages that are displayed don't match the result that is returned from database. ( I have debugged it and i can find where is the problem. ).

      That is the scenario:
      By default there are 8 records in the database and when pages is displayed use can see first 5 items and 2 pages.
      If user performs some search ( '12' in my case ) returned result is displayed but number of pages is the same as before search.
      At latter time if user deletes text in the search field and hits search button page displays only 5 items without 2 page buttons.

      Any idea where is the problem ?
      This is the code that i have written.

      
      
      
      public class AgreementViewManager extends AbstractFacesBean {
       private Logger log = Logger.getLogger(AgreementViewManager.class);
       private static final int PAGE_SIZE = 5;
       private int pageNumber = 1;
      
       private HtmlDataTable dataTable = null;
       private DataModel model = null;
      
       private BigInteger count = null;
       private String searchIngoingNumber = null;
      
       private AgreementService agreementService = null;
      
       /**
       * Creates a new instance of AgreementViewManager
       */
       public AgreementViewManager() {
       getPagedModel(1);
       }
      
       /**
       * Performes searching for ingoing number.
       */
       public String searchByNumber() {
       log.debug("searchIngoingNumber: " + searchIngoingNumber);
       // perform search over agreements.
       AgreementService service = getAgreementService();
      
       // check wheather ingoingNumber has been set ..
       if (null == searchIngoingNumber || "".equals(searchIngoingNumber)) {
       getPagedModel(1);
       return "";
       }
       SearchResult result = service.getAgreements(1, PAGE_SIZE, searchIngoingNumber);
      
       List agreements = result.getResult();
       BigInteger count = result.getCount();
      
       this.count = count;
       this.model = new PagedListDataModel(agreements, count.intValue(), PAGE_SIZE);
      
       return "";
       }
      
       /**
       * Gets Agreements model that should be displayed on the current page.
       *
       * @return model that have to be displayed in the page.
       */
       public DataModel getAllAgreements() {
       DataModel model = getModel();
       return model;
       }
      
       /**
       * Retrives agreements model which corresponds to the given page.
       *
       * @param pageNumber the page for which data model is required
       * @return DataModel with agreements.
       */
       private DataModel getPagedModel(final int pageNumber) {
       AgreementService service = getAgreementService();
       count = service.getAggreementsCount();
      
       SearchResult result = service.getAgreements(pageNumber, PAGE_SIZE, null);
       List agreements = result.getResult();
      
       this.model = new PagedListDataModel(agreements, count.intValue(), PAGE_SIZE);
       return model;
       }
      
       /**
       * Listens for user request. This event is invoked when user clicks on some page number on data scrolle. When
       * user clicks on a page this method is invoked and it checks whether which operation is ivoked
       * ( next page, specified page, prev page and etc). After operation is determined the model is updated with
       * new value that is stored in the given page.
       *
       * @param e
       * @return
       */
       public String listen(DataScrollerEvent e) {
      
       pageNumber = Integer.parseInt(e.getOldScrolVal()) + 1;
      
       if ("next".equals(e.getNewScrolVal())) {
       pageNumber += 1;
       } else if ("previous".equals(e.getNewScrolVal())) {
       pageNumber -= 1;
       } else if ("fastrewind".equals(e.getNewScrolVal())) {
       pageNumber -= 1;
       } else if ("fastforward".equals(e.getNewScrolVal())) {
       pageNumber += 1;
       } else if ("last".equals(e.getNewScrolVal())) {
       pageNumber = count.intValue() / (PAGE_SIZE) + 1;
       } else if ("first".equals(e.getNewScrolVal())) {
       pageNumber = 1;
       } else {
       pageNumber = Integer.parseInt(e.getNewScrolVal());
       }
       if (log.isDebugEnabled()) {
       log.debug("Page Number:" + pageNumber);
       }
       this.model = getPagedModel(pageNumber);
      
       return "";
       }
      
       protected AgreementService getAgreementService() {
       if (agreementService == null) {
       agreementService = new AgreementService();
       }
       return agreementService;
       }
      
       public HtmlDataTable getDataTable() {
       return dataTable;
       }
      
       public void setDataTable(HtmlDataTable dataTable) {
       this.dataTable = dataTable;
       }
      
       public DataModel getModel() {
       return model;
       }
      
       public void setModel(DataModel model) {
       this.model = model;
       }
      
       public int getPageNumber() {
       return pageNumber;
       }
      
       public void setPageNumber(int pageNumber) {
       this.pageNumber = pageNumber;
       }
      
       public String getSearchIngoingNumber() {
       return searchIngoingNumber;
       }
      
       public void setSearchIngoingNumber(String searchIngoingNumber) {
       this.searchIngoingNumber = searchIngoingNumber;
       }
      }
      
      


      Pagination Model
      public class PagedListDataModel extends DataModel {
       private int rowIndex = -1;
       private int totalNumRows;
       private int pageSize;
       private List list;
       /** Creates a new instance of PagedDataModel */
       public PagedListDataModel() {
       super();
       }
      
       public PagedListDataModel(List list, int totalNumRows, int pageSize) {
       super();
       setWrappedData(list);
       this.totalNumRows = totalNumRows;
       this.pageSize = pageSize;
       }
      
       public boolean isRowAvailable() {
       if (list == null)
       return false;
       int rowIndex = getRowIndex();
       if (rowIndex >= 0 && rowIndex < list.size())
       return true;
       else
       return false;
       }
      
       public int getRowCount() {
       return totalNumRows;
       }
      
       public Object getRowData() {
       if (list == null)
       return null;
       else if (!isRowAvailable())
       throw new IllegalArgumentException();
       else {
       int dataIndex = getRowIndex();
       return list.get(dataIndex);
       }
       }
      
       public int getRowIndex() {
       return (rowIndex % pageSize);
       }
      
       public void setRowIndex(int rowIndex) {
       this.rowIndex = rowIndex;
       }
      
       public Object getWrappedData() {
       return list;
       }
      
       public void setWrappedData(Object list) {
       this.list = (List) list;
       }
      }
      

      And this is on the page
      <html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:h="http://java.sun.com/jsf/html"
       xmlns:t="http://myfaces.apache.org/tomahawk"
       xmlns:a4j="https://ajax4jsf.dev.java.net/ajax"
       xmlns:rich="http://richfaces.ajax4jsf.org/rich">
      
      
      <ui:composition template="/templates/agreements.jspx">
      
       <ui:define name="agreements">
       <a4j:outputPanel id="errorsPanel">
       <h:messages/>
       </a4j:outputPanel>
       <h1>????????</h1>
       <br/>
       <h:form id="agreementsForm">
       <a4j:outputPanel id="dataPanel">
      
       <table>
       <tr>
       <td colspan="2">
       <span>??????? N:</span>
       </td>
       <td>
       <h:inputText id="searchIngoingNumber" value="#{agreementView.searchIngoingNumber}">
      
       </h:inputText>
       </td>
       <td>
       <a4j:commandLink id="searchButtonId" style="defaultLinkStyle"
       action="#{agreementView.searchByNumber}" reRender="agreementList,agreementScroller">
       <h:graphicImage value="/images/find.gif"/>
       </a4j:commandLink>
       </td>
       </tr>
       <tr>
       <td colspan="3">
       <rich:datascroller id="agreementScroller" for="agreementList" maxPages="20"
       scrollerListener="#{agreementView.listen}"
       stepControls="false" fastControls="hide"/>
       <rich:dataTable width="483" id="agreementList" rows="5" columnClasses="col"
       value="#{agreementView.allAgreements}" var="agr">
      
       <f:facet name="header">
       <rich:columnGroup>
       <rich:column styleClass="headerText" colspan="6">
       <h:outputText value="????????"/>
       </rich:column>
       <rich:column breakBefore="true">
       <h:outputText styleClass="headerText" value="???"/>
       </rich:column>
       <rich:column>
       <h:outputText styleClass="headerText" value="?????"/>
       </rich:column>
       <rich:column>
       <h:outputText styleClass="headerText" value="??????"/>
       </rich:column>
       <rich:column>
       <h:outputText styleClass="headerText" value="????"/>
       </rich:column>
       <rich:column>
       <h:outputText styleClass="headerText" value="????????"/>
       </rich:column>
       <rich:column>
       <h:outputText styleClass="headerText" value="?????"/>
       </rich:column>
       </rich:columnGroup>
       </f:facet>
      
       <h:column>
       <h:outputText rendered="#{agr.agreementtype == 1}" value="???"/>
       <h:outputText rendered="#{agr.agreementtype == 2}" value="??????? ?? ???????????"/>
       <h:outputText rendered="#{agr.agreementtype == 3}" value="????????"/>
       </h:column>
       <h:column>
       <h:outputText value="#{agr.ingoingNumber}"/>
       </h:column>
       <h:column>
       <h:outputText rendered="#{agr.client.clientType == 1}" value="????????????"/>
       <h:outputText rendered="#{agr.client.clientType == 0}" value="??????"/>
       </h:column>
       <h:column>
       <h:outputText value="#{agr.client.clientType}"/>
       </h:column>
       <h:column>
       <a4j:commandLink style="defaultLinkStyle" immediate="true"
       action="#{agreementView.editAgreement}">
       <h:graphicImage value="/images/edit.gif"/>
       </a4j:commandLink>
       </h:column>
       <h:column>
       <a4j:commandLink style="defaultLinkStyle" immediate="true" action="#{agreementView.printAgreement}">
       <h:graphicImage value="/images/print.gif"/>
       </a4j:commandLink>
       </h:column>
       </rich:dataTable>
       </td>
       </tr>
       </table>
       </a4j:outputPanel>
       </h:form>
       </ui:define>
      </ui:composition>
      </html>