yellowSub
yellowSub

Reputation: 209

search or filter data based on first letter match

I am using ng-select library for implementing a search dropdown. I am trying to implement a custom function that will filter items by first matching letter in the dropdown when I start typing. For example if user types in 'A', it should only show People name in the dropdown that starts with "A".

I have created an stackblitz example. I tried using Array.filter() function, but filter function won't work since the 'item' I am getting returned from my 'customSearchFn' is not in an array. Is there a way to implement this without using filter function?

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: '[email protected]',
      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: '[email protected]',
      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: '[email protected]',
      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: '[email protected]',
      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: '[email protected]',
      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: '[email protected]',
      phone: '+1 (868) 594-2919'
    }
  ];

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

  }
}

Upvotes: 3

Views: 2796

Answers (1)

Ariel
Ariel

Reputation: 1436

You only need to modify your customSearchFunction to:

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

Working stackblitz

FYI, the custom function is called with every item on your list.

Upvotes: 1

Related Questions