5

I have situation where I need to sort the data based on custom logic at time of search under ng-select. There is option for Search across multiple fields using [searchFn] but I need to sort the match result based on my custom logic.

  1. Is this possible ?
  2. Please share some initial ideal how this can be achieved ?

ng-select : https://github.com/ng-select/ng-select

Kaushik Thanki
  • 3,334
  • 3
  • 23
  • 50

2 Answers2

1

You could listen to the search event and fire a custom function to filter your dataset and then modify the dataset that the ng-select has. You will need to manage repopulating the dropdown though if nothing is searched!

<ng-select (search)="onSearch($event)" [items]="data"></ng-select>

onSearch(e) {
    // Do your custom filtering here to the total dataset and then reassign.
    this.data = filtered_data;
}
Sam
  • 1,736
  • 8
  • 15
0

You might also consider using MatSort, simply fire it after clicking the search button. i'm not sure if this would fit your request but you can see an example here: https://material.angular.io/components/sort/overview

R.K
  • 197
  • 7