数据行选择Primefaces

时间:2013-05-21 17:46:58

标签: jsf primefaces datatable row

我正在尝试为具有primefaces的数据表实现行选择,但是当我单击某行时,没有任何内容显示。我把它缩小到所选汽车的制定者没有被调用,但我不知道如何设置它。这是我的代码,有人可以帮忙吗?

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> 

4 个答案:

答案 0 :(得分:1)

只需添加到数据表rowKey即可。如果没有这个属性选择将无法工作。

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

rowKey必须是值列表中的唯一。

答案 1 :(得分:0)

我认为你添加了太多代码。

请参阅以下示例:

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;
    }
}

汽车

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**

答案 2 :(得分:0)

问题在于对话框组件。在对话框组件中,您正在使用存储所选车辆对象(selectedCar)的同一对象。页面从上到下重新定义。因此,当您制作clic时,存储的值是来自对话框组件的selectedCar对象。该值首先以null初始化。

为此你应该是对话框组件中的其他对象。

答案 3 :(得分:0)

请注意,当您使用bean时,将它们包含在html组件的“内部”中,例如:

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

当你应该在value选项中的标签内包含bean:

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

希望它有效:)