PrimeFaces 数据表排序不起作用 [英] PrimeFaces dataTable sorting not working

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

问题描述

我无法让 PrimeFaces dataTable 组件的排序行为按照文档进行工作.(我正在使用 PrimFaces 4.0、JSF 2.1.12 和 Tomcat 7.0.)据我所知,我看到的问题与与 PF dataTable 相关的任何其他问题报告/讨论都不对应.为了探索这个问题,我创建了一个基于 ShowCase 示例的示例,该示例使用排序的 dataTable,复制了 tableBean 支持 bean 的 ShowCase 源代码(包括示例的本地汽车数据的生成;不涉及外部数据库访问)和配套的 Car 类.xhtml 也是 ShowCase 示例的非常接近的副本:

I am having trouble getting the PrimeFaces dataTable component's sort behavior to work as documented. (I am using PrimFaces 4.0, JSF 2.1.12, and Tomcat 7.0.) The problem I am seeing doesn't correspond to any of the other problem reports/discussions related to PF dataTable, as far as I can tell. To explore the problem I created an example based closely on the ShowCase example of using a sorted dataTable, copying the ShowCase source code for the tableBean backing bean (including the generation of local car data for the example; no external DB access is involved) and the supporting Car class. The xhtml is also a very close copy of the ShowCase example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
    <h:head>
    </h:head>
    <h:body>
           <h:form>

               <p:dataTable id="dataTable" var="car" value="#{tableBean.carsSmall}">
                   <f:facet name="header">
                       Ajax Sorting
                   </f:facet>

                   <p:column id="modelHeader" sortBy="#{car.model}">
                       <f:facet name="header">
                           <h:outputText value="Model" />
                       </f:facet>
                       <h:outputText value="#{car.model}" />
                   </p:column>

                   <p:column sortBy="#{car.year}">
                       <f:facet name="header">
                           <h:outputText value="Year" />
                       </f:facet>
                       <h:outputText value="#{car.year}" />
                   </p:column>

                   <p:column sortBy="#{car.manufacturer}">
                       <f:facet name="header">
                           <h:outputText value="Manufacturer" />
                       </f:facet>
                       <h:outputText value="#{car.manufacturer}" />
                   </p:column>

                   <p:column sortBy="#{car.color}">
                       <f:facet name="header">
                           <h:outputText value="Color" />
                       </f:facet>
                       <h:outputText value="#{car.color}" />
                   </p:column>
               </p:dataTable>

           </h:form>
    </h:body>
</html>

当 xhtml 运行时,数据表显示出来,但只有一列显示为可用于排序(即,标题中有向上/向下箭头图标).

When the xhtml is run, the data table shows up, but with only one column displayed as being available for sorting (i.e., with the up/down arrow icon in the header).

dataTable有两个问题:

The dataTable has two problems:

  1. 只有一列(Year)显示为可用于排序.(Year 是 Car 类中int"类型的属性,而其他三列是 String 类型,因此问题的一个方面是 String 字段的 sortBy="#{car.xxx}" 标记被忽略.)
  2. Year 列实际上是不可排序的.单击年份"标题的向上/向下箭头无效.单击 Year 标题时会发生服务器回调,但表未排序.我已经追踪到在服务器回调期间发生的 ELException,其中代码无法处理表达式#{car.0}".毫无疑问,0"应该是年",而失败的表达式无疑是没有排序发生的原因.

在弄清楚为什么这个尝试使用 PrimeFaces 可排序数据表的非常简单的示例(几乎是逐字复制自 ShowCase 来源)让我感到悲伤时,我们将不胜感激.

Any help would be appreciated in figuring out why this very simple example (copied almost verbatim from the ShowCase sources) of trying to use a PrimeFaces sortable dataTable is giving me grief.

推荐答案

你的第一个字符串是

<p:dataTable id="dataTable" var="car" value="#{tableBean.cars}">

所以 tableBean 有一个方法

so tableBean has a method

public List<Car> getCars()
{
    return carEJB.findAll();
}

但是你的 bean 没有变量来保存排序后的方法结果.

but your bean has no variable to save method result after sort.

解决方案:

public class CarController
{
...
    private List<Car> cars;
...
    privare void reset()
    {
        cars = carEJB.findAll();
    }
...
    public List<Car> getCars()
    {
        return cars;
    }
}

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

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