2015-03-03 37 views
0

我想在數據表中有一個selectonemenu作爲過濾器選項 我已經做了幾個測試,沒有成功 現在我試圖創建一個示例自定義經過過濾,利用selectOneButton,與靜態字段,試圖簡化,但同樣它不工作總理面孔5.0:數據表中的自定義過濾器被忽略

     <p:column filterBy="#{sController.getStatus(se)}" headerText="Stato" 
           style="text-align:center;" sortBy="#{sController.getStatus(se)}" 
           filterMatchMode="exact" 
           > 
         <f:facet name="filter"> 
          <p:selectOneButton onchange="PF('sDataTableWV').filter()"> 
           <f:converter converterId="javax.faces.Boolean" /> 
           <f:selectItem itemLabel="All" itemValue="" /> 
           <f:selectItem itemLabel="Sold" itemValue="true" /> 
           <f:selectItem itemLabel="Sale" itemValue="false" /> 
          </p:selectOneButton> 
         </f:facet> 
         <img src="#{resource[sController.getStatus(se)]}" /> 
        </p:column> 

小面完全被忽略,我看到常用的輸入字段,而不是

------ --------編輯------------------ 根據要求,我準備了一個採用showca的源代碼的新頁面SE

這裏是XHTML頁面

<?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:h="http://java.sun.com/jsf/html" 
 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
 
     xmlns:p="http://primefaces.org/ui" 
 
     xmlns:f="http://java.sun.com/jsf/core"> 
 
    <h:head> 
 
     <title>Pagina di test</title> 
 
    </h:head> 
 
    <body> 
 
     <h3>Pagina di test</h3> 
 
     <h:form> 
 
      <p:dataTable var="car" value="#{dtFilterView.cars}" widgetVar="carsTable" 
 
         emptyMessage="No cars found with given criteria" filteredValue="#{dtFilterView.filteredCars}"> 
 

 
       <f:facet name="header"> 
 
        <p:outputPanel> 
 
         <h:outputText value="Search all fields:" /> 
 
         <p:inputText id="globalFilter" onkeyup="PF('carsTable').filter()" style="width:150px" placeholder="Enter keyword"/> 
 
        </p:outputPanel> 
 
       </f:facet> 
 

 
       <p:column filterBy="#{car.id}" headerText="Id" footerText="contains" filterMatchMode="contains"> 
 
        <h:outputText value="#{car.id}" /> 
 
       </p:column> 
 

 
       <p:column filterBy="#{car.year}" headerText="Year" footerText="lte" filterMatchMode="lte"> 
 
        <f:facet name="filter"> 
 
         <p:spinner onchange="PF('carsTable').filter()" styleClass="year-spinner"> 
 
          <f:converter converterId="javax.faces.Integer" /> 
 
         </p:spinner> 
 
        </f:facet> 
 
        <h:outputText value="#{car.year}" /> 
 
       </p:column> 
 

 
       <p:column filterBy="#{car.brand}" headerText="Brand" footerText="exact" filterMatchMode="exact"> 
 
        <f:facet name="filter"> 
 
         <p:selectOneMenu onchange="PF('carsTable').filter()" > 
 
          <f:selectItem itemLabel="Select One" itemValue="#{null}" noSelectionOption="true" /> 
 
          <f:selectItems value="#{dtFilterView.brands}" /> 
 
         </p:selectOneMenu> 
 
        </f:facet> 
 
        <h:outputText value="#{car.brand}" /> 
 
       </p:column> 
 

 
       <p:column filterBy="#{car.color}" headerText="Color" footerText="in" filterMatchMode="in"> 
 
        <f:facet name="filter"> 
 
         <p:selectCheckboxMenu label="Colors" onchange="PF('carsTable').filter()" panelStyle="width:125px" scrollHeight="150"> 
 
          <f:selectItems value="#{dtFilterView.colors}" /> 
 
         </p:selectCheckboxMenu> 
 
        </f:facet> 
 
        <h:outputText value="#{car.color}" /> 
 
       </p:column> 
 

 
       <p:column filterBy="#{car.sold}" headerText="Status" footerText="equals" filterMatchMode="equals"> 
 
        <f:facet name="filter"> 
 
         <p:selectOneButton onchange="PF('carsTable').filter()"> 
 
          <f:converter converterId="javax.faces.Boolean" /> 
 
          <f:selectItem itemLabel="All" itemValue="" /> 
 
          <f:selectItem itemLabel="Sold" itemValue="true" /> 
 
          <f:selectItem itemLabel="Sale" itemValue="false" /> 
 
         </p:selectOneButton> 
 
        </f:facet> 
 
        <h:outputText value="#{car.sold ? 'Sold': 'Sale'}" /> 
 
       </p:column> 
 

 
       <p:column filterBy="#{car.price}" headerText="Price" footerText="custom (min)" filterFunction="#{dtFilterView.filterByPrice}"> 
 
        <h:outputText value="#{car.price}"> 
 
         <f:convertNumber currencySymbol="$" type="currency"/> 
 
        </h:outputText> 
 
       </p:column> 
 
      </p:dataTable> 
 
     </h:form> 
 

 
    </body> 
 
