我有一个要求,在我的primefaces数据表中, filterMatchMode 属性(如字段的任何部分,整个字段)需要由用户通过选择菜单选择。为此我已声明我的viewscoped托管bean中的变量为 filterCriteria ,并将filterMatchMode属性设置为
filterMatchMode = “#{beanName.filterCriteria}”
bean变量在提交时在托管bean中设置,但过滤没有发生,我收到空消息。当我从过滤器框中删除键入值时,数据表不会被恢复,否则必须恢复在正常情况下。 我使用并修改了primefaces showcase here中给出的示例代码。代码片段如下
<h:form id="tblFrm">
<h:selectOneMenu id="filterOptions"
value="#{dtFilterView.filterOption}" required="true"
requiredMessage="You must select an option!">
<f:selectItem id="hd1" itemLabel="Select" itemValue="#{null}" />
<f:selectItem id="opt1" itemLabel="Any part of field" itemValue="any" />
<f:selectItem id="opt2" itemLabel="Whole Field" itemValue="whole" />
<f:selectItem id="opt3" itemLabel="Start of field" itemValue="strt" />
</h:selectOneMenu>
<p:commandButton id="filterSb" value="GO"
action="#{dtFilterView.filterAction}" update="dtTbl" />
<p:dataTable var="car" id="dtTbl" value="#{dtFilterView.cars}"
widgetVar="carsTable"
emptyMessage="No cars found with given criteria"
filteredValue="#{dtFilterView.filteredCars}">
<p:column filterBy="#{car.id}" headerText="Id" footerText="contains"
filterMatchMode="#{dtFilterView.filterCriteria}" >
<h:outputText value="#{car.id}" />
</p:column>
。 。
在托管bean内部使用 filterAction 方法
public void filterAction()
{
if(filterOption.equalsIgnoreCase("any"))
this.filterCriteria="contains";
else
if(filterOption.equalsIgnoreCase("whole"))
this.filterCriteria="exact";
else
if(filterOption.equalsIgnoreCase("strt"))
this.filterCriteria="startsWith";
}
我在哪里错了。有没有办法实现这个功能。 在此先感谢!!!
答案 0 :(得分:0)
如果您没有忘记filterOption和filterCriteria的getter / setter,它可以正常工作。
XHTML
<h:form>
<h:selectOneMenu id="filterOptions"
value="#{dtFilterView.filterOption}" required="true"
requiredMessage="You must select an option!">
<f:selectItem id="hd1" itemLabel="Select" itemValue="#{null}" />
<f:selectItem id="opt1" itemLabel="Any part of field" itemValue="any" />
<f:selectItem id="opt2" itemLabel="Whole Field" itemValue="whole" />
<f:selectItem id="opt3" itemLabel="Start of field" itemValue="strt" />
</h:selectOneMenu>
<p:commandButton id="filterSb" value="GO"
action="#{dtFilterView.filterAction}"
update="dtTbl" />
<p:dataTable var="car"
value="#{dtFilterView.cars}"
widgetVar="carsTable"
id="dtTbl"
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="#{dtFilterView.filterCriteria}" >
<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>
managedbean
@ManagedBean(name = "dtFilterView")
@ViewScoped
public class FilterView implements Serializable {
private List<Car> cars;
private List<Car> filteredCars;
private String filterOption;
private String filterCriteria;
@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;
}
public String getFilterOption() {
return filterOption;
}
public void setFilterOption(String filterOption) {
this.filterOption = filterOption;
}
public String getFilterCriteria() {
return filterCriteria;
}
public void setFilterCriteria(String filterCriteria) {
this.filterCriteria = filterCriteria;
}
public void filterAction() {
if (filterOption.equalsIgnoreCase("any")) {
this.filterCriteria = "contains";
} else if (filterOption.equalsIgnoreCase("whole")) {
this.filterCriteria = "exact";
} else if (filterOption.equalsIgnoreCase("strt")) {
this.filterCriteria = "startsWith";
}
}
}