-1

I am trying to implement row selection for a datatable with primefaces, but when I click on a row, nothing displays. I've narrowed it down to the setter for the selected car is not getting called, but I do not know how to set this. Here's my code, can anybody help?

TableBean

import java.io.Serializable;  
import java.util.ArrayList;  
import java.util.List;  
import java.util.UUID;  
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean(name="TableBean")
@ViewScoped
public class ViewCDeployments implements Serializable {
    private static final long serialVersionUID = 2213388781051004157L;

    private final static String[] colors;  

    private final static String[] manufacturers;  

    static {  
        colors = new String[10];  
        colors[0] = "Black";  
        colors[1] = "White";  
        colors[2] = "Green";  
        colors[3] = "Red";  
        colors[4] = "Blue";  
        colors[5] = "Orange";  
        colors[6] = "Silver";  
        colors[7] = "Yellow";  
        colors[8] = "Brown";  
        colors[9] = "Maroon";  

        manufacturers = new String[10];  
        manufacturers[0] = "Mercedes";  
        manufacturers[1] = "BMW";  
        manufacturers[2] = "Volvo";  
        manufacturers[3] = "Audi";  
        manufacturers[4] = "Renault";  
        manufacturers[5] = "Opel";  
        manufacturers[6] = "Volkswagen";  
        manufacturers[7] = "Chrysler";  
        manufacturers[8] = "Ferrari";  
        manufacturers[9] = "Ford";  
    }  

    private List<Car> cars;  

    private Car selectedCar;  

    private Car[] selectedCars;  

    private CarDataModel mediumCarsModel;  

    public ViewCDeployments() {  
        cars = new ArrayList<Car>();  

        populateRandomCars(cars, 50);  

        mediumCarsModel = new CarDataModel(cars);  
    }  

    public Car[] getSelectedCars() {  
        return selectedCars;  
    }  
    public void setSelectedCars(Car[] selectedCars) {  
        this.selectedCars = selectedCars;  
    }  

    public Car getSelectedCar() {  
        return selectedCar;  
    }  

    public void setSelectedCar(Car selectedCar) {  
        this.selectedCar = selectedCar;  
    }  

    private void populateRandomCars(List<Car> list, int size) {  
        for(int i = 0 ; i < size ; i++)  
            list.add(new Car(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomColor()));  
    }  

    private int getRandomYear() {  
        return (int) (Math.random() * 50 + 1960);  
    }  

    private String getRandomColor() {  
        return colors[(int) (Math.random() * 10)];  
    }  

    private String getRandomManufacturer() {  
        return manufacturers[(int) (Math.random() * 10)];  
    }  

    private String getRandomModel() {  
        return UUID.randomUUID().toString().substring(0, 8);  
    }  

    public CarDataModel getMediumCarsModel() {  
        return mediumCarsModel;  
    }  

    public List<Car> getCars() {
        return cars;
    }
}

Car.java

import java.io.Serializable;

public class Car implements Serializable {

        /**
     * 
     */
    private static final long serialVersionUID = 240545116337689611L;
        private String model;
        private int year;
        private String manufacturer;
        private String color;
    private int price;

    public Car(String model, int year, String manufacturer, String color) {
                this.model = model;
                this.year = year;
                this.manufacturer = manufacturer;
                this.color = color;
        }

        public Car(String model, int year, String manufacturer, String color, int price) {
                this.model = model;
                this.year = year;
                this.manufacturer = manufacturer;
                this.color = color;
        this.price = price;
        }

        public String getModel() {
                return model;
        }

        public void setModel(String model) {
                this.model = model;
        }

        public int getYear() {
                return year;
        }

        public void setYear(int year) {
                this.year = year;
        }

        public String getManufacturer() {
                return manufacturer;
        }

        public void setManufacturer(String manufacturer) {
                this.manufacturer = manufacturer;
        }

        public String getColor() {
                return color;
        }

        public void setColor(String color) {
                this.color = color;
        }

     public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

        @Override
        public boolean equals(Object obj) {
                if(obj == null)
                        return false;

                if(!(obj instanceof Car))
                        return false;

                Car compare = (Car) obj;

                return compare.model.equals(this.model);
        }

