I made this StackBlitz with the modifications needed from my previous example on ng-grid filters.
You need to redefine the function doesFilterPass(...)
wich is executed once per row in the table to match your desired filter:
customFilter(filterWord: string, node: RowNode): boolean {
let data = [
{ key: 'Smith', tokens: ['Smith', 'ael', '+0912312321'] },
{ key: 'Robert', tokens: ['Robert', 'rob', '457891187'] }
];
let rowData = null;
data.forEach((record => {
if (record.key === this.valueGetter(node).toString()) {
rowData = record;
}
}));
return rowData.tokens.some((token) => {
return token.toLowerCase().indexOf(filterWord) >= 0;
});
}
doesFilterPass(params: IDoesFilterPassParams): boolean {
return this.text.toLowerCase()
.split(" ")
.every((filterWord) => {
return this.customFilter(filterWord, params.node);
});
}
As you can see the function retrieves all search tokens (separated by spaces) and match every one using the custom filter wich is delegated to the function customFilter(...)
. Note that the argument node
contains the current value of the given row.
The filter function then retrieves the data that will be used in the filter for the current row and tries to match the filter word with any of the tokens.
Note that the key
property in each object inside the data array must match the name of the column defined in app.component.ts
:
rowData = [
{ name: 'Smith' },
{ name: 'Robert' }
];
Ideally the data array should be provided by some service. In this way you are able to filter for each token in the tokens property. In this case the example will filter for more than one criteria at the same time, for example the input "S 091" will match Smith, you should modify the customFilter
function to restrict the filter to only one parameter each time.