Sort Issue on p:datatable

UI Components for JSF
Post Reply
givemefive
Posts: 4
Joined: 25 Nov 2010, 21:46

10 Dec 2010, 13:41

Hi, this is my first post on primefaces.
I defined a p:datatable with sorting but when i try to sort a column nothing happens.
I'm using Primefaces 2.2 RC2, Mojarra 2.0.3 (FCS b03) an Jboss 5.1.0.GA
Thanks.
Giorgio

t1.xthml template

Code: Select all

<?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:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <f:view contentType="text/html">
        <h:head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <link type="text/css" rel="stylesheet" href="#{request.contextPath}/resources/css/default.css" />
            <link type="text/css" rel="stylesheet" href="#{request.contextPath}/resources/themes/#{loginBean.theme}/skin.css" />
            <link type="text/css" rel="stylesheet" href="#{request.contextPath}/resources/css/syntaxhighlighter.css" />
            <link type="text/css" rel="stylesheet" href="#{request.contextPath}/resources/css/icone.css" />
            <style type="text/css">
                    .new {
                            background: url(../resources/images/new-ico.gif) no-repeat 1%;
                    }

                    .ui-widget {
                            font-size: 98% !important;
                    }

                    .ui-menu .ui-menu-item .ui-menu-item-label {
                            padding: .2em 1.7em;
                    }

                    .ui-growl {
                            top:115px;
                    }

                    .themeMenu {
                            overflow: auto;
                            height:300px;
                            width:200px;
                    }
            </style>
            <link rel="shortcut icon" href="#{request.contextPath}/favicon.ico" />
            <title>E.A.Sy.Lab - Enhanced Automation System Laboratory</title>
        </h:head>

        <h:body>

            <div id="top">
                <ui:insert name="top">Top</ui:insert>
            </div>

            <div id="content" class="center_content">
                <ui:insert name="content">Content</ui:insert>
            </div>

            <div id="bottom">
                <ui:insert name="bottom">Bottom</ui:insert>
            </div>

        </h:body>
    </f:view>
</html>
template client page:

Code: Select all

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./../template/t1.xhtml"
                xmlns:p="http://primefaces.prime.com.tr/ui">

    <ui:define name="top">
        <ui:include src="./top.xhtml"/>
    </ui:define>

    <ui:define name="content">
        <p:panel id="pannello">
            <ui:include src="#{menuBarBean.includedPage}"/>
        </p:panel>
    </ui:define>

    <ui:define name="bottom">
        <ui:include src="./footer.xhtml"/>
    </ui:define>

</ui:composition>
top.xhtml

Code: Select all

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:f="http://java.sun.com/jsf/core">    
    <body>
        <h:form id="topForm">            
            <p:panel style="border: 0px;">
                <p:graphicImage id="logo" value="./../resources/images/logo.png" />

                <h:outputLabel value="#{loginBean.utente.riferimento}" style="font-weight: bold;"></h:outputLabel>                
                <h:outputLabel value="Ultimo accesso: #{loginBean.utente.ultimoAccesso}" style="font-weight: bold;"></h:outputLabel>
            </p:panel>

            <!-- MENU' -->
            <p:growl id="menu"/>
            <p:menubar effect="NONE" autoSubmenuDisplay="true">
                <p:submenu label="File">
                    <p:menuitem value="Esci" ajax="false" action="#{loginBean.logout}"/>
                </p:submenu>

                <p:submenu label="Tabelle">
                        <p:menuitem id="menuUdM" value="Unità di misura" ajax="true" update="pannello" actionListener="#{menuBarBean.sideBarAction}">
                            <f:param id="menuUdmParam" name="pageViewId" value="udm" />
                        </p:menuitem>                        
		</p:submenu>
                
            </p:menubar>
        </h:form>
    </body>
</html>
MenuBarBean.java

Code: Select all

package it.artech.superlab.view;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.apache.log4j.Logger;

@ManagedBean
@ViewScoped
public class MenuBarBean implements Serializable {

    private String includedPage = "";
    
    public MenuBarBean() {
    }

    public void sideBarAction(ActionEvent actionEvent)
    {
        FacesContext context = FacesContext.getCurrentInstance();
        String selectedPageViewId = context.getExternalContext().getRequestParameterMap().get("pageViewId");
        
	if (selectedPageViewId.equalsIgnoreCase("udm"))
        {
            includedPage = "prova.xhtml";
        }        
    }

