posts - 5,  comments - 7,  trackbacks - 0
jsp页面中的相关代码:
    <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>
后台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使用的是伪分页的方式,也就是说在数据库层那并没有分页,只是程序一次读出所有数据然后进行程序分页操作,这样适合数据量不太大的分页操作,数据量过大的话就需要自己写数据模型进行操作了。本分页代码加入了删除记录的功能,delete结果返回null会使删除操作完成后返回当前页面,这样就会发现刚才被删除数据消失了,后面数据都上来一条。
posted on 2008-11-23 00:43 Vincent-chen 阅读(350) 评论(0)  编辑  收藏 所属分类: richfaces

只有注册用户登录后才能发表评论。


网站导航: