英文:
More items to be filtered in my angular project
问题
I'm creating a test project and I put a filter where I can filter the last name from my list of users.
But I wanted to filter not only the last name but also the name, email and username.
I tried to think of how to make these changes in my code but I couldn't so far. I wanted to know if you could help me.
My HTML:
<input type="text" [(ngModel)]="lastname" (input)="Search()"/>
<mat-table [dataSource]="usuarios">
<!-- Name Column -->
<ng-container matColumnDef="firstName">
<mat-header-cell *matHeaderCellDef> Nome </mat-header-cell>
<mat-cell *matCellDef="let element">{{ element.firstName }}</mat-cell>
</ng-container>
<!-- Last Name Column -->
<ng-container matColumnDef="lastName">
<mat-header-cell *matHeaderCellDef>Sobrenome</mat-header-cell>
<mat-cell *matCellDef="let element">{{ element.lastName }}</mat-cell>
</ng-container>
<!-- UserName Column -->
<ng-container matColumnDef="username">
<mat-header-cell *matHeaderCellDef>Username</mat-header-cell>
<mat-cell *matCellDef="let element">{{ element.username }}</mat-cell>
</ng-container>
<!-- Email Column -->
<ng-container matColumnDef="email">
<mat-header-cell *matHeaderCellDef>E-mail</mat-header-cell>
<mat-cell *matCellDef="let element">{{ element.email }}</mat-cell>
</ng-container>
</mat-table>
My component.ts:
export class GerenciamentoUsuariosListaComponent implements OnInit {
@Input() usuarios: any[] = [];
usuarios1: any;
lastname: string;
// ...
readonly displayedColumns = ['firstName', 'lastName', 'username', 'email', 'actions'];
constructor(private service: GerenciamentoUsuariosService) {
this.lastname = '';
}
// ...
ngOnInit(): void {
this.refreshListUser1();
}
refreshListUser1() {
this.usuarios1 = this.service.list().subscribe(
resp => {
this.usuarios = resp.content;
console.log(this.usuarios);
}
);
}
Search() {
if (this.lastname !== '') {
this.usuarios = this.usuarios.filter(res => {
const fullName = res.firstName + ' ' + res.lastName + ' ' + res.username + ' ' + res.email;
return fullName.toLocaleLowerCase().includes(this.lastname.toLocaleLowerCase());
});
} else {
this.ngOnInit();
}
}
}
If I missed more information and you need it, please let me know.
英文:
I'm creating a test project and I put a filter where I can filter the last name from my list of users.
But I wanted to filter not only the last name but also the name, email and username.
I tried to think of how to make these changes in my code but I couldn't so far. I wanted to know if you could help me.
My HTML:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<input type="text" [(ngModel)]="lastname" (input)="Search()"/>
<mat-table [dataSource]="usuarios">
<!-- Name Column -->
<ng-container matColumnDef="firstName">
<mat-header-cell *matHeaderCellDef> Nome </mat-header-cell>
<mat-cell *matCellDef="let element">{{ element.firstName }}</mat-cell>
</ng-container>
<!-- Last Name Column -->
<ng-container matColumnDef="lastName">
<mat-header-cell *matHeaderCellDef>Sobrenome</mat-header-cell>
<mat-cell *matCellDef="let element">{{ element.lastName }}</mat-cell>
</ng-container>
<!-- UserName Column -->
<ng-container matColumnDef="username">
<mat-header-cell *matHeaderCellDef>Username</mat-header-cell>
<mat-cell *matCellDef="let element">{{ element.username }}</mat-cell>
</ng-container>
<!-- Email Column -->
<ng-container matColumnDef="email">
<mat-header-cell *matHeaderCellDef>E-mail</mat-header-cell>
<mat-cell *matCellDef="let element">{{ element.email }}</mat-cell>
</ng-container> ....
<!-- end snippet -->
My component.ts:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
export class GerenciamentoUsuariosListaComponent implements OnInit {
@Input() usuarios: any[] = [];
usuarios1: any;
lastname :string;
..
readonly displayedColumns = ['firstName', 'lastName', 'username','email','actions'];
constructor(private service: GerenciamentoUsuariosService) {
this.lastname = '';
}
..
ngOnInit(): void {
this.refreshListUser1()
}
refreshListUser1() {
this.usuarios1 = this.service.list().subscribe(
resp => {
this.usuarios = resp.content
console.log(this.usuarios)
});
}
Search(){
if(this.lastname != ""){
this.usuarios = this.usuarios.filter(res =>{
return res.lastName.toLocaleLowerCase().match(this.lastname.toLocaleLowerCase());
})
}else if(this.lastname == ""){
this.ngOnInit();
}
}
}
<!-- end snippet -->
If I missed more information and you need it, please let me know.
答案1
得分: 1
你可以根据你想要的条件进行筛选
this.usuarios = this.usuarios.filter(res => {
const keyword = this.lastname.toLocaleLowerCase();
return res.firstName.toLocaleLowerCase().includes(keyword) ||
res.lastName.toLocaleLowerCase().includes(keyword) ||
res.username.toLocaleLowerCase().includes(keyword) ||
res.email.toLocaleLowerCase().includes(keyword);
});
这是用于部分搜索的代码,如果你想进行完全匹配搜索,那么你需要进行相等性检查。
希望对你有帮助。
英文:
You can filter out based on as many condition you want
this.usuarios = this.usuarios.filter(res =>{
const keyword = this.lastname.toLocaleLowerCase();
return res.firstName.toLocaleLowerCase().includes(keyword) ||
res.lastName.toLocaleLowerCase().includes(keyword) ||
res.username.toLocaleLowerCase().includes(keyword) ||
res.email.toLocaleLowerCase().includes(keyword);
});
That is for the partial search and if you want full search then you need to do the equality check.
Hope it helps you
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论