注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

流星永恒的博客

JSF,Facelets,Rich(Prime)Faces,和java的笔记

 
 
 

日志

 
 

How to restore currentPage of primefaces datatable  

2013-07-27 12:55:30|  分类: jsf2.0 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
When we using primefaces datatable, always have this usecase, for example, from the user list page to create page or to edit page, then back to the list page, the list page then display the first page (index is '0') of the user data, not the page index before we did create or edit operation, this article will share a method how to restore the page.

We have a session scope managed bean named UserBean, like following code.

@ManagedBean

@SessionScope
public class UserBean {
private LazyDataModel<User> dataModel;
@Autowired
private UserService service;

private User selected;

private boolean restorePage = false;

private int currentPage = 0;



@PostConstruct
private void initBean() {
dataModel = new LazyDataModel<User>() {
private List<User> data;
/**
* serial number
*/
private static final long serialVersionUID = -3193833863010533843L;

@Override
public List<User> load(int first, int pageSize, String sortField,
SortOrder sortOrder, Map<String, String> filters) {
if (restorePage) {
restorePage = false;
first = currentPage*pageSize;
} else {
currentPage = first/pageSize;
}

data = service.loadUsers(first, pageSize);
setRowCount(50); //all records count, your code may be service.queryCount();
return data;
}

@Override
public User getRowData(String rowKey) {
if (null != data) {
User go = CollectionUtils.findByProperty(data, "id",
new Long(rowKey));
return go;
} else {
return null;
}
}

@Override
public Object getRowKey(User u) {
return u.getId();
}
};
}

...

public String preCreate() {

restorePage = true;

return ...

}

...


we let the property "currentPage" save the pageIndex, and if we do a create/edit operation, then we set restorePage to true before the action, and back to list if you done your operation.

On the list page we should add javascript code to let databale display right page index.

<p:commandButton value="Add User" action="#{userBean.preCreate}" immediate="true"></p:commandButton>

<p:dataTable value="#{userBean.dataModel}" var="u" rows="10"
rowIndexVar="rowIndex"
lazy="true"
paginator="true"
rowKey="#{u.id}"
selectionMode="single"
widgetVar="dataTable"
selection="#{userBean.selected}">
<p:column headerText="ID">
<h:outputText value="#{rowIndex+1}" />
</p:column>
<p:column headerText="Name">
<h:outputText value="#{u.name}" />
</p:column>
<p:column headerText="Career">
<h:outputText value="#{u.career}" />
</p:column>
</p:dataTable>

<script type="text/javascript">
jQuery(function(){
PF('dataTable').getPaginator().setPage(#{userBean.currentPage});
});

</script>

Note: You should put javascript code behind the datatable, because the js action of primefaces datatable is added using jquery ready function too.

You can use java code restore the page too, there is no need javascript code anymore.
DataTable dataTable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent("..:mydatatable");
dataTable.setFirst(
currentPage*pageSize);
Then if you list display page 4 (page index is 3), then you click "Add User" button, to add a new user. then back to the list page, the page still displays the page 4, not the first page.
I using JSF 2.1, primefaces 4.0-SNAPSHOT.

GOOD LUCK!
  评论这张
 
阅读(1296)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017