如何保留素数数据表的排序顺序? [英] How to retain the sort order of primefaces datatable?
问题描述
在我的项目中,我使用的是ViewScoped bean,我有两个页面,一个是主页,另一个是详细信息页面.
In my project I am using a ViewScoped bean and I have two pages one is main page and the other details page.
在主页上,我有一个带有排序和过滤功能的primefaces数据表. 数据表在每一行中都有一个链接.如果我对列进行排序,则它可以正常工作.如果单击主页上的链接,则它将转到显示相应行详细信息的详细信息页面.在详细信息页面中,我有一个后退按钮.如果单击它,将带我回到主页,但排序顺序未保留在数据表中.
In the main page I have a primefaces datatable with sorting and filtering functionality. The datatable has a link in each row. If I sort a column, then it works properly. If I click on a link in the main page, then it will go to the details page showing the details of the corresponding row. In the details page I have a back button. If I click on it, it will take me back to the main page, but the sort order is not retained in the datatable.
我需要保留排序顺序.我该如何实现?
I need to retain the sort order. How can I achieve this?
推荐答案
我知道这个问题已经很老了,但是我只是从事此工作,所以我想我会分享我的解决方案.
I know this question is pretty old, but I just worked on this so I figured I'd share my solution for the future.
我们正在使用PrimeFaces 3.5
We're using PrimeFaces 3.5
这仅在延迟加载表上实现,而从未在内存表上实现.可能存在的最大区别是,您需要为内存表存储列类型(类).
This was only implemented on a lazy loading table and never implemented on an in-memory table. The biggest difference that might exist is you would need to store the column type (Class) for an in-memory table.
首先,您需要某种可以将排序状态保存到的SessionScoped控制器.您需要存储两种状态:排序列和排序顺序(升/降).
First of all, you need some sort of SessionScoped controller that you can save the sort state to. You'll need to store two states: sort column and sort order (ascending/descending).
第二,将p:datatable绑定到ViewScoped控制器(binding="#{viewController.datatable}"
)中的对象,并为其实现基本的getter和setter.在setter方法中,我有这个:
Secondly, bind the p:datatable to an object in your ViewScoped controller (binding="#{viewController.datatable}"
), and implement the basic getter and setter for it. In the setter method, I have this:
public void setDatatable(DataTable datatable) {
ExpressionFactory expressionFactory = ExpressionFactory.newInstance();
ELContext elContext = FacesContext.getCurrentInstance().getELContext();
if(!datatableInitialized) {
if(getSessionController().getSortState() == null) {
datatable.setValueExpression("sortBy", expressionFactory.createValueExpression(elContext, DEFAULT_SORT_COLUMN, Object.class));
datatable.setSortOrder(DEFAULT_SORT_DIRECTION);
} else {
SortState state = getSessionController().getSortState();
datatable.setValueExpression("sortBy", expressionFactory.createValueExpression(elContext, state.getValueExpression(), Object.class));
datatable.setSortOrder(state.getDirection());
}
datatableInitialized = true;
}
this.datatable = datatable;
}
重要的位是setValueExpression行,createValueExpression方法的第二个参数需要JSF样式表达式,即:#{pojo.stuff}
.还请注意,我只是使用Object.class作为类型,我相信我可以避免这种情况,因为表是延迟加载的,并且我自己在LazyDataModel实现中处理所有排序.
The important bits are the setValueExpression lines, the second parameter of the createValueExpression method requires the JSF style expression, ie: #{pojo.stuff}
. Also notice how I'm just using Object.class as the type, I believe I can get away with this because the table is lazy loaded and I'm handling all the sorting myself in the LazyDataModel implementation.
第三,将排序事件添加到数据表中:
Third, add the sorting event to the datatable:
<p:ajax event="sort" listener="#{viewController.sortListener}" />
和控制器中的侦听器:
public void sortListener(SortEvent event) {
SortState state = new SortState();
state.setValueExpression(event.getSortColumn().getValueExpression("sortBy").getExpressionString());
state.setDirection(event.isAscending() ? "ascending" : "descending");
getSessionController().setOpportunitiesSortState(state);
}
就是这样.
这篇关于如何保留素数数据表的排序顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!