java 数据表排序问题(JSF2.0 + primefaces)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5487752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:21:00  来源:igfitidea点击:

dataTable sorting problem (JSF2.0 + primefaces)

javajsfjakarta-eejsf-2primefaces

提问by sfrj

I dont know why my dataTable does not sort the columns when i click on the sort arrow. It only works if i first type something on the filter and erase it.(It is like it needs to have at least one character on the filter to be able to sort correctly).

我不知道为什么当我单击排序箭头时我的 dataTable 没有对列进行排序。只有当我首先在过滤器上输入一些东西并删除它时它才有效。(就像过滤器上至少需要有一个字符才能正确排序)。

I will paste the code here:

我将在这里粘贴代码:

This is the JSF page with the dataTable

这是带有数据表的 JSF 页面

<!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:t="http://myfaces.apache.org/tomahawk"
xmlns:p="http://primefaces.prime.com.tr/ui">
    <ui:composition template="WEB-INF/templates/BasicTemplate.xhtml">
<ui:define name="resultsForm">
<h:form enctype="multipart/form-data">
    <h:inputText id="search" value="" /><h:commandButton value="search"/>

    <p:dataTable var="garbage" value="#{resultsController.allGarbage}" dynamic="true" paginator="true" paginatorPosition="bottom" rows="10"  
             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
             rowsPerPageTemplate="5,10,15">         

            <p:column filterBy="#{garbage.filename}" filterMatchMode="startsWith" sortBy="#{garbage.filename}" parser="string">  
            <f:facet name="header">  
            <h:outputText value="Filename" />  
            </f:facet>  
            <h:outputText value="#{garbage.filename}" />
             </p:column> 

            <p:column filterBy="#{garbage.description}" filterMatchMode="contains">  
            <f:facet name="header">  
            <h:outputText value="Description" />  
            </f:facet>  
            <h:outputText value="#{garbage.description}" />  
             </p:column> 

            <p:column sortBy="#{garbage.uploadDate}" parser="string">  
            <f:facet name="header">  
            <h:outputText value="Upload date" />  
            </f:facet>  
            <h:outputText value="#{garbage.uploadDate}" /> 
             </p:column>                
    </p:dataTable> 
</h:form>
</ui:define>

Here the managed bean that interacts with that page:

这里是与该页面交互的托管 bean:

@ManagedBean
@ViewScoped implements Serializable
public class ResultsController {

@EJB
private ISearchEJB searchEJB;

private Garbage garbage;

public List<Garbage> getAllGarbage() {
    return searchEJB.findAllGarbage();
}

public Garbage getGarbage() {
    return garbage;
}

public void setGarbage(Garbage garbage) {
    this.garbage = garbage;
}   

The EJB that accesses the database:

访问数据库的 EJB:

@Stateless(name = "ejbs/SearchEJB")
public class SearchEJB implements ISearchEJB {

@PersistenceContext
private EntityManager em;   
public List<Garbage> findAllGarbage() {
    Query query = em.createNamedQuery("findAllGarbage");
    List<Garbage> gList = new ArrayList<Garbage>();

    for (Object o : query.getResultList()) {
        Object[] cols = (Object[]) o;
        Garbage tmpG = new Garbage();
        tmpG.setFilename(cols[0].toString());
        tmpG.setDescription(cols[1].toString());
        tmpG.setUploadDate(cols[2].toString());

        gList.add(tmpG);
    }
    return gList;
}

}

}

The entity with the JPQL named query being used:

