Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter/search from the drop-down list of options using angular

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?

like image 365
New123 Avatar asked Jan 26 '23 13:01

New123


2 Answers

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

like image 97
Adrita Sharma Avatar answered Jan 28 '23 10:01

Adrita Sharma


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);
}
like image 30
Muhammad Abdullah Avatar answered Jan 28 '23 10:01

Muhammad Abdullah