【问题标题】:Datatable Row Selection Primefaces数据表行选择 Primefaces
【发布时间】:2013-05-21 17:46:57
【问题描述】:

我正在尝试为带有 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 + '}';
    }
}

汽车数据模型

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> 

【问题讨论】:

    标签: jsf primefaces datatable row


    【解决方案1】:

    只需添加到您的数据表 rowKey。没有此属性选择将不起作用。

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

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

    【讨论】:

    【解决方案2】:

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

    请看下面的例子:

    test.xml

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

    【讨论】:

    【解决方案3】:

    问题出在对话框组件上。在您的对话框组件中,您使用的是存储选定汽车对象 (selectedCar) 的相同对象。页面从上到下呈现。因此,当您进行 clic 时,存储的值是来自对话框组件的 selectedCar 对象。这个值先初始化为null。

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

    【讨论】:

      【解决方案4】:

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

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

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

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

      希望它有效:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-24
        • 2015-07-10
        • 2013-05-31
        • 1970-01-01
        • 2012-09-08
        • 2014-03-19
        • 2012-11-09
        • 2015-07-06
        相关资源
        最近更新 更多