2011-07-12 64 views
11

Primefaces Datatable cho phép bạn cấu hình loại lọc bạn sử dụng cho một cột, sử dụng bộ lọc thuộc tínhMatchMode.Làm thế nào nó hoạt động filterMatchMode từ PrimeFaces DataTable?

Tuy nhiên, nếu bạn sử dụng LazyDataModel, bạn phải triển khai phương pháp tìm kiếm của riêng mình, phương thức này hoàn toàn không nhận được thuộc tính đó. tính năng này chỉ hữu ích cho DataModels thông thường?

+0

GitHub phát hành: https://github.com/primefaces/primefaces/issues/30 – lazlev

Trả lời

12

Hiện tại tính năng này không được hỗ trợ cho LazyDataModel out-of-box, tuy nhiên bạn vẫn có thể sử dụng nó với nỗ lực tương đối ít. blemasleposted bản vá tương ứng trên diễn đàn thủ đô, tiếc là vẫn không có phản hồi từ các nhà phát triển.

Nếu bạn muốn sử dụng giải pháp không xâm lấn mã, bạn có thể dùng thử.

chế Lọc thu được với:

/** 
* @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; 
} 

đâu FilterMatchMode là:

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

Nơi bạn đặt ' phương thức getFiltersMatchMode'? – senyor

+0

@senyor Sử dụng nó trong phương thức 'LazyDataModel'' load() 'của bạn. – Micer

Các vấn đề liên quan