PrimeFaces数据表默认排序方式 [英] PrimeFaces datatable default sortBy from backing bean

查看:71
本文介绍了PrimeFaces数据表默认排序方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有POJO对象的数据表:

I have a data table with a POJO object:

<p:dataTable id="table" var="object" sortBy="#{object.name}" sortOrder="DESCENDING">

例如,

object具有字段idnamedatesize.我可以使用xhtml设置默认的排序字段,但我想从支持bean设置它.

object has fields id, name, date, size for example. I am able to set default sort field using xhtml, but I want set it from backing bean.

当用户创建排序请求(例如name)时,我能够解析列id.

I am able to parse column id when user creates sort request for example name.

public void sortEventListener(AjaxBehaviorEvent actionEvent) {
    String id = ((SortEvent) actionEvent).getSortColumn().getColumnKey();
    String[] idPath = id.split(":");
    sortBy = idPath[idPath.length - 1];
    sortOrder = ((SortEvent) actionEvent).isAscending();
}

我的任务检测到用户想要对哪一列进行排序并将其持久化到db.重新加载后,数据表应按此列排序.

My task detects which column user wants to sort and persists it to db. After reload the data table should be sorted by this column.

设置后

sortBy="#{bean.sortBy}" // sortBy = name

它不起作用,呈现页面后数据表未排序.

it's not working and data table is not sorted after rendering the page.

请帮助.

推荐答案

如果将数据表绑定到bean 中的org.primefaces.component.datatable.DataTable对象,或者在bean中找到表组件,则可以使用表对象以编程方式设置sortBy值表达式.

If you bind your data table to a org.primefaces.component.datatable.DataTable object in your bean or find the table component in your bean, you can use the table object to set the sortBy value expression programmatically.

要了解PrimeFaces如何处理排序,可以查看

To get an idea how PrimeFaces is handling sorting, you can have a look at the source code at GitHub.

具有排序列时,可以轻松获取排序值表达式.因此,在您的听众中,您可以使用类似以下内容的

When you have the sort column, you can easily get the sort value expression. So, in your listener you could use something like:

UIColumn sortColumn = sortEvent.getSortColumn();
ValueExpression sortByVE = sortColumn.getValueExpression("sortBy");

顺便说一句,您可以在排序侦听器方法中将参数类型AjaxBehaviorEvent替换为SortEvent.

By the way, you can replace the parameter type AjaxBehaviorEvent with SortEvent in your sort listener method.

现在,存储sortByVE表达式,并在需要时将其设置为绑定表的sortBy值表达式:

Now, store the sortByVE expression, and set it as the sortBy value expression of the bound table when needed:

dataTable.setValueExpression("sortBy", sortByVE);

如果要从头开始创建值表达式,请使用类似以下内容的

If you want to create the value expression from scratch, use something like:

FacesContext context = FacesContext.getCurrentInstance();
ExpressionFactory ef = context.getApplication().getExpressionFactory();
ValueExpression ve = ef.createValueExpression(context.getELContext(),
                                              "#{object.name}",
                                              Object.class);
dataTable.setValueExpression("sortBy", ve);

在此示例中,"#{object.name}"是固定的.您应该基于从排序侦听器获得的值来构造它.

In this example "#{object.name}" is fixed. You should construct it based on the value you get from your sort listener.

如果要在bean中查找表, OmniFaces组件可能会有所帮助.它还提供了创建值表达式的快捷方式.

If you want to find your table in your bean, OmniFaces Components might be helpful. It also offers a shortcut method to create value expressions.

另请参阅:

  • How does the 'binding' attribute work in JSF? When and how should it be used?
  • Programmatically getting UIComponents of a JSF view in bean's constructor
  • How do I set the value of HtmlOutputTag in JSF?

这篇关于PrimeFaces数据表默认排序方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