        @Override
        public int hashCode() {
                int hash = 1;

            return hash * 31 + model.hashCode();
        }

    @Override
    public String toString() {
        return "Car{" + "model=" + model + ", year=" + year + ", manufacturer=" + manufacturer + ", color=" + color + ", price=" + price + '}';
    }
}

CarDataModel

import java.io.Serializable;
import java.util.List;
import javax.faces.model.ListDataModel;
import org.primefaces.model.SelectableDataModel;

public class CarDataModel extends ListDataModel<Car> implements SelectableDataModel<Car>, Serializable {  

    /**
     * 
     */
    private static final long serialVersionUID = -5147159758418722534L;

    public CarDataModel() {
    }

    public CarDataModel(List<Car> data) {
        super(data);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Car getRowData(String rowKey) {
        //In a real app, a more efficient way like a query by rowKey should be implemented to deal with huge data

        List<Car> cars = (List<Car>) getWrappedData();

        for(Car car : cars) {
            if(car.getModel().equals(rowKey))
                return car;
        }

        return null;
    }

    @Override
    public Object getRowKey(Car car) {
        return car.getModel();
    }
}

XHTML

    <ui:composition 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.org/ui"
    xmlns:ui="http://java.sun.com/jsf/facelets">

    <h:form id="form">

            <p:dataTable id="cars" var="car" value="#{TableBean.mediumCarsModel}" paginator="true" rows="10"  
                 selection="#{TableBean.selectedCar}"> 

                <f:facet name="header">
                    RadioButton Based Selection  
                </f:facet>

                <p:column selectionMode="single" style="width:18px" />

                <p:column headerText="Model">
                    #{car.model}  
                </p:column>

                <p:column headerText="Year">
                    #{car.year}  
                </p:column>

                <p:column headerText="Manufacturer" >
                    #{car.manufacturer}  
                </p:column>  

                <p:column headerText="Color">
                    #{car.color}  
                </p:column>

                <f:facet name="footer">
                    <p:commandButton id="viewCommand" value="View" icon="ui-icon-search"  
                                     update=":frmContent:form:displaySingle" oncomplete="singleCarDialog.show()"/>  
                </f:facet>  
            </p:dataTable>   

            <p:dialog id="dialog" header="Car Detail" widgetVar="singleCarDialog" resizable="false"  
                      showEffect="fade" hideEffect="explode">  

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

                    <f:facet name="header">  
                        Header 
                    </f:facet>  

                    <h:outputText value="Model:" />  
                    <h:outputText value="#{TableBean.selectedCar.model}" />  

                    <h:outputText value="Year:" />  
                    <h:outputText value="#{TableBean.selectedCar.year}" />  

                    <h:outputText value="Manufacturer:" />  
                    <h:outputText value="#{TableBean.selectedCar.manufacturer}" />  

                    <h:outputText value="Color:" />  
                    <h:outputText value="#{TableBean.selectedCar.color}" />  
                </h:panelGrid>  
            </p:dialog>    

        </h:form>


</ui:composition> 
Nelson.b.austin
  • 3,080
  • 6
  • 37
  • 63

4 Answers4

1

Just add to your datatable rowKey. Without this property selection won't work.

        <p:dataTable id="cars" var="car" value="#{TableBean.mediumCarsModel}" paginator="true" rows="10" rowKey="#{car.manufacturer}" selection="#{TableBean.selectedCar}"> 

The rowKey must be the unique in the list of values.

Ro Man
  • 48
  • 5
0

I think you have added too much code.

See the example below :

test.xhml

 <?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:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:p="http://primefaces.org/ui"
>
    <h:head>
        <title>PrimeFaces AJAX Enabled SelectOneMenu</title>
        <style>
    .ui-widget,.ui-widget .ui-widget {
        font-size: 90% !important;
    }
    </style>

    </h:head>
    <h:body>
        <h:body>

            <h:form id="form">
                <p:dataTable id="cars" var="car" value="#{testBean.cars}">

                    <p:column headerText="Model" style="width:24%">
                        <h:outputText value="#{car.model}" />
                    </p:column>

