How to enable Sorting in DataTable.

UI Components for JSF
Post Reply
samwun9988
Posts: 69
Joined: 22 Jan 2011, 11:04

30 Jan 2011, 02:32

Hello,

I try to use Sorting in DataTable, for weeks, but I still can't get it to work.
The ShowCase on the primefaces website doesn't show other relevant files, eg. which js/css should be included in the example. Therefore when I just copied the code from the showcase, the Sorting is not working at all.

Can anyone please show me a complete example of a Sorting DataTable?

Your help is very much appreciated.

Environment: PF 2.2RC2, Glassfish v3, Jdk 1.6, Netbeans 6.9.1

Thanks
Sam
Primefaces 2.2.1, JBOSS 6.0.0.Final, JDK1.6

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

31 Jan 2011, 03:06

This depends on how you read the data for your dataTable. If you are using a List directly from Hibernate or something simlilar this doesn't work. It also doesn't work when you create a new List every time you call the get method for it.

You have to return a List which is a member variable of the bean. Additionally make sure your datatable is located within a <h:form>

samwun9988
Posts: 69
Joined: 22 Jan 2011, 11:04

31 Jan 2011, 11:26

Hello, Thank you very much for the suggestion.

I have changed the CustomerManagedBean class, with the list method coded as below:

Code: Select all

@ManagedBean(name="customer")
@SessionScoped
public class CustomerManagedBean 
{

    @EJB
    private CustomerSessionBean customerSessionBean;
    private Customer customer;
    private final static Logger logger = Logger.getLogger(CustomerManagedBean.class.getName());
    private List<Customer> custList;
    private LazyDataModel<Customer> lazyModel;

    public CustomerMBean() {
		
	}

    /**
     * Returns list of customer objects to be displayed in the data table
     * @return
     */
    public List<Customer> getCustomers()
    {
        List<Customer> retList = customerSessionBean.retrieve();
        custList = new ArrayList<Customer>(retList);
        System.out.println(custList.size());
        return custList;
    }

CustomerSessionBean class:

Code: Select all

@Stateless
@LocalBean
public class CustomerSessionBean {
    @Resource(name = "jms/NotificationQueue3")
    private Queue notificationQueue3;
    @Resource(name = "jms/NotificationQueue3Factory")
    private ConnectionFactory notificationQueue3Factory;
    @PersistenceContext(unitName = "CustomerApp-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }
....
   [b]public List<Customer> retrieve()[/b] {
        Query query = em.createNamedQuery("Customer.findAll");
        return query.getResultList();
    }
....

Code: Select all

<h:form>
<p:dataTable value="#{customer.customers}" var="cust" paginator="true"
                         paginatorTemplate="{RowsPerPageDropDown} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {CurrentPageReport}" rows="8"
                         emptyMessage="No Records to Display"
                         >
        <p:column sortBy="#{cust.zip}" filterBy="#{cust.zip}">
            <f:facet name="header">
                <h:outputText value="Zip" />
            </f:facet>
            <h:outputText value="#{cust.zip}" />
        </p:column>

        <p:column sortBy="#{item.city}" filterBy="#{item.city}">
            <f:facet name="header">
                <h:outputText value="City" />
            </f:facet>
            <h:outputText value="#{item.city}" />
</p:datatable>

</h:form>

Sorting is still not working as expected.

Thanks
Sam.
Primefaces 2.2.1, JBOSS 6.0.0.Final, JDK1.6

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

31 Jan 2011, 13:36

Just for testing purposes, try the following code:

Code: Select all

public List<Customer> getCustomers()
{
    if (custList == null) {
        List<Customer> retList = customerSessionBean.retrieve();
        custList = new ArrayList<Customer>(retList);
        System.out.println(custList.size());
    }
    return custList;
}

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 16 guests