Prime面5.0:忽略数据表中的自定义过滤器

时间:2015-03-03 09:27:02

标签: primefaces

我正在尝试在数据表中使用selectonemenu作为过滤器选项 我做了几次测试,没有成功 现在我正在尝试使用带有静态字段的selectOneButton创建一个示例自定义过滤器,尝试简化,但同样不起作用

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

完全忽略了facet,我看到了通常的输入字段

--------------编辑------------------ 根据要求,我准备了一个新页面,其中包含展示的源代码

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

}

结果完全相同,过滤器是输入字段

我正在使用glassfish 3.2

1 个答案:

答案 0 :(得分:0)

抱歉,我的开发环境为5.0,但生产时为3.5

更改库全部工作