问题 它是如何运作PrimeFaces DataTable的filterMatchMode的?


Primefaces Datatable允许您使用属性filterMatchMode配置用于列的过滤类型。

尽管如此,如果您使用LazyDataModel,您必须实现自己的搜索方法,该方法根本不接收该属性。此功能仅对普通DataModel有用吗?


2204
2017-07-12 19:59


起源

GitHub问题: github.com/primefaces/primefaces/issues/30 - lazlev


答案:


目前,LazyDataModel不支持此功能,但您仍然可以相对轻松地使用它。 blemasle  发布 在primefaces论坛上相应的补丁,遗憾的是开发者仍然没有回应。

如果你想使用非代码侵入式解决方案,你可以试试我的。

过滤器约束可通过以

 /**
 * @param tableExpr expression, starting from the view root,
 *        which identifies the datatable to retrieve information from
 * @return map, containing pairs of {@code <filtered field id, match mode>}.
 *         Filtered field id is evaluated from the 'filterBy' 
 *         attribute of the column in the following way:
 *         #{item.name} -> name
 *         #{item.category.name} -> category.name
 */
public Map<String, FilterMatchMode> getFiltersMatchMode(String tableSearchExpr) {
    FacesContext context = FacesContext.getCurrentInstance();
    Object component = context.getViewRoot().findComponent(tableSearchExpr);

    if (null == component) {
        throw new IllegalArgumentException(
                    "No component found for search expression: " 
                            + tableSearchExpr);
    }
    if (!(component instanceof DataTable)) {
        throw new IllegalArgumentException(
                    "Component is not a DataTable: " + tableSearchExpr);
    }

    DataTable table = (DataTable) component;
    Map<String, FilterMatchMode> constraints = 
            new HashMap<String, FilterMatchMode>(table.getColumns().size());

    for (Column column : table.getColumns()) {
        ValueExpression filterExpression = 
                  column.getValueExpression("filterBy");
        if (null != filterExpression) {
            String filterExpressionString = filterExpression.
                                                   getExpressionString();
            //evaluating filtered field id
            String filteredField = filterExpressionString.substring(
                    filterExpressionString.indexOf('.') + 1,
                    filterExpressionString.indexOf('}'));

            FilterMatchMode matchMode = 
                  FilterMatchMode.fromUiParam(column.getFilterMatchMode());

            constraints.put(filteredField, matchMode);
        }
    }

    return constraints;
}

FilterMatchMode的位置是:

public enum FilterMatchMode {

STARTS_WITH("startsWith"), ENDS_WITH("endsWith"), 
CONTAINS("contains"), EXACT("exact");

/**
 * Value of p:column's filterMatchMode attribute 
 *     which corresponds to this math mode
 */
private final String uiParam;

FilterMatchMode(String uiParam) {
    this.uiParam = uiParam;
}

/**
 * @param uiParam value of p:column's filterMatchMode attribute
 * @return MatchMode which corresponds to given UI parameter
 * @throws IllegalArgumentException if no MatchMode 
 *          is corresponding to given UI parameter
 */
public static FilterMatchMode fromUiParam(String uiParam) {
    for (FilterMatchMode matchMode : values()) {
        if (matchMode.uiParam.equals(uiParam)) {
            return matchMode;
        }
    }
    throw new IllegalArgumentException("No MatchMode found for " + uiParam);
}

}

12
2017-10-29 21:40



你把它放在哪里 getFiltersMatchMode 方法? - senyor
@senyor在你的使用中 LazyDataModel  load() 方法实现。 - Micer