FilterService is a helper utility to filter collections against constraints.
+A custom equals filter that checks for exact case sensitive value is registered and defined as a match mode of a column filter.
+ +
+import {FilterService} from 'primevue/api';
+
+
+
+ Filters are accessed with FilterService.filters.
+
+const value = 'PrimeNG';
+
+FilterService.filters.equals(value, 'NG'); //false
+FilterService.filters.equals(value, 8); //false
+FilterService.filters.equals(value, new Date()); //false
+FilterService.filters.contains(value, 'NG'); //true
+FilterService.filters.startsWith(value, 'NG'); //false
+FilterService.filters.endsWith(value, 'NG'); //true
+FilterService.filters.lt(10, 20); //true
+FilterService.filters.gt(50, 20); //true
+FilterService.filters.in(value, ['PrimeFaces', 'PrimeNG']); //true
+
+
+
+ FilterService can be extended by adding new constraints using the register function.
+
+FilterService.register('isPrimeNumber', (value, filter): boolean => {
+ if (filter === undefined || filter === null || filter.trim() === '') {
+ return true;
+ }
+
+ if (value === undefined || value === null) {
+ return false;
+ }
+
+ return value.toString() === filter.toString();
+});
+
+FilterService.filters['isPrimeNumber'](3); //true
+FilterService.filters['isPrimeNumber'](5); //true
+FilterService.filters['isPrimeNumber'](568985673); //false
+
+
+
+ Name | +Parameters | +Description | +
---|---|---|
startsWith | +value: Value to filter + filter: Filter value + filterLocale: Locale to use in filtering |
+ Whether the value starts with the filter value | +
contains | +value: Value to filter + filter: Filter value + filterLocale: Locale to use in filtering |
+ Whether the value contains the filter value | +
endsWith | +value: Value to filter + filter: Filter value + filterLocale: Locale to use in filtering |
+ Whether the value ends with the filter value | +
equals | +value: Value to filter + filter: Filter value + filterLocale: Locale to use in filtering |
+ Whether the value equals the filter value | +
notEquals | +value: Value to filter + filter: Filter value + filterLocale: Locale to use in filtering |
+ Whether the value does not equal the filter value | +
in | +value: Value to filter + filter[]: An array of filter values + filterLocale: Locale to use in filtering |
+ Whether the value contains the filter value | +
lt | +value: Value to filter + filter: Filter value + filterLocale: Locale to use in filtering |
+ Whether the value is less than the filter value | +
lte | +value: Value to filter + filter: Filter value + filterLocale: Locale to use in filtering |
+ Whether the value is less than or equals to the filter value | +
gt | +value: Value to filter + filter: Filter value + filterLocale: Locale to use in filtering |
+ Whether the value is greater than the filter value | +
gte | +value: Value to filter + filter: Filter value + filterLocale: Locale to use in filtering |
+ Whether the value is greater than or equals to the filter value | +
is | +value: Value to filter + filter: Filter value + filterLocale: Locale to use in filtering |
+ Whether the value equals the filter value, alias to equals | +
isNot | +value: Value to filter + filter: Filter value + filterLocale: Locale to use in filtering |
+ Whether the value does not equal the filter value, alias to notEquals. | +
before | +value: Value to filter + filter: Filter value + filterLocale: Locale to use in filtering |
+ Whether the date value is before the filter date. | +
after | +value: Value to filter + filter: Filter value + filterLocale: Locale to use in filtering |
+ Whether the date value is after the filter date. | +
Name | +Parameters | +Description | +
---|---|---|
filter | +value[]: An array of values to filter + fields[]: An array of properties in the value object + filterValue: Filter value + filterMatchMode: Constraint + filterLocale: Locale to use in filtering |
+ Whether the property values of the given value collection matches the filter. | +
filters | +- | +Property to access constraints collection. | +
register | +name: string + fn: Filter callback |
+ Registers a new constraint in filters. | +
None.
+ +
+<DataTable :value="customers" :paginator="true" class="p-datatable-customers" :rows="10"
+ dataKey="id" v-model:filters="filters" filterDisplay="row" :loading="loading">
+ <template #empty>
+ No customers found.
+ </template>
+ <template #loading>
+ Loading customers data. Please wait.
+ </template>
+ <Column field="name" header="Name" :filterMatchModeOptions="matchModeOptions">
+ <template #body="{data}">
+ {{data.name}}
+ </template>
+ <template #filter="{filterModel,filterCallback}">
+ <InputText type="text" v-model="filterModel.value" @input="filterCallback()" class="p-column-filter" :placeholder="`Search by name - ${filterModel.matchMode}`"/>
+ </template>
+ </Column>
+ <Column header="Country" filterField="country.name" :filterMatchModeOptions="matchModeOptions">
+ <template #body="{data}">
+ <img src="../../assets/images/flag_placeholder.png" :class="'flag flag-' + data.country.code" width="30" />
+ <span class="image-text">{{data.country.name}}</span>
+ </template>
+ <template #filter="{filterModel,filterCallback}">
+ <InputText type="text" v-model="filterModel.value" @input="filterCallback()" class="p-column-filter" :placeholder="`Search by country - ${filterModel.matchMode}`" />
+ </template>
+ </Column>
+</DataTable>
+
+
+
+
+import {FilterMatchMode,FilterService} from 'primevue/api';
+import CustomerService from '../../service/CustomerService';
+
+const YOUR_FILTER = 'YOUR FILTER';
+
+export default {
+ data() {
+ return {
+ customers: null,
+ filters: {
+ 'name': {value: null, matchMode: YOUR_FILTER},
+ 'country.name': {value: null, matchMode: FilterMatchMode.STARTS_WITH}
+ },
+ matchModeOptions: [
+ {label: 'Your Equals', value: YOUR_FILTER},
+ {label: 'Starts With', value: FilterMatchMode.STARTS_WITH}
+ ],
+ loading: true
+ }
+ },
+ created() {
+ this.customerService = new CustomerService();
+ },
+ mounted() {
+ this.customerService.getCustomersLarge().then(data => {
+ this.customers = data;
+ this.loading = false;
+ });
+
+ FilterService.register(YOUR_FILTER, (value, filter) => {
+ if (filter === undefined || filter === null || filter.trim() === '') {
+ return true;
+ }
+
+ if (value === undefined || value === null) {
+ return false;
+ }
+
+ return value.toString() === filter.toString();
+ });
+ }
+}
+
+
+