	public String getIncludedPage()
	{
		return includedPage;
	}

	public void setIncludedPage(String includedPage)
	{
		this.includedPage = includedPage;
	}

}
prova.xhtml

Code: Select all

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:f="http://java.sun.com/jsf/core">
    <body>
        <h:form id="testForm">
            <h:head />
            <h1><h:outputText value="List"/></h1>
            <p:dataTable value="#{udmBean.listaUdm}" var="item"
                         selectionMode="single">
                <p:column sortBy="#{item.idUnita}">
                    <f:facet name="header">
                        <h:outputText value="IdUnita"/>
                    </f:facet>
                    <h:outputText value="#{item.idUnita}"/>
                </p:column>
                <p:column sortBy="#{item.descrizione}">
                    <f:facet name="header">
                        <h:outputText value="Descrizione"/>
                    </f:facet>
                    <h:outputText value="#{item.descrizione}"/>
                </p:column>
            </p:dataTable>
        </h:form>    

    </body>
</html>
UnitaDiMisuraBean.java

Code: Select all

package it.artech.superlab.view;

import it.artech.superlab.ejb.entity.archivioanalisi.UnitaDiMisura;
import it.artech.superlab.ejb.session.client.local.GestioneArchivioAnalisiLocal;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.apache.log4j.Logger;

@ManagedBean(name="udmBean")
@RequestScoped
public class UnitaDiMisuraBean {
    @EJB
    private GestioneArchivioAnalisiLocal gestArchivio;

    private final static Logger logger = Logger.getLogger(UnitaDiMisuraBean.class.getName());

    List<UnitaDiMisura> listaUdm = new ArrayList<UnitaDiMisura>();
    UnitaDiMisura udm;
    
    /** Creates a new instance of UnitaDiMisuraBean */
    public UnitaDiMisuraBean() {
    }

    public List<UnitaDiMisura> getListaUdm() {
        listaUdm = gestArchivio.getElencoUnitaDiMisura();
        
        return listaUdm;
    }

    public void setListaUdm(List<UnitaDiMisura> listaUdm) {
        this.listaUdm = listaUdm;
    }

    public UnitaDiMisura getUdm() {
        return udm;
    }

    public void setUdm(UnitaDiMisura udm) {
        this.udm = udm;
    }
}

robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

10 Dec 2010, 21:54

I have the same problem right now. Does the filter-mechanism work in your case?

givemefive
Posts: 4
Joined: 25 Nov 2010, 21:46

10 Dec 2010, 22:52

No, it doesn't work, and paginator too. :(

robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

11 Dec 2010, 12:59

In my case, the filtering an pagination works, but not the sorting mechanism... But i don't know what's causing the problem in your case!

rexcardan
Posts: 1
Joined: 24 Sep 2010, 15:55

13 Dec 2010, 05:21

I found that the sort does not work in the case that I call a method that returns a list.. eg

Code: Select all

<p:dataTable value={#userManager.allUsers} var=user>
<p:column sortBy=#{user.name}>
....
Where the userManager bean method looks like

Code: Select all

public UserManager(){
}

public List<User> getAllUsers(){
//call user ejb facade and pull users from database
List<User> users = userFacade.getAllUsers();
return users;
However, if I create a variable named allUsers and create the setAllUsers and getAllUsers methods, the sort works! Looks like:

Code: Select all

<p:dataTable value={#userManager.allUsers} var=user>
<p:column sortBy=#{user.name}>
....
Where the userManager bean method looks like

Code: Select all

@RequestScoped
public UserManager{
private List<User> allUsers;
//constructor
public UserManager(){

}
//after contructor is called do this
@PostConstruct
private init(){
allUser = getAllUsersFromDB();
}

public List<User> getAllUsersFromDB(){
//call user ejb facade and pull users from database
List<User> users = userFacade.getAllUsers();
return users;
}

//Getter and Setter Methods
public List<User> getAllUsers(){
return allUsers;
}

public void setAllUsers(List<User> allUsers){
this.allUsers = allUsers;
}
No idea why this would be the case, but it seems to work for me. I don't even need to use dynamic="true" in the dataTable declaration.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 16 guests