Primefaces Datatable row selection

jsfjsf-2primefaces

Edit2 – I've added the faces-config.xml at the end of the post.

I'm having problems with Primefaces datatable row selection. I want to be able to select a row and move the data into an object that I can then manipulate. I'm using a model based on the primefaces showcase example, but it doesn't work. Frankly, I'm running out of ideas as to what is wrong. Below is my xhtml and managedbean.

<html xmlns="http://www.w3c.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui" >
<h:head>

</h:head>
<h:body>
<center>
<h:form id="form">

<p:dataTable id="personTable" var="client" value="#{tableBean.persons}" rowKey="#{client.name}"
             selection="#{tableBean.person}" selectionMode="single">

    <f:facet name="header">
        Click "View" button after selecting a row to see details
    </f:facet>

    <p:column headerText="Name">
        #{client.name}
    </p:column>

    <p:column headerText="Address">
        #{client.address}
    </p:column>

    <p:column headerText="Phone" >
        #{client.phone}
    </p:column>
</p:dataTable>

<h:panelGrid id="display" columns="2" cellpadding="4">


        <h:outputText value="Name:" />
        <h:outputText value="#{tableBean.person.name}" />

        <h:outputText value="Address:" />
        <h:outputText value="#{tableBean.person.address}" />

        <h:outputText value="Phone:" />
        <h:outputText value="#{tableBean.person.phone}" />

</h:panelGrid>

</h:form>

</center>
</h:body>
</html>

Managed Bean here:

package com.dave.test;

import java.util.ArrayList;
import java.util.List;

public class TableBean {

private List<Person> persons = null;
private Person person;

public TableBean() {
    persons = new ArrayList<Person>();
    persons.add(new Person("Jimmy", "18 Maple", "337-278-1019"));
    persons.add(new Person("Sally", "47 Oak", "787-509-3819"));
    persons.add(new Person("Roger", "754 Fifth Ave.", "926-420-8219"));
    persons.add(new Person("Mimi", "891 2nd St.", "713-371-8632"));

}

public List<Person> getPersons() {
    return persons;
}

public void setPersons(List<Person> persons) {
    this.persons = persons;
}

public Person getPerson() {
    return person;
}

public void setPerson(Person person) {
    this.person = person;
}


}

Thanks, Dave

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-    
 facesconfig_2_0.xsd">
 <managed-bean>
   <managed-bean-name>tableBean</managed-bean-name>
   <managed-bean-class>com.dave.test.TableBean</managed-bean-class>
   <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
</faces-config>

Best Answer

I'm assuming that when you click the row, there is no data. That is because you are using a request scoped bean. This means that when you load the page, the bean is populated. After the page is loaded, the bean is gone.

I would suggest changing your scope to ViewScope to see if that helps at all.

Also, if you're using jsf 2.0, you can use annotations instead of the faces-config.xml file. Your backer would look like this:

package com.dave.test;

import java.util.ArrayList;
import java.util.List;

public class TableBean {

private List<Person> persons = null;
private Person person;

@ManagedBean
@ViewScoped
public TableBean() {
    persons = new ArrayList<Person>();
    persons.add(new Person("Jimmy", "18 Maple", "337-278-1019"));
    persons.add(new Person("Sally", "47 Oak", "787-509-3819"));
    persons.add(new Person("Roger", "754 Fifth Ave.", "926-420-8219"));
    persons.add(new Person("Mimi", "891 2nd St.", "713-371-8632"));

}

public List<Person> getPersons() {
    return persons;
}

public void setPersons(List<Person> persons) {
    this.persons = persons;
}

public Person getPerson() {
    return person;
}

public void setPerson(Person person) {
    this.person = person;
}


}

Now you can remove your managed bean stuff from faces-config.xml.

EDIT I just realized you don't have an ajax event to handle the row selection. If you're looking at the primefaces instant row selection, you need to notice that they are using <p:ajax event="rowSelect" ..../> along with a method in the backing bean to handle this.

Related Topic