richfaces外部过滤:不执行filterMethod

时间:2014-12-15 16:25:25

标签: java ejb richfaces

我有一个带有richfaces 3.3的过滤器,它不执行filterMethod中给出的方法。

过滤器:

         <r:column filterMethod="#{orderSearchFilter.filterStateText}" >
             <f:facet name="header">
                <h:selectOneMenu value="#{orderSearchFilter.filterValueStateText}">
                    <f:selectItems value="#{orderSearchFilter.filterListStateTexts}" />
                    <a:support event="onchange" reRender="orders,dataScroller_foot" />
                </h:selectOneMenu>
            </f:facet>
            <h:outputText value="#{_data.status}"/>
         </r:column> 

过滤Bean:

import java.util.ArrayList;

import javax.faces.model.SelectItem;

import org.jboss.seam.annotations.Name;

import ch.ildsoftware.gvs.order.OrderItem;


@Name("orderSearchFilter")
public class OrderSearchFilter {

private ArrayList<SelectItem> filterListStateTexts = new ArrayList<SelectItem>();
private String filterValueInventoryNr="";
private String filterValueSerialNr="";
private SelectionStateText filterValueStateText;


public OrderSearchFilter() {
    for(SelectionStateText sT : SelectionStateText.values()) {
        SelectItem select = new SelectItem();
        select.setLabel(sT.label());
        select.setValue(sT);
        filterListStateTexts.add(select);
    }
}

public ArrayList<SelectItem> getFilterListStateTexts() {
    return filterListStateTexts;
}

public String getFilterValueInventoryNr() {
    return filterValueInventoryNr;
}

public void setFilterValueInventoryNr(String filterValueInventoryNr) {
    this.filterValueInventoryNr = filterValueInventoryNr;
}

public String getFilterValueSerialNr() {
    return filterValueSerialNr;
}

public void setFilterValueSerialNr(String filterValueSerialNr) {
    this.filterValueSerialNr = filterValueSerialNr;
}

public SelectionStateText getFilterValueStateText() {
    return filterValueStateText;
}

public void setFilterValueStateText(SelectionStateText filterValueStateText) {
    this.filterValueStateText = filterValueStateText;
}

public Boolean filterStateText(Object current) {
    OrderItem item = (OrderItem) current;
    SelectionStateText state = getFilterValueStateText();
    for(String status : state.states()) {
        if (status.isEmpty() || status.equals(item.getStatus()))
            return true; 
    }
    return false;
}

public Boolean getFilterStateText(Object current) {
    OrderItem item = (OrderItem) current;
    SelectionStateText state = getFilterValueStateText();
    for(String status : state.states()) {
        if (status.isEmpty() || status.equals(item.getStatus()))
            return true; 
    }
    return false;
}
}

我找到了不同的实现,这就是为什么有两次写入的方法,一次是get前缀。现在我基本上遵循以下示例:http://livedemo.exadel.com/richfaces-demo/richfaces/filteringFeature.jsf?tab=ex-usage&cid=4110192

在调试器中,我可以看到,过滤器值已设置(setFilterValueStateText),但都没有调用过滤器方法。

我只能看到它跳转到另一个类的modify()方法,其中filterMethod后面的字符串弹出为FilterField(不是ExtendedFilterField),但是过滤bean的当前值无法访问。在这个类中,处理其他过滤器:

public class OrderSearchDataModel extends QueryDataModel implements Modifiable 
{

private OrderSearchCriteria orderSearchCriteria;

public OrderSearchDataModel(OrderSearch query)
{
    super(query);
}

@Override
public void modify(List<FilterField> filterFields,
        List<SortField2> sortFields)
{   
    if (filterFields != null && !filterFields.isEmpty()) {

        ExtendedFilterField extendedFilterField = null;
        String value = null;
        Expression expression = null;
        String expressionStr = null;
        orderSearchCriteria = new OrderSearchCriteria();

        for (FilterField filterField : filterFields) {
            if (filterField instanceof ExtendedFilterField) {
                extendedFilterField = (ExtendedFilterField) filterField;
                value = extendedFilterField.getFilterValue();
                if (value != null && !value.equals("")) {
                    expression = extendedFilterField.getExpression();
                    expressionStr = expression.getExpressionString();
                    if (!expression.isLiteralText()) {
                        expressionStr = expressionStr.
                                replaceAll("[#|$]{1}\\{.*?\\.", "").
                                replaceAll("\\}", "");
                    }
                    if (expressionStr.equals("inventoryNr"))
                        orderSearchCriteria.setInventoryNr(value);
                    if (expressionStr.equals("serialNr"))
                        orderSearchCriteria.setSerialNr(value);

                    OrderSearch orderSearch = (OrderSearch) getQuery();
                    orderSearch.setSearchCriteria(orderSearchCriteria);
                }
            }
        }
    }

    Set<SortField2> validSortFields = new HashSet<SortField2>();
    for (SortField2 sortField : sortFields)
    {
        if (!sortField.getOrdering().equals(Ordering.UNSORTED))
            validSortFields.add(sortField);
    }

    if (validSortFields.size() == 1)
    {
        SortField2 sortfield = validSortFields.iterator().next();
        ValueExpression expression = (ValueExpression) sortfield
                .getExpression();
        OrderSearchField field = (OrderSearchField) expression.getValue(FacesContext
                .getCurrentInstance().getELContext());
        boolean asc = sortfield.getOrdering().equals(Ordering.ASCENDING);
        Sorting<OrderSearchField> sorting = new Sorting<OrderSearchField>(
                field, asc);
        OrderSearch orderSearch = (OrderSearch) getQuery();
        orderSearch.setSorting(sorting);
    }
    else if(validSortFields.size() > 1) throw new FacesException("multiple column sorting is not suported");

}
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

这不是问题的真正解决方案,而是一种解决方法,这在某种程度上是可接受的。 我基本上放弃了外部过滤器并切换到内部过滤器:

我的过滤器现在看起来像这样:

         <r:column
            filterBy="#{_data.status}"
            filterValue="#{orderSearchFilter.filterValueStateText}" >
             <f:facet name="header">
                <h:selectOneMenu value="#{orderSearchFilter.filterValueStateText}">
                    <f:selectItems value="#{orderSearchFilter.filterListStateTexts}" />
                    <a:support event="onchange" ignoreDupResponses="true" requestDelay="700"
                        reRender="resultCount,orders,dataScroller_head,dataScroller_foot" />
                </h:selectOneMenu>
            </f:facet>
            <h:outputText value="#{_data.status}"/>
         </r:column> 

这里处理了(comlete类在上面可见):

                if (expressionStr.equals("inventoryNr"))
                        orderSearchCriteria.setInventoryNr(value);
                if (expressionStr.equals("serialNr"))
                        orderSearchCriteria.setSerialNr(value);
                if (expressionStr.equals("status")) {
                        SelectionStateText state = SelectionStateText.valueOf(value);
                        orderSearchCriteria.setStateTexts(state.states());
                }
                    OrderSearch orderSearch = (OrderSearch) getQuery();
                    orderSearch.setSearchCriteria(orderSearchCriteria);

现在我有一个正常运行的过滤器,只是在选择列表下面有一个不必要的字段:

richfaces data table headers with filter workaround