7 Replies Latest reply on Sep 18, 2014 2:24 PM by snowhite

    True pagination using AbstractList<E>

    snowhite

      Hi All,

       

      We have a Richfaces 3 application and we are trying to move to Richfaces 4. In our Richfaces3 application we have several pages that implement true pagination using the Java Paged List method described in this blog. http://stackoverflow.com/a/14272190

       

      Now when I try to follow the same approach for rich:dataTable in Richfaces 4, setPage() method is not called when clicked on the datascroller pages. I also tried to use scrollListener for the datascroller but with no luck. None of the methods were called when a page's clicked.

       

      I want to know if I can still use the AbstractList approach to do true pagination in Richfaces 4 or do I need to implement any other model. Any info or pointers regarding this is very much appreciated as we have several pages following this approach for server side pagination.

        • 1. Re: True pagination using AbstractList<E>
          michpetrov

          Hi,

           

          can you share your code? I think the general approach should still work.

          • 2. Re: Re: True pagination using AbstractList<E>
            snowhite

            Thanks for the reply. I attached my code here.

            • 3. Re: True pagination using AbstractList<E>
              michpetrov

              That works for me, providing you have all the methods from LineItemsDataModel implemented (though I've used @ManagedBean instead of the Seam annotation).

              • 4. Re: True pagination using AbstractList<E>
                snowhite

                I tried using @ManagedBean but its not working either. Can you please share your code, thanks.

                • 5. Re: Re: True pagination using AbstractList<E>
                  michpetrov

                  bean

                  @ManagedBean
                  @SessionScoped
                  public class LineItemsDataModel implements Serializable{
                  
                      private static final long serialVersionUID = 1L;
                      int page = 0;
                      private int total;
                      private List<String> lstLineItems;
                      private int itemsPerPage = 6;
                     
                      public int getItemsPerPage() {
                          return itemsPerPage;
                      }
                  
                      public void setItemsPerPage(int itemsPerPage) {
                          this.itemsPerPage = itemsPerPage;
                      }
                  
                      private List<String> list = new ArrayList<String>();
                      private Logger log = Logger.getLogger("lists");
                  
                      public LineItemsDataModel() {
                          list.add("aaa");
                          list.add("aab");
                          list.add("aac");
                          list.add("aad");
                          list.add("aae");
                          list.add("aaf");
                          list.add("aag");
                          list.add("aah");
                          list.add("bba");
                          list.add("bbb");
                          list.add("bbc");
                          list.add("bbd");
                          list.add("bbe");
                          list.add("bbf");
                          list.add("bbg");
                          list.add("bbh");
                          
                          fetch();
                     }
                  
                      public void fetch(){
                          total = 30; //returns  total count of items
                          int limit = itemsPerPage;
                          int startRow = (getPage()-1)*itemsPerPage + 1;
                          if(startRow>total){startRow=1;page=1;};
                              List<String> l = list.subList(startRow, startRow + limit); //getList(startRow,limit);  //returns only single page list
                              setLstLineItems(new PagedList<String>(l,total,itemsPerPage));
                          }
                  
                      public String actionOnPageChange(DataScrollEvent event) {
                          String val = event.getNewScrolVal();
                          log.info(val);
                          return val;
                      }
                  
                      public int getPage() {
                          if(page == 0) return 1;
                          return page;
                      }
                  
                      public void setPage(int page) {
                          this.page = page;
                          fetch();
                      }
                  
                      public int getTotal() {
                          return total;
                      }
                      public void setTotal(int total) {
                          this.total = total;
                      }
                  
                      public List<String> getLstLineItems() {
                          return lstLineItems;
                      }
                  
                      public void setLstLineItems(List<String> lstLineItems) {
                          this.lstLineItems = lstLineItems;
                      }
                  }
                  

                  page:

                   

                  <h:form id="form">
                      <rich:extendedDataTable value="#{lineItemsDataModel.lstLineItems}" var="item" id="table"
                          rows="#{lineItemsDataModel.itemsPerPage}">
                          <rich:column>
                              <f:facet name="header">
                                  <h:outputText value="Ref Number" />
                              </f:facet>
                              <h:outputText value="#{item}" />
                          </rich:column>
                          <rich:column>
                              <f:facet name="header">
                                  <h:outputText value="Order Number" />
                              </f:facet>
                              <h:outputText value="#{item}" />
                          </rich:column>
                          <rich:column>
                              <f:facet name="header">
                                  <h:outputText value="Company Name" />
                              </f:facet>
                              <h:outputText value="#{item}" />
                          </rich:column>
                          <f:facet name="footer">
                              <rich:dataScroller render="table,sumScr1" execute="table"
                                  scrollListener="#{lineItemsDataModel.actionOnPageChange}" id="sumScr1" maxPages="5" for="table"
                                  page="#{lineItemsDataModel.page}">
                                  <f:facet name="first">
                                      <h:outputText value="First" />
                                  </f:facet>
                                  <f:facet name="last">
                                      <h:outputText value="Last" />
                                  </f:facet>
                              </rich:dataScroller>
                          </f:facet>
                      </rich:extendedDataTable>
                  </h:form>
                  
                  • 6. Re: True pagination using AbstractList<E>
                    snowhite

                    Hi Petrov,

                     

                    I apologize for the delay in my response. I really appreciate your help with this. It worked for me. I realized the only difference between yours and mine was that you are calling fetch() method in the constructor itself.

                     

                    Now I have one more issue with it. If I go to any page (say page 2 or 3 etc) constructor is being called and the page value is always set to 1. However actionOnPageChange() method is getting called after that with proper page values set. Is there a way to avoid this? I dont understand why the constructor is being called again that means LineItemsDataModel is again getting created for every page? I tried by setting the Scope to Session (@SessionScroped) as well but no luck. I also tried caching the values but with no luck. Let me know your valuable thoughts on this.

                     

                    Thanks

                    • 7. Re: True pagination using AbstractList<E>
                      snowhite

                      I think I figured out why it was calling the constructor on every page. Even though LineItemsDataModel scrope is set to Session Scope (@SessionScoped) I see in the console it is set to Event Scope. When I used Seam annotation to set the Scope (@Scope (ScopeType.SESSION) or @Scope (ScopeType.PAGE)) it is working fine. Now the constructor is not called on every page click.

                       

                      I appreciate your time in looking into this.

                       

                      Thanks,

                      1 of 1 people found this helpful