![](/img/trans.png)
[英]How can I filter drop-down list options by using custom scripts in Aurachain?
[英]Filter/search from the drop-down list of options using angular
我有带有复选框和下拉列表选项的多选下拉菜单。 我想为下拉选项实现运行时/动态过滤搜索。 我能够实现过滤搜索,但不能在运行时实现。
this.options = [{ value: "Small", selected: false },
{ value: "Medium", selected: false},
{ value: "Large", selected: false }]
filterUsers() {
let filterUsers= this.options.filter(item => item.value === this.selectedUser);
if (filterUsers.length > 0) {
this.options = filterUsers;
}
}
console.log(filterUsers);
}
HTML
<input type = "text" [(ngModel)]="selectedUser" (input) = "filterUsers()">
如何动态实现过滤搜索?
像这样尝试:
options = [
{ value: "Small", selected: false },
{ value: "Medium", selected: false },
{ value: "Large", selected: false }
];
selectedUser: any;
filterdOptions = [];
filterUsers() {
this.filterdOptions = this.options.filter(
item => item.value.toLowerCase().includes(this.selectedUser.toLowerCase())
);
console.log(this.filterdOptions);
}
如果您开始输入sm
,则过滤选项将显示值为Small
的对象
尝试这个:
options = [
{ value: "Small", selected: false },
{ value: "Medium", selected: false },
{ value: "Large", selected: false }
];
selectedUser: any;
filterdOptions = [];
filterUsers() {
this.filterdOptions = this.options.filter((item:any)=>{
return item.value.toLowerCase()=== this.selectedUser.toLowerCase()
});
console.log(this.filterdOptions);
}
如果您希望这是动态的(即基于更改的值或输入搜索词),您可以将管道用于此类任务。 见https://angular.io/guide/pipes
要在您的情况下实现,请首先创建此管道(并在您的应用程序模块声明中引用);
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'searchOptions'
})
export class SearchOptionsPipe implements PipeTransform {
transform(items: any[], filter: string): any {
if (!items || !filter) {
return items;
}
// This will search and match any option.value that contains the search term
return items.filter(item => item.value.toLowerCase().indexOf(filter.toLowerCase()) !== -1);
}
}
然后在你的 component.html (无论你在哪里 *ngFor'ing 你的选项);
<mat-option
*ngFor="let option of options | searchOptions:searchTerm"
[value]="option.value" ... >{{ option.title }}</mat-option>
最后在你的 component.ts 中你可以配置选项和搜索;
options = [
{value: 'apple', title: 'Yummy Apple'},
{value: 'orange', title: 'Juicy Orange'},
{value: 'pineapple', title: 'Sweet Pineapple'}
];
searchTerm = 'apple'; // Would show the 1st and 3rd item
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.