-->

dataTable sorting problem (JSF2.0 + primefaces)

2020-07-30 09:02发布

问题:

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).

I will paste the code here:

This is the JSF page with the dataTable

<!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:

@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:

@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:

    @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

回答1:

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

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

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.

Webel



回答2:

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.

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.



回答3:

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.

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

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

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.

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.

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

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=14838