隨筆-348  評論-598  文章-0  trackbacks-0
          jsp頁面中的相關(guān)代碼:
              <f:view>
                  
          <h:form>
                      
          <rich:dataTable id="carList" width="483" rows="10" columnClasses="col"
                          value
          ="#{user.allCars}" 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>
          后臺(tái)User類的代碼:
          public class User
          {

              
          private int scrollerPage = 1;
              
              
          private List<CarBean> allCars; 
              
              
          public User()
              
          {

              }

              
              
          public int getScrollerPage()
              
          {
                  
          return scrollerPage;
              }


              
          public void setScrollerPage(int scrollerPage)
              
          {
                  
          this.scrollerPage = scrollerPage;
                  System.out.println(
          "current page = " + scrollerPage);
              }


              
          public List<CarBean> getAllCars()
              
          {
                  CarBeanDAO dao 
          = new CarBeanDAO();
                  Query q 
          = EntityManagerHelper.createQuery("from CarBean cars order by cars.id desc");

                  
          return q.getResultList();
              }

              
              
          public void setAllCars(List<CarBean> allCars)
              
          {
                  
          this.allCars = allCars;
              }

              
              
          public String delete()
              
          {
                  FacesContext ctx 
          = FacesContext.getCurrentInstance();
                  
          int id = Integer.parseInt(ctx.getExternalContext().getRequestParameterMap().get("id"));
                  EntityManagerHelper.beginTransaction();
                  CarBeanDAO dao 
          = new CarBeanDAO();
                  CarBean bean 
          = dao.findById(id);
                  
          if(bean != null)
                      dao.delete(bean);
                  EntityManagerHelper.commit();
                  
          return null;
              }

          }

          datatable+datascroller使用的是偽分頁的方式,也就是說在數(shù)據(jù)庫層那并沒有分頁,只是程序一次讀出所有數(shù)據(jù)然后進(jìn)行程序分頁操作,這樣適合數(shù)據(jù)量不太大的分頁操作,數(shù)據(jù)量過大的話就需要自己寫數(shù)據(jù)模型進(jìn)行操作了。本分頁代碼加入了刪除記錄的功能,delete結(jié)果返回null會(huì)使刪除操作完成后返回當(dāng)前頁面,這樣就會(huì)發(fā)現(xiàn)剛才被刪除數(shù)據(jù)消失了,后面數(shù)據(jù)都上來一條。

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

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2008-10-28 21:50 TiGERTiAN 閱讀(2491) 評論(0)  編輯  收藏 所屬分類: JavaJSF
          主站蜘蛛池模板: 鄂尔多斯市| 宝山区| 鹰潭市| 札达县| 蕲春县| 汝州市| 增城市| 台南市| 宕昌县| 永和县| 张家川| 南康市| 宁夏| 手游| 东辽县| 博兴县| 曲水县| 承德县| 博野县| 曲阳县| 蒲江县| 德钦县| 洛川县| 耒阳市| 溧水县| 安平县| 鹿邑县| 公安县| 区。| 都匀市| 临桂县| 手游| 砚山县| 蒙城县| 诸暨市| 曲水县| 平陆县| 渝中区| 潜山县| 县级市| 金塔县|