开发者

How it works the filterMatchMode from PrimeFaces DataTable?

开发者 https://www.devze.com 2023-03-19 14:28 出处:网络
Primefaces Datatable let you configure the filtering type you use for a column, using the property filter开发者_开发问答MatchMode.

Primefaces Datatable let you configure the filtering type you use for a column, using the property filter开发者_开发问答MatchMode.

Nonetheless, if you use LazyDataModel you have to implement your own search method, which doesn't receive that property at all. is this feature only useful for normal DataModels?


Currently this feature is not supported for LazyDataModel out-of-box, however you can still use it with relatively little effort. blemasle posted corresponding patch on primefaces forum, unfortunately there is still no response from the developers.

If you want to use non code-invasive solution, you can try mine.

Filter constraints are obtained with:

 /**
 * @param tableSearchExpr 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 (UIColumn 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;
}

Where FilterMatchMode is:

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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消