</html>

這裏Car.java

package controllers.test; 



import java.io.Serializable; 

public class Car implements Serializable { 

public String id; 
public String brand; 
public int year; 
public String color; 
public int price; 
public boolean sold; 

public Car() {} 

public Car(String id, String brand, int year, String color) { 
    this.id = id; 
    this.brand = brand; 
    this.year = year; 
    this.color = color; 
} 

public Car(String id, String brand, int year, String color, int price, boolean sold) { 
    this.id = id; 
    this.brand = brand; 
    this.year = year; 
    this.color = color; 
    this.price = price; 
    this.sold = sold; 
} 

public String getId() { 
    return id; 
} 
public void setId(String id) { 
    this.id = id; 
} 

public String getBrand() { 
    return brand; 
} 
public void setBrand(String brand) { 
    this.brand = brand; 
} 

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

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

public boolean isSold() { 
    return sold; 
} 
public void setSold(boolean sold) { 
    this.sold = sold; 
} 

@Override 
public int hashCode() { 
    int hash = 7; 
    hash = 59 * hash + (this.id != null ? this.id.hashCode() : 0); 
    return hash; 
} 

@Override 
public boolean equals(Object obj) { 
    if (obj == null) { 
     return false; 
    } 
    if (getClass() != obj.getClass()) { 
     return false; 
    } 
    final Car other = (Car) obj; 
    if ((this.id == null) ? (other.id != null) : !this.id.equals(other.id)) { 
     return false; 
    } 
    return true; 
} 
} 

然後CarService.java

import java.util.ArrayList; 

import java.util.Arrays; 

import java.util.List; 

import java.util.UUID; 

import javax.faces.bean.ApplicationScoped; 

import javax.faces.bean.ManagedBean; 


@ManagedBean(name = "carService") 

@ApplicationScoped 

public class CarService { 

private final static String[] colors; 

private final static String[] brands; 

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

    brands = new String[10]; 
    brands[0] = "BMW"; 
    brands[1] = "Mercedes"; 
    brands[2] = "Volvo"; 
    brands[3] = "Audi"; 
    brands[4] = "Renault"; 
    brands[5] = "Fiat"; 
    brands[6] = "Volkswagen"; 
    brands[7] = "Honda"; 
    brands[8] = "Jaguar"; 
    brands[9] = "Ford"; 
} 

public List<Car> createCars(int size) { 
    List<Car> list = new ArrayList<Car>(); 
    for(int i = 0 ; i < size ; i++) { 
     list.add(new Car(getRandomId(), getRandomBrand(), getRandomYear(), getRandomColor(), getRandomPrice(), getRandomSoldState())); 
    } 

    return list; 
} 

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

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

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

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

public int getRandomPrice() { 
    return (int) (Math.random() * 100000); 
} 

public boolean getRandomSoldState() { 
    return (Math.random() > 0.5) ? true: false; 
} 

public List<String> getColors() { 
    return Arrays.asList(colors); 
} 

public List<String> getBrands() { 
    return Arrays.asList(brands); 
} 
} 

和最後FilterView.java

import java.io.Serializable; 

import java.util.List; 

import java.util.Locale; 

import javax.annotation.PostConstruct; 

import javax.faces.bean.ManagedBean; 

import javax.faces.bean.ManagedProperty; 

import javax.faces.bean.ViewScoped; 


@ManagedBean(name="dtFilterView") 

@ViewScoped 

public class FilterView implements Serializable { 

private List<Car> cars; 

private List<Car> filteredCars; 

@ManagedProperty("#{carService}") 
private CarService service; 

@PostConstruct 
public void init() { 
    cars = service.createCars(10); 
} 

public boolean filterByPrice(Object value, Object filter, Locale locale) { 
    String filterText = (filter == null) ? null : filter.toString().trim(); 
    if(filterText == null||filterText.equals("")) { 
     return true; 
    } 

    if(value == null) { 
     return false; 
    } 

    return ((Comparable) value).compareTo(Integer.valueOf(filterText)) > 0; 
} 

public List<String> getBrands() { 
    return service.getBrands(); 
} 

public List<String> getColors() { 
    return service.getColors(); 
} 

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

public List<Car> getFilteredCars() { 
    return filteredCars; 
} 

public void setFilteredCars(List<Car> filteredCars) { 
    this.filteredCars = filteredCars; 
} 

public void setService(CarService service) { 
    this.service = service; 
} 

} 

結果是完全一樣的,過濾器輸入字段

我在用5.0的GlassFish 3.2

+0

你好。請提供一個MCVE - http://stackoverflow.com/help/mcve – 2015-03-03 11:21:30

回答

0

對不起,我有我的開發環境,但在生產是3.5

更改庫全部工作