【问题标题】:Filter/search from the drop-down list of options using angular使用角度从选项下拉列表中过滤/搜索
【发布时间】:2020-08-27 10:32:44
【问题描述】:

我有带有复选框和下拉列表选项的多选下拉菜单。我想为下拉选项实现运行时/动态过滤搜索。 我能够实现过滤搜索,但在运行时无法实现。

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()">

如何动态实现过滤搜索?

【问题讨论】:

  • 您面临的真正问题是什么?
  • 我认为你的函数没有返回空数组

标签: javascript angular typescript


【解决方案1】:

试试这样:

  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

如果您开始输入sm,过滤选项将显示值为Small的对象

【讨论】:

    【解决方案2】:

    试试这个:

    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);
    }
    

    【讨论】:

      【解决方案3】:

      如果您希望它是动态的(即基于更改值或输入搜索词),您可以将管道用于此类任务。见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
      

      【讨论】:

      • 在这种情况下我在哪里输入值?
      • @LeonardoRick searchTerm 变量是这里的输入。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      相关资源
      最近更新 更多