使用 JPQL 命名查询的实体:

    @NamedQuery(name = "findAllGarbage", query = "SELECT g.filename, g.description,    g.uploadDate FROM Garbage g;")
    @Entity
    public class Garbage implements Serializable{

@Id
@GeneratedValue
@Column(nullable = false)
private Long id;
@Column(nullable = false)
private String filename;
@Column(nullable = false)
private String fileType;
@Column(nullable = false)
private String uploadDate;
@Column(nullable = false)
private String destroyDate;
@Lob
@Column(nullable = false)
private byte[] file;
@Column(nullable = false)
private String description;

A print screen with browsers output

带有浏览器输出的打印屏幕

enter image description here

在此处输入图片说明

回答by alfonx

My experience is, that the primefaces backing bean (ViewScoped!) must hold it's own List of the rows. So, e.g., if you query the database every time you request the p:dataTable:value sorting will not work. Solution: Collect the list from the Database and keep it in a local List variable in the backing bean.

我的经验是,primefaces 支持 bean(ViewScoped!)必须拥有它自己的行列表。因此,例如,如果每次请求 p:dataTable:value 时都查询数据库,则排序将不起作用。解决方案:从数据库中收集列表并将其保存在支持 bean 的本地 List 变量中。

In the code you provided

在您提供的代码中

public List<Garbage> getAllGarbage() {
    return searchEJB.findAllGarbage();
}

you get the list on every request. It doesn't work like that by design. Hope that helps.

你会得到每个请求的列表。它不是按照设计那样工作的。希望有帮助。

回答by Webel IT Australia - upvoter

I would like to confirm that I experience exactly the same problem described with primefaces-2.2.1.

我想确认我遇到了与 primefaces-2.2.1 描述的完全相同的问题。

The value (row elements) of my dataTable are computed from a query.

我的 dataTable 的值(行元素)是从查询中计算出来的。

Sorting on a simple name String property fails unless I also have a filterBy, and even then it only works if I type in at least one letter into the filter box. Then I can sort ascending/descending within the filtered result.

除非我也有一个 filterBy,否则对简单名称 String 属性的排序会失败,即使这样,它也只有在我在过滤器框中输入至少一个字母时才有效。然后我可以在过滤结果中升序/降序排序。

Webel

韦贝尔

回答by Webel IT Australia - upvoter

Added as answer not comment to previous answer since long

添加为答案而不是评论很久以前的答案

Update: Primefaces 3.5 seems to have fixed this p:dataTable sortBy problem that I reported on 2.2.1 and was still a problem on 3.3.

更新:Primefaces 3.5 似乎已经修复了这个我在 2.2.1 上报告的 p:dataTable sortBy 问题,并且在 3.3 上仍然是一个问题。

However, on Mac OS X, p:dataTable only works correctly on some Firefox versions on some Max OS X versions.

但是,在 Mac OS X 上,p:dataTable 只能在某些 Max OS X 版本的某些 Firefox 版本上正常工作。

It is OK on Firefox 20.0 on Mac OS X 10.6.8 (both still supported).

在 Mac OS X 10.6.8 上的 Firefox 20.0 上没问题(仍然支持)。

There are a number of very strange problems on Firefox 16.0.2 (which is end of line) on Mac OS X 10.5.8 (also end of line), such as the edit icons appearing as pen, tick, cross and row editing not activating at all, the sort icon not appearing, and on sorting the column headers explode and are repeated strangely offset to the right of all column headers. Row-editing worked fine with Primefaces 3.3 on Firefox 16.0.2 (which is end of line) on Mac OS X 10.5.8.

Mac OS X 10.5.8(也是行尾)上的Firefox 16.0.2(行尾)有很多很奇怪的问题,比如编辑图标显示为钢笔、勾号、十字和行编辑不完全激活,排序图标不会出现,并且在对列标题进行排序时会爆炸并奇怪地重复偏移到所有列标题的右侧。行编辑在 Mac OS X 10.5.8 上的 Firefox 16.0.2(即行尾)上与 Pr​​imefaces 3.3 配合良好。

On Mac OS X 10.5.8 row editing and sortBy are both ok on Safari 5.0.6 and also Chrome 21.0.1180.90.

在 Mac OS X 10.5.8 上,行编辑和 sortBy 在 Safari 5.0.6 和 Chrome 21.0.1180.90 上都可以。

These tests were performed on Glassfish 3.1.1 (in Netbeans7.1). Primefaces 3.3

这些测试是在 Glassfish 3.1.1(在 Netbeans7.1 中)上进行的。素面 3.3

http://code.google.com/p/primefaces/issues/detail?id=2476

http://code.google.com/p/primefaces/issues/detail?id=2476

http://forum.primefaces.org/viewtopic.php?f=3&t=14845

http://forum.primefaces.org/viewtopic.php?f=3&t=14845

http://forum.primefaces.org/viewtopic.php?f=3&t=14838

http://forum.primefaces.org/viewtopic.php?f=3&t=14838