                    <p:column headerText="Year" style="width:24%">
                        <h:outputText value="#{car.year}" />
                    </p:column>

                    <p:column headerText="Manufacturer" style="width:24%">
                        <h:outputText value="#{car.manufacturer}" />
                    </p:column>

                    <p:column headerText="Color" style="width:24%">
                        <h:outputText value="#{car.color}" />
                    </p:column>

                    <p:column style="width:4%">
                        <p:commandButton id="selectButton" update=":form:display"
                            oncomplete="carDialog.show()" icon="ui-icon-search" title="View">
                            <f:setPropertyActionListener value="#{car}"
                                target="#{testBean.selectedCar}" />
                        </p:commandButton>
                    </p:column>

                </p:dataTable>
                <p:dialog header="Car Detail" widgetVar="carDialog" resizable="false"
                    id="carDlg" showEffect="fade" hideEffect="explode" modal="true">

                    <h:panelGrid id="display" columns="2" cellpadding="4"
                        style="margin:0 auto;">

                        <h:outputText value="Model:" />
                        <h:outputText value="#{testBean.selectedCar.manufacturer}"
                            style="font-weight:bold" />



                    </h:panelGrid>

                </p:dialog>

            </h:form>


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

BackBean

package com.jmxwebapp.mbeans;

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

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean(name = "testBean")
@ViewScoped
public class CarBean {
    private List<Car> cars;
    private Car selectedCar;

    public CarBean() {
        cars = new ArrayList<Car>();
        cars.add(new Car("Test", 2013, "Toyo", "Blue"));
        cars.add(new Car("Test1", 2013, "Toyo", "Red"));
    }

    public List<Car> getCars() {
        return cars;
    }

    public void setCars(List<Car> cars) {
        this.cars = cars;
    }

    public Car getSelectedCar() {
        return selectedCar;
    }

    public void setSelectedCar(Car selectedCar) {
        this.selectedCar = selectedCar;
    }
}

Car

package com.jmxwebapp.mbeans;

import java.io.Serializable;

public class Car implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 240545116337689611L;
    private String model;
    private int year;
    private String manufacturer;
    private String color;
    private int price;

    public Car(String model, int year, String manufacturer, String color) {
        this.model = model;
        this.year = year;
        this.manufacturer = manufacturer;
        this.color = color;
    }

    public Car(String model, int year, String manufacturer, String color, int price) {
        this.model = model;
        this.year = year;
        this.manufacturer = manufacturer;
        this.color = color;
        this.price = price;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null)
            return false;

        if (!(obj instanceof Car))
            return false;

        Car compare = (Car) obj;

        return compare.model.equals(this.model);
    }

    @Override
    public int hashCode() {
        int hash = 1;

        return hash * 31 + model.hashCode();
    }

    @Override
    public String toString() {
        return "Car{" + "model=" + model + ", year=" + year + ", manufacturer=" + manufacturer + ", color=" + color + ", price=" + price + '}';
    }
}

**Try above it should work**
Makky
  • 17,117
  • 17
  • 63
  • 86
  • Your code is fine but you have to specifically click on the button to select a row. The component should let you select a row by clicking on any column of the row. – Roberto Linares Nov 12 '13 at 18:00
  • http://stackoverflow.com/questions/30054708/pdataexporter-selected-rows-only Can anyone help me? – Arun Raja May 06 '15 at 08:17
0

The problem is with dialog component. In your dialog component You're using the same object where store selected car object (selectedCar). Pages are renderized from top to bottom. Therefore when You make clic the value stored is selectedCar object from dialog component. This value is initialized in null firstly.

For this You should be other object in your dialog component.

Cesar Miguel
  • 661
  • 1
  • 13
  • 38
0

Notice that when you are using your beans you include them "inside" the html component, like:

<p:column headerText="Model">
    #{car.model}  
</p:column>

When you should be incluiding the bean inside the tag in the value option:

<p:column headerText="Model" value="#{car.model}>       
</p:column>

Hope it works :)

DarkCygnus
  • 7,420
  • 4
  • 36
  • 59