I have multi-select drop-down with check-boxes and a drop-down list options. I wanted to implement an run time/dynamic filtered search for the drop-down options. I was able to implement filtered search but not that at run time.
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()">
How can I achieve filtered search dynamically?
Try like this:
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);
}
Demo
If you start typing sm
, the filtered option will show the object with value Small
try this:
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With