2013-05-21 52 views
-1

我想實現與primefaces數據表的行選擇,但是當我點擊一行時,什麼都不顯示。我已經縮小到選定的汽車沒有被調用,但我不知道如何設置這個。這是我的代碼,任何人都可以幫忙嗎?數據錶行選擇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> 

回答

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

你的代碼是好的,但你必須專門點擊按鈕來選擇一行。 組件應讓您通過單擊該行的任何列來選擇一行。 –

+0

http://stackoverflow.com/questions/30054708/pdataexporter-selected-rows-only任何人都可以幫助我嗎? –

0

問題是與對話單元。在您的對話框組件中您使用的是存儲選定汽車對象(selectedCar)的相同對象。頁面從上到下進行渲染。因此,當您製作clic時,存儲的值將從對話框組件中選中汽車對象。該值首先在null中初始化。

爲此您應該是您的對話框組件中的其他對象。

1

只需添加到您的數據表rowKey。沒有這個屬性選擇將不起作用。

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

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

+0

http://stackoverflow.com/questions/30054708/pdataexporter-selected-rows-only任何人都可以幫助我 –

0

注意,當您使用的是豆你有他們「內部」的HTML組件,如:

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

當你應在value選項incluiding,在標籤內的豆:

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

希望它的作品:)