1
votes

I keep on trying to fix the sortBy function of this dataTable(Primefaces component) but i just cant understand why it doesnt work, when other features like pagination or filter work correctly. For this dataTable i just need to pass an array for its tag attribute called "value" and also a single object of the same type of the array for the tag attribute called "var". Down below i will post my code.

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
@RequestScoped
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 {

@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

The console output when the page is refreshed (SEVERE: line 1:61 no viable alternative at character ';'):

enter image description here

2
What version of PrimeFaces are you using ?Mark

2 Answers

3
votes

Change the scope of the ResultsController to @ViewScoped. The ResultsController and the Garbage object will need to implement java.io.Serializable.


Maybe this will help. Below is my code that I got to work:

index.xhmtl

<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.prime.com.tr/ui">
    <f:view contentType="text/html" >
        <h:head>
            <title>PrimeFacesTest</title>
        </h:head>
        <h:body>
            <h:form>
                <p:dataTable var="garbage" value="#{indexBean.allGarbage}"
                             dynamic="true" paginator="true" paginatorPosition="bottom" rows="4"
                             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                             rowsPerPageTemplate="5,10,15">

                    <p:column filterBy="#{garbage.filename}" filterMatchMode="startsWith" sortBy="#{garbage.filename}" >
                        <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>

        </h:body>
    </f:view>
</html>

IndexBean.java

@ManagedBean
@ViewScoped
public class IndexBean implements Serializable {

    private ArrayList allGarbage;

    public IndexBean() {
        allGarbage = new ArrayList();
        allGarbage.add(new Garbage("Abc", "Abc", "28/03/2011 12:13:32"));
        allGarbage.add(new Garbage("Bbc;", "bbc", "28/03/2011 12:14:32"));
        allGarbage.add(new Garbage("Cbc", "Cbc", "28/03/2011 12:17:32"));
        allGarbage.add(new Garbage("1Abc", "1Abc", "28/03/2011 12:13:32"));
        allGarbage.add(new Garbage("2Bbc;", "2bbc", "28/03/2011 12:14:32"));
        allGarbage.add(new Garbage("3Cbc", "3Cbc", "28/03/2011 12:17:32"));
        allGarbage.add(new Garbage("4Abc", "4Abc", "28/03/2011 12:13:32"));
        allGarbage.add(new Garbage("5Bbc;", "5bbc", "28/03/2011 12:14:32"));
        allGarbage.add(new Garbage("6Cbc", "6Cbc", "28/03/2011 12:17:32"));
    }

    public ArrayList getAllGarbage() {
        return allGarbage;
    }

}

Garbage.java

public class Garbage implements Serializable {

    private String filename;
    private String description;
    private String uploadDate;

    public Garbage(String filename, String description, String uploadDate) {
        this.filename = filename;
        this.description = description;
        this.uploadDate = uploadDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getUploadDate() {
        return uploadDate;
    }

    public void setUploadDate(String uploadDate) {
        this.uploadDate = uploadDate;
    }

}
0
votes

This answer is a bit late but i hope it helps others. I had the same problem on Java7, JSF2 with Primefaces 4 and my fix was to initialize the ArrayList<> upon creation of the Bean. Otherwise the sort function fails to sort the data and works like described, only if something is filtered before.

So the Bean would look like:

@ManagedBean
@ViewScoped
public class IndexBean implements Serializable {

private ArrayList<Garbage> allGarbage;

@PostConstuct
public init() 
{
    allGarbage = new ArrayList<Garbage>();
    allGarbage.add(new Garbage("Abc", "Abc", "28/03/2011 12:13:32"));
    allGarbage.add(new Garbage("Bbc;", "bbc", "28/03/2011 12:14:32"));
    allGarbage.add(new Garbage("Cbc", "Cbc", "28/03/2011 12:17:32"));
    allGarbage.add(new Garbage("1Abc", "1Abc", "28/03/2011 12:13:32"));
    allGarbage.add(new Garbage("2Bbc;", "2bbc", "28/03/2011 12:14:32"));
    allGarbage.add(new Garbage("3Cbc", "3Cbc", "28/03/2011 12:17:32"));
    allGarbage.add(new Garbage("4Abc", "4Abc", "28/03/2011 12:13:32"));
    allGarbage.add(new Garbage("5Bbc;", "5bbc", "28/03/2011 12:14:32"));
    allGarbage.add(new Garbage("6Cbc", "6Cbc", "28/03/2011 12:17:32"));
}

public ArrayList<Garbage> getAllGarbage() 
{
    return allGarbage;
}

public void setAllgarbage(ArrayList<Garbage> garbage)
{
    this.allGarbage = garbage;
}

}

You don't need the 'filteredBy' and 'filterMatchMode' attributes anymore.