如何使文本过滤器不区分大小写?
How do I make text filter case-insensitive?
我当前的文本筛选器成功筛选了一个数组并仅输出完全匹配的大小写。我希望能够在不匹配大小写的情况下进行搜索,不影响各自情况下的搜索结果。
users = [
{fname: 'John', lname: 'doe'},
{fname: 'Jane', lname: 'Doe'}
];
<h3>USERS</h3>
<input type="text" Name="userSearch" [(ngModel)]="userSearch">
<ul *ngFor="let user of users">
<li>{{ user.fname }} {{ user.lname }}</li>
</ul>
Search Text: doe
Search Result: John doe
Expected Results: John doe
,Jane Doe
当前文本过滤器管道:
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({ name: 'textFilter' })
export class TextFilter implements PipeTransform
{
transform(value: any, term: any) {
if (!term) { return value; }
return value.filter((item: any) => {
for (let prop in item) {
if (typeof item[prop] === 'string' && item[prop].indexOf(term) > -1) {
return true;
}
}
return false;
});
}
}
试试这个
@Pipe({ name: 'textFilter' })
export class TextFilter implements PipeTransform
{
transform(value: any, term: any) {
if (!term) { return value; }
return value.filter((item: any) => {
for (let prop in item) {
if (typeof item[prop] === 'string' &&
item[prop].toLowerCase().indexOf(term.toLowerCase()) > -1) {
return true;
}
}
return false;
});
}
}
我当前的文本筛选器成功筛选了一个数组并仅输出完全匹配的大小写。我希望能够在不匹配大小写的情况下进行搜索,不影响各自情况下的搜索结果。
users = [
{fname: 'John', lname: 'doe'},
{fname: 'Jane', lname: 'Doe'}
];
<h3>USERS</h3>
<input type="text" Name="userSearch" [(ngModel)]="userSearch">
<ul *ngFor="let user of users">
<li>{{ user.fname }} {{ user.lname }}</li>
</ul>
Search Text:
doe
Search Result:John doe
Expected Results:John doe
,Jane Doe
当前文本过滤器管道:
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({ name: 'textFilter' })
export class TextFilter implements PipeTransform
{
transform(value: any, term: any) {
if (!term) { return value; }
return value.filter((item: any) => {
for (let prop in item) {
if (typeof item[prop] === 'string' && item[prop].indexOf(term) > -1) {
return true;
}
}
return false;
});
}
}
试试这个
@Pipe({ name: 'textFilter' })
export class TextFilter implements PipeTransform
{
transform(value: any, term: any) {
if (!term) { return value; }
return value.filter((item: any) => {
for (let prop in item) {
if (typeof item[prop] === 'string' &&
item[prop].toLowerCase().indexOf(term.toLowerCase()) > -1) {
return true;
}
}
return false;
});
}
}