问题
Example if i search [App] i don't want to see [Apple, Appliance, Appliances], instead i want to only see [App] in the search
@Pipe({
name: 'filter'
})
export class FilterPipe implements PipeTransform {
transform(items: any, filter: any, isAnd: bool): any {
if (filter && Array.isArray(items)) {
let filterKeys = Object.keys(filter);
if (isAnd) {
return items.filter(item =>
filterKeys.reduce((memo, keyName) =>
(memo && new RegExp(filter[keyName], 'gi').test(item[keyName])) || filter[keyName] === "", true));
} else {
return items.filter(item => {
return filterKeys.some((keyName) => {
console.log(keyName);
return new RegExp(filter[keyName], 'gi').test(item[keyName]) || filter[keyName] === "";
});
});
}
} else {
return items;
}
}
}
回答1:
Instead of doing new RegExp(filter[keyName], 'gi').test(item[keyName]))
, you will need to do filter[keyName] === item[keyName]
. I hope this helps
回答2:
I think with this code is success
export class FilterPipe implements PipeTransform {
transform(items: any, filter: any, isAnd: bool): any {
if (filter && Array.isArray(items)) {
return filter.map(filterItem => items.filter(itemItem => itemItem === filterItem).join())
} else {
return items;
}
}
}
来源:https://stackoverflow.com/questions/57700936/how-to-get-exact-word-using-angular-filter-pipe-and-not-words-matching-the-value