0

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>
Dave
  • 545
  • 3
  • 14
  • 29
  • Are your managed beans being declared in `faces-config.xml` or did you forget the annotations? We need to see why the managed bean is not retaining data after a server request. Could you please include your faces-config.xml? – maple_shaft Jul 30 '12 at 19:30
  • Thanks for responding, I just added it at the end of my post. – Dave Jul 30 '12 at 20:41
  • I'm not seeing your faces-config.xml file @Dave. – Catfish Jul 30 '12 at 21:07
  • Off-topic, but you know the `
    ` tag is deprecated right?
    – Catfish Jul 30 '12 at 21:08
  • Okay, it's in there now (got lost the first time somehow). As for center, yes, but this is just a test program for Primefaces. I'll fancy it up after I get this issue set in my mind. – Dave Jul 30 '12 at 21:30
  • Ok so now what exactly is the problem? You say that you're trying to select a row to get the data, but what happens when you click the row? – Catfish Jul 31 '12 at 14:38
  • http://stackoverflow.com/questions/30054708/pdataexporter-selected-rows-only – Arun Raja May 06 '15 at 08:17

2 Answers2

0

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.

Catfish
  • 18,876
  • 54
  • 209
  • 353
  • Thanks, but I did all that and still the same result. I'm starting to think it's something about my configuration. – Dave Jul 31 '12 at 15:18
  • But I'm darned if I can figure out what. – Dave Jul 31 '12 at 15:26
  • Thanks, I finally noticed that also. Now I'm onto a new problem. Also, one of my problems was that I used IE9 to test - WRONG. Use anything else, big compatiblity problems – Dave Aug 02 '12 at 23:59
  • @Catfish Can you help me in this issue? http://stackoverflow.com/questions/30054708/pdataexporter-selected-rows-only – Arun Raja May 06 '15 at 08:18
0

lo único que debes hacer es al bean TableBean.java quitarle(borrar) el @SessionScoped y a la clase Car.java quitarle @ManagedBean(name = "car") , @SessionScoped , implements Serializable. esta clase es una simple clase no tiene porque ser un managebean unicamente son datos.

Translated:

All you have to do is to take TableBean.java bean (delete) the @SessionScoped and take Car.java class @ManagedBean (name = "car"), @SessionScoped, implements Serializable. This class is a simple class need not be a managebean only are data.

competent_tech
  • 44,465
  • 11
  • 90
  • 113