JSF Primefaces datatable match mode for global filter (not individual column)

14,396

Solution 1

If you look at the source code of primefaces

org.primefaces.component.datatable.feature.FilterFeature.java

At line 133 you can see primefaces uses contains method of String

if(columnValue.toLowerCase(filterLocale).contains(globalFilter)){
    globalMatch = true;
}

So for now there is no way other than changing code according to your needs and building your own primefaces jar.

Solution 2

From Primefaces 4.0 docs:

Filter located at header is a global one applying on all fields, this is implemented by calling client side API method called filter(), important part is to specify the id of the input text as globalFilter which is a reserved identifier for datatable.

The use case would be:

<p:dataTable var="car" value="#{carBean.cars}" 
filteredValue="#{carBean.filteredCars}" widgetVar="carsTable">
    <f:facet name="header">
        <p:outputPanel>
            <h:outputText value="Search all fields:" />
            <h:inputText id="globalFilter" onkeyup="PF('carsTable').filter()" />
        </p:outputPanel>
    </f:facet>
    <p:column filterBy="model" headerText="Model" filterMatchMode="contains">
        <h:outputText value="#{car.model}" />
    </p:column>
    <p:column filterBy="year" headerText="Year" footerText="startsWith">
        <h:outputText value="#{car.year}" />
    </p:column>
    <p:column filterBy="manufacturer" headerText="Manufacturer" 
filterOptions="#{carBean.manufacturerOptions}" filterMatchMode="exact">
        <h:outputText value="#{car.manufacturer}" />
    </p:column>
    <p:column filterBy="color" headerText="Color" filterMatchMode="endsWith">
        <h:outputText value="#{car.color}" />
    </p:column>
</p:dataTable>

It doesn't tell anything about startsWithor endsWith specific cases for global filter. It could be interesting to open a thread on the issue tracker.

Share:
14,396

Related videos on Youtube

Praveen
Author by

Praveen

Updated on June 04, 2022

Comments

  • Praveen
    Praveen almost 2 years

    What is the filter match mode for global filter (not the individual column filter which defaults to 'startsWith') and how to change it?

    The reason I ask is, when I use the global filter with match mode set to 'startsWith' in all my columns, still I get values with 'contains' filter mode. See screenshot below.

    country table screenshot

    I shouldn't be getting the rows other than the first row as I specified 'startsWith' in all columns.

    Here is my datatable,

    <h:form id="countryTable">
    <p:dataTable rowKey="" value="#{countryBean.countriesList}"
        var="country" selection="#{countryBean.selectedCountries}"
        styleClass="data-table-style" widgetVar="countryTableWVar"
        filteredValue="#{countryBean.filteredCountries}">
        <f:facet name="header">
            <div class="align-left">
                <p:outputPanel>
                    <h:outputText value="Search all fields:" />
                    <p:inputText id="globalFilter" onkeyup="countryTableWVar.filter();"
                        style="width:150px" />
                </p:outputPanel>
            </div>
    
        </f:facet>
        <p:column selectionMode="multiple" style="width:2%;" />
        <p:column headerText="Numeric Code" filterMatchMode="startsWith"
            filterStyle="display:none" filterBy="numericCode">
            <h:outputText value="#{country.numericCode}"></h:outputText>
        </p:column>
        <p:column headerText="Alpha_2 Code" filterMatchMode="startsWith"
            filterStyle="display:none" filterBy="alpha2">
            <h:outputText value="#{country.alpha2}"></h:outputText>
        </p:column>
        <p:column headerText="Alpha_3 Code" filterMatchMode="startsWith"
            filterStyle="display:none" filterBy="alpha3">
            <h:outputText value="#{country.alpha3}"></h:outputText>
        </p:column>
        <p:column headerText="Name" filterMatchMode="startsWith"
            filterStyle="display:none" filterBy="name">
            <h:outputText value="#{country.name}"></h:outputText>
        </p:column>
    </p:dataTable>
    </h:form>
    

    How to change the datatable global filter match mode?