隨筆-348  評(píng)論-598  文章-0  trackbacks-0
          頁(yè)面:
          <f:view>
                  
          <h:form id="formlist">    
                      
          <rich:dataTable id="carList" width="483" columnClasses="col" rows="10"
                          value
          ="#{testBean.dataModel}" var="car">            
                          
          <f:facet name="header">
                              
          <rich:columnGroup>
                                  
          <h:column>
                                      
          <h:outputText styleClass="headerText" value="Name" />
                                  
          </h:column>
                                  
          <h:column>
                                      
          <h:outputText styleClass="headerText" value="Decription" />
                                  
          </h:column>
                                  
          <h:column>
                                      
          <h:outputText styleClass="headerText" value="Base Price" />
                                  
          </h:column>
                                  
          <h:column>
                                      
          <h:outputText styleClass="headerText" value="Time" />
                                  
          </h:column>
                                  
          <h:column>
                                      
          <h:outputText styleClass="headerText" value="操作" />
                                  
          </h:column>                        
                              
          </rich:columnGroup>
                          
          </f:facet>
              
                          
          <h:column>
                              
          <h:outputText value="#{car.name}" />
                          
          </h:column>
                          
          <h:column>
                              
          <h:outputText value="#{car.description}" />
                          
          </h:column>
                          
          <h:column>
                              
          <h:outputText value="#{car.baseprice}" />
                          
          </h:column>
                          
          <h:column>
                              
          <h:outputText value="#{car.timestamp}" />
                          
          </h:column>
                          
          <h:column>
                              
          <h:commandLink action="#{user.delete}" value="刪除" >
                                  
          <f:param name="id" value="#{car.id}"/>
                              
          </h:commandLink>
                          
          </h:column>                
                      
          </rich:dataTable>
                      
          <rich:datascroller for="carList" id="dc1"
                      style
          ="width:483px" page="#{user.scrollerPage}"/>                        
                  
          </h:form>
              
          </f:view>
          DataPage.java:
          package com.jsecode.util.pagination;

          import java.util.List;

          public class DataPage
          {

              
          /**
               * 將需要的頁(yè)的數(shù)據(jù)封裝到一個(gè)DataPage中去, 這個(gè)類表示了我們需要的一頁(yè)的數(shù)據(jù),<br>
               * 里面包含有三個(gè)元素:datasetSize,startRow,和一個(gè)用于表示具體數(shù)據(jù)的List。<br>
               * datasetSize表示了這個(gè)記錄集的總條數(shù),查詢數(shù)據(jù)的時(shí)候,使用同樣的條件取count即可,<br>
               * startRow表示該頁(yè)的起始行在數(shù)據(jù)庫(kù)中所有記錄集中的位置
               
          */


              
          private int datasetSize;

              
          private int startRow;

              
          private List data;

              
          /**
               * 
               * 
          @param datasetSize
               *            數(shù)據(jù)集大小
               * 
          @param startRow
               *            起始行
               * 
          @param data
               *            數(shù)據(jù)list
               
          */

              
          public DataPage(int datasetSize, int startRow, List data)
              
          {

                  
          this.datasetSize = datasetSize;

                  
          this.startRow = startRow;

                  
          this.data = data;

              }


              
          /**
               * 
               * 
          @return
               
          */

              
          public int getDatasetSize()
              
          {

                  
          return datasetSize;

              }


              
          public int getStartRow()
              
          {

                  
          return startRow;

              }


              
          /**
               * 
               * 
          @return 已填充好的數(shù)據(jù)集
               
          */

              
          public List getData()
              
          {

                  
          return data;

              }


          }

          PagedListDataModel.java:
          package com.jsecode.util.pagination;

          import javax.faces.model.DataModel;

          public abstract class PagedListDataModel extends DataModel {
              
          int pageSize;
              
          int rowIndex;
              DataPage page;

              
          /**
               * Create a datamodel that pages through the data showing the specified
               * number of rows on each page.
               
          */

              
          public PagedListDataModel(int pageSize) {
                  
          super();
                  
          this.pageSize = pageSize;
                  
          this.rowIndex = -1;
                  
          this.page = null;
              }


              
          /**
               * Not used in this class; data is fetched via a callback to the fetchData
               * method rather than by explicitly assigning a list.
               
          */

              
          public void setWrappedData(Object o) {
                  
          if (o instanceof DataPage) {
                      
          this.page = (DataPage) o;
                  }
           else {
                      
          throw new UnsupportedOperationException(" setWrappedData ");
                  }

              }


              
          public int getRowIndex() {
                  
          return rowIndex;
              }


              
          /**
               * Specify what the "current row" within the dataset is. Note that the
               * UIData component will repeatedly call this method followed by getRowData
               * to obtain the objects to render in the table.
               
          */

              
          public void setRowIndex(int index) {
                  rowIndex 
          = index;
              }


              
          /**
               * Return the total number of rows of data available (not just the number of
               * rows in the current page!).
               
          */

              
          public int getRowCount() {
                  
          return getPage().getDatasetSize();
              }


              
          /**
               * Return a DataPage object; if one is not currently available then fetch
               * one. Note that this doesn't ensure that the datapage returned includes
               * the current rowIndex row; see getRowData.
               
          */

              
          private DataPage getPage() {
                  
          if (page != null{
                      
          return page;
                  }

                  
          int rowIndex = getRowIndex();
                  
          int startRow = rowIndex;
                  
          if (rowIndex == -1{
                      
          // even when no row is selected, we still need a page
                      
          // object so that we know the amount of data available.
                      startRow = 0;
                  }
           // invoke method on enclosing class
                  page = fetchPage(startRow, pageSize);
                  
          return page;
              }


              
          /**
               * Return the object corresponding to the current rowIndex. If the DataPage
               * object currently cached doesn't include that index then fetchPage is
               * called to retrieve the appropriate page.
               
          */

              
          public Object getRowData() {
                  
          if (rowIndex < 0{
                      
          throw new IllegalArgumentException(
                              
          " Invalid rowIndex for PagedListDataModel; not within page ");
                  }
           // ensure page exists; if rowIndex is beyond dataset size, then
                  
          // we should still get back a DataPage object with the dataset size
                  
          // in it
                  if (page == null{
                      page 
          = fetchPage(rowIndex, pageSize);
                  }

                  
          int datasetSize = page.getDatasetSize();
                  
          int startRow = page.getStartRow();
                  
          int nRows = page.getData().size();
                  
          int endRow = startRow + nRows;
                  
          if (rowIndex >= datasetSize) {
                      
          throw new IllegalArgumentException(" Invalid rowIndex ");
                  }

                  
          if (rowIndex < startRow) {
                      page 
          = fetchPage(rowIndex, pageSize);
                      startRow 
          = page.getStartRow();
                  }
           else if (rowIndex >= endRow) {
                      page 
          = fetchPage(rowIndex, pageSize);
                      startRow 
          = page.getStartRow();
                  }

                  
          return page.getData().get(rowIndex - startRow);
              }


              
          public Object getWrappedData() {
                  
          return page.getData();
              }


              
          /**
               * Return true if the rowIndex value is currently set to a value that
               * matches some element in the dataset. Note that it may match a row that is
               * not in the currently cached DataPage; if so then when getRowData is
               * called the required DataPage will be fetched by calling fetchData.
               
          */

              
          public boolean isRowAvailable() {
                  DataPage page 
          = getPage();
                  
          if (page == null{
                      
          return false;
                  }

                  
          int rowIndex = getRowIndex();
                  
          if (rowIndex < 0{
                      
          return false;
                  }
           else if (rowIndex >= page.getDatasetSize()) {
                      
          return false;
                  }
           else {
                      
          return true;
                  }

              }


              
          /**
               * Method which must be implemented in cooperation with the managed bean
               * class to fetch data on demand.
               
          */

              
          public abstract DataPage fetchPage(int startRow, int pageSize);

              
          /**
               * 添加了刷新模塊
               
          */

              
          public void reflash() {
                  
          if (this.page != null{
                      
          this.page = null;
                      getPage();
                  }

              }

          }


          TestBean.java:被datatable調(diào)用的類
          public class TestBean
          {
              
          //不同的分頁(yè)需求需要添加不同的dataModel,并實(shí)現(xiàn)不同的getDataModel
              
          //例如,需要獲得有名稱條件的分頁(yè)可以定義dataModelName,和getDataModelName()實(shí)現(xiàn)
              private DataModel dataModel;

              
          public DataModel getDataModel()
              
          {

                  
          if ( dataModel == null ) {
                      dataModel 
          = new PagedListDataModel(10)
                      
          {
                          
          /**
                           * 用不同的條件獲得數(shù)據(jù)集和數(shù)量
                           
          */

                          
          public DataPage fetchPage(int startRow, int pageSize)
                          
          {
                              
          // call enclosing managed bean method to fetch the data
                              CarBeanDAO dao = new CarBeanDAO();
                              String sql 
          = "from CarBean model order by model.id desc";                
                              Query query 
          = EntityManagerHelper.createQuery(sql);                    
                              query.setFirstResult(startRow);                    
                              query.setMaxResults(pageSize);                    
                              List list 
          = query.getResultList();
                              System.out.println(
          "current row count = " + list.size());
                              Query q 
          = EntityManagerHelper.createQuery("select count(*) from CarBean");
                              
          return new DataPage(Integer.parseInt(q.getSingleResult().toString()), startRow, list);
                          }

                      }
          ;
                  }

                  
                  
          return dataModel;

              }

          }
          配置文件:
           <managed-bean>
            
          <managed-bean-name>testBean</managed-bean-name>
            
          <managed-bean-class>com.jsecode.util.pagination.TestBean</managed-bean-class>
            
          <managed-bean-scope>session</managed-bean-scope>
           
          </managed-bean> 

          有人反映會(huì)出現(xiàn)獲取兩次DataModel的情況,為什么呢?
          經(jīng)過(guò)我的測(cè)試,是因?yàn)樵O(shè)置的datatable的rows屬性的值大于了程序中pageSize的值,比如你設(shè)置rows="12",而你的pageSize設(shè)置的是10,那么JSF為了滿足顯示12條得條件,就會(huì)取兩次數(shù)據(jù)集組成一個(gè)12跳得數(shù)據(jù)集顯示出來(lái)。


          ---------------------------------------------------------
          專注移動(dòng)開(kāi)發(fā)

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2008-10-29 16:52 TiGERTiAN 閱讀(2726) 評(píng)論(1)  編輯  收藏 所屬分類: JavaJSF

          評(píng)論:
          # re: RichFaces中使用datatable和datascroller進(jìn)行分頁(yè)(使用數(shù)據(jù)庫(kù)分頁(yè),改良版)(含源碼)(JSF 1.2,RichFaces 3.2.1GA) 2008-10-30 16:34 | 韓現(xiàn)龍未登錄
          太好了!正在找這個(gè),感謝樓主。
          我由原來(lái)的C#陣營(yíng)轉(zhuǎn)到了java,還希望樓主能夠多多指教!
          加我MSN:hanxianlong@hotmail.com  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 和林格尔县| 千阳县| 承德县| 永嘉县| 荣昌县| 象山县| 武隆县| 临沧市| 永城市| 五华县| 保康县| 乌拉特后旗| 梓潼县| 拉孜县| 岳池县| 郓城县| 盖州市| 连城县| 乌兰浩特市| 鄂伦春自治旗| 哈密市| 镇沅| 大埔县| 佛学| 安徽省| 黄大仙区| 永顺县| 高平市| 鲁甸县| 池州市| 会泽县| 沧源| 宣恩县| 沐川县| 综艺| 姜堰市| 丰城市| 陆河县| 咸丰县| 崇义县| 庆安县|