javascript - 根据首字母匹配搜索或过滤数据

标签 javascript angular typescript angular-ngselect

我正在使用 ng-select library用于实现搜索下拉菜单。我正在尝试实现一个自定义函数,该函数将在我开始输入时通过下拉列表中第一个匹配的字母来过滤项目。例如,如果用户输入“A”,它应该只在以“A”开头的下拉列表中显示人员姓名。

我创建了一个 stackblitz例子。我尝试使用 Array.filter() 函数,但过滤器函数将无法工作,因为我从我的“customSearchFn”返回“item” >' 不在数组中。有没有不使用过滤函数就可以实现的方法?

hello.component.ts

import { Component, Input } from '@angular/core';

@Component({
  selector: 'hello',
  template: `
    <ng-select
      [items]="people"
      bindLabel="name"
      autofocus
      [searchFn]="customSearchFn"
    >
    </ng-select>
  `,
  styles: [
    `
      h1 {
        font-family: Lato;
      }
    `
  ]
})
export class HelloComponent {
  @Input() name: string;
  people = [
    {
      id: '5a15b13c36e7a7f00cf0d7cb',
      index: 2,
      isActive: true,
      picture: 'http://placehold.it/32x32',
      age: 23,
      name: 'Karyn Wright',
      gender: 'female',
      company: 'ZOLAR',
      email: 'karynwright@zolar.com',
      phone: '+1 (851) 583-2547'
    },
    {
      id: '5a15b13c2340978ec3d2c0ea',
      index: 3,
      isActive: false,
      picture: 'http://placehold.it/32x32',
      age: 35,
      name: 'Rochelle Estes',
      disabled: true,
      gender: 'female',
      company: 'EXTRAWEAR',
      email: 'rochelleestes@extrawear.com',
      phone: '+1 (849) 408-2029'
    },
    {
      id: '5a15b13c2b1746e12788711f',
      index: 11,
      isActive: true,
      picture: 'http://placehold.it/32x32',
      age: 25,
      name: 'Nguyen Elliott',
      gender: 'male',
      company: 'PORTALINE',
      email: 'nguyenelliott@portaline.com',
      phone: '+1 (905) 491-3377'
    },
    {
      id: '5a15b13c605403381eec5019',
      index: 12,
      isActive: true,
      picture: 'http://placehold.it/32x32',
      age: 31,
      name: 'Mills Barnett',
      gender: 'male',
      company: 'FARMEX',
      email: 'millsbarnett@farmex.com',
      phone: '+1 (882) 462-3986'
    },
    {
      id: '5a15b13c947c836d177aa85c',
      index: 14,
      isActive: false,
      picture: 'http://placehold.it/32x32',
      age: 29,
      name: 'Yvette Navarro',
      gender: 'female',
      company: 'KINETICA',
      email: 'yvettenavarro@kinetica.com',
      phone: '+1 (807) 485-3824'
    },
    {
      id: '5a15b13c5dbbe61245c1fb73',
      index: 15,
      isActive: false,
      picture: 'http://placehold.it/32x32',
      age: 20,
      name: 'Elisa Guzman',
      gender: 'female',
      company: 'KAGE',
      email: 'elisaguzman@kage.com',
      phone: '+1 (868) 594-2919'
    }
  ];

  customSearchFn(term: string, item: any) {
    console.log(term);
    console.log(item);

  }
}

最佳答案

您只需将您的 customSearchFunction 修改为:

 customSearchFn(term: string, item: any) {
     // check if the name startsWith the input, everything in lowercase so "a" will match "A" 
     return item.name.toLowerCase().startsWith(term.toLowerCase())
 }

工作 stackblitz

仅供引用,您的列表中的每个项目都会调用自定义函数。

关于javascript - 根据首字母匹配搜索或过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68302172/

相关文章:

angular - 如何在库项目中具有绝对导入路径?

typescript - Cypress +TS : Get text from HTMLelement

javascript - 如何将变量值插入javascript :(here)

javascript - Kendo Scheduler 更新数据源后刷新

javascript - 奇怪的 fadeIn() 和 fadeOut() 问题 jQuery

php - 飞行响应中的 Access-Control-Allow-Headers 不允许请求 header 字段 Content-Type

javascript - 如何通过单击按钮或事件执行两项任务

angular - 使用 ng-content 将一个组件导入到另一个组件中

angular - 如何在 Angular 中创建动态菜单

typescript - 访问 "root"模块与实际子模块同名