简体   繁体   English

Primefaces数据表中的自定义过滤器

[英]Primefaces custom filter in datatable

Is it possible to write a custom filter for a datatable? 是否可以为数据表编写自定义过滤器?

I want to filter the data regarding the property status_flag. 我想过滤有关属性status_flag的数据。 This status_flag can have following values: available, enable, disabled. 此status_flag可以具有以下值:available,enable,disabled。

I need a filter method which shows me either the total list or the total list without the disabled. 我需要一个过滤方法,向我显示总列表或没有禁用的总列表。

For Primefaces 5, there is a new attribute filterFunction that makes possible to define custom filter in Java code: http://blog.primefaces.org/?p=3084 对于Primefaces 5,有一个新的属性filterFunction可以在Java代码中定义自定义过滤器: http ://blog.primefaces.org/?p = 3084

However, filter input is still a string in input text. 但是,过滤器输入仍然是输入文本中的字符串。

If you need a custom component to input filter values, or you are stuck with Primefaces 4 (as I am on a recent project), I will describe what worked for me. 如果您需要一个自定义组件来输入过滤器值,或者您仍然坚持使用Primefaces 4(就像我在最近的项目中那样),我将描述对我有用的内容。

I extended filtering behavior using these key steps 我使用这些关键步骤扩展了过滤行为

  • put a normal JSF input component into header facet of a column instead of using filterBy attribute 将一个普通的JSF输入组件放入列的标题面,而不是使用filterBy属性
  • attach a javascript callback to this component triggered on user input, which calls PF('dataTableWidgetVar').filter() 将javascript回调附加到在用户输入上触发的此组件,该组件调用PF('dataTableWidgetVar').filter()
  • add filteredValue attribute to the dataTable, which applies a custom filter in Java setter on top of existing filter 将filteredValue属性添加到dataTable,它将Java setter中的自定义过滤器应用于现有过滤器之上

Key thing is to take advantage of filteredValue attribute - when Primefaces filter() function is called or when primefaces filters change, filteredValue is set to list filtered values (or null if no filter applied). 关键是要利用filteredValue属性 - 当调用Primefaces filter()函数或者当primefaces过滤器更改时,filteredValue被设置为列出过滤值(如果没有应用过滤器,则为null)。 Then Primefaces reads filteredValues back from getter to update list of items in dataTable. 然后Primefaces从getter读取filteredValues以更新dataTable中的项目列表。 If we put our filter in between these calls (either in getter or setter, setter is more efficient as it is called only when filters change), we modify original filtered list with our filter and return it back through the getter. 如果我们将过滤器置于这些调用之间(在getter或setter中,setter效率更高,因为只有在过滤器更改时才调用),我们使用过滤器修改原始过滤列表并通过getter返回。

Some code: 一些代码:

Definition of datatable with inputText as filter component: 使用inputText作为过滤器组件定义数据表:

<p:dataTable filteredValue="#{view.filteredResults} >
    ...
    <p:columnGroup type="header">
    ...
        <p:row>
    ...
           <p:column>
               <f:facet name="header">
                    <p:inputText value="#{view.filterValue}" />
                </f:facet>
           </p:column>

    ...
</p:dataTable>

Java Setter and Getter of filteredResults in view named view: 在视图命名视图中的filteredSetter和getter of filteredResults:

public void setFilteredResults(List<?> filteredResults) {
    this.filteredResults = applyPremiumFilters(filteredResults, filterValue);
}

public List<?> getFilteredResults() {
    return this.filteredResults;
}

The rest is Javascript code to apply filter on dataTable when value in filter component is changed. 其余是Javascript代码,当过滤器组件中的值更改时,将对dataTable应用过滤器。

Of course you can, 当然可以,

I will give you an example below: 我将在下面给你一个例子:

<p:column filterBy="status"    
          filterOptions="#{yourBean.statusOptions}"  
          filterMatchMode="exact">  
...
</p:column>

The Java code: Java代码:

public List<SelectItem> getStatusOptions()
{  
    List<SelectItem> options = new ArrayList<SelectItem>();  

    options.add(new SelectItem("avalaible", "Avalaible"));
    options.add(new SelectItem("enable",    "Enable")); 
    options.add(new SelectItem("disabled",  "Disabled"));     

    return options;  
}  

Using SelectItem . 使用SelectItem

You will find an example here http://www.primefaces.org/showcase/ui/datatableFiltering.jsf 你会在这里找到一个例子http://www.primefaces.org/showcase/ui/datatableFiltering.jsf

Hope it will help... 希望它会有所帮助......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM