I know this is an old one, but here's a bit generic component (can be easily extended to be fully generic) that allows search by multiple fields.
StackBlitz link
Full component:
@Component({
selector: "app-generic-select",
template: `
<ng-select
[formControl]="control"
class="select-control"
id="item-select"
[items]="adjustedAvailableItems"
[multiple]="true"
[closeOnSelect]="false"
[clearSearchOnAdd]="true"
[hideSelected]="true"
bindLabel="searchField"
>
<ng-template ng-multi-label-tmp let-items="items" let-clear="clear">
<div class="ng-value" *ngFor="let item of items">
<span
class="ng-value-icon left"
(click)="clear(item)"
aria-hidden="true"
>×</span
>
<span class="ng-value-label">{{ getText(item) }}</span>
</div>
</ng-template>
<ng-template ng-option-tmp let-item="item">
{{ getText(item) }}
</ng-template>
</ng-select>
`
})
export class GenericSelectComponent implements OnChanges {
@Input() control: FormControl;
@Input() availableItems: any[];
@Input() printContent: (item) => string;
@Input() itemSearchFields: string[];
adjustedAvailableItems: any[];
constructor() {}
ngOnChanges(changes: SimpleChanges) {
if (changes.itemSearchFields || changes.availableItems) {
this.adjustedAvailableItems = this.adjustAvailableItems(
this.availableItems
);
}
}
private adjustAvailableItems(items: any[]): any[] {
if (!this.itemSearchFields || !this.itemSearchFields.length) {
return items;
}
return items.map(item => {
item.searchField = this.itemSearchFields
.map(searchField => item[searchField])
.reduce((curr, next) => curr + " " + next);
return item;
});
}
getText(item: any): string {
if (!item) {
return "";
}
if (!this.printContent) {
return item.toString();
}
return this.printContent(item);
}
}
usage:
@Component({
selector: "my-app",
template: `
<app-generic-select
[availableItems]="availableAccounts"
[control]="accountControl"
[itemSearchFields]="['name', 'country']"
[printContent]="accountText"
>
</app-generic-select>
{{ accountForm.value | json }}
`
})
export class AppComponent implements OnInit {
accountForm: FormGroup;
availableAccounts: Account[] = [];
delayedObservable = Observable.of(this.getTestAccounts()).delay(3000);
constructor(private formBuilder: FormBuilder) {}
ngOnInit(): void {
this.accountForm = this.formBuilder.group({
accounts: [[], []]
});
this.delayedObservable.subscribe(
accounts => (this.availableAccounts = accounts)
);
}
get accountControl(): FormControl {
return this.accountForm.get("accounts") as FormControl;
}
accountText = (item: Account): string => {
return item.name + " - " + item.country;
};
getTestAccounts(): Account[] {
return [
{
name: "Adam",
email: "[email protected]",
age: 12,
country: "United States"
},
{
name: "Samantha",
email: "[email protected]",
age: 30,
country: "United States"
},
{
name: "Amalie",
email: "[email protected]",
age: 12,
country: "Argentina"
},
{
name: "Estefanía",
email: "[email protected]",
age: 21,
country: "Argentina"
},
{
name: "Adrian",
email: "[email protected]",
age: 21,
country: "Ecuador"
},
{
name: "Wladimir",
email: "[email protected]",
age: 30,
country: "Ecuador"
},
{
name: "Natasha",
email: "[email protected]",
age: 54,
country: "Ecuador"
},
{
name: "Nicole",
email: "[email protected]",
age: 43,
country: "Colombia"
},
{
name: "Michael",
email: "[email protected]",
age: 15,
country: "Colombia"
},
{
name: "Nicolás",
email: "[email protected]",
age: 43,
country: "Colombia"
}
];
}
}
bindLabel="{{ firstName + lastName }}"
? – Varve[bindLabel]="firstName + lastName"
remove{{}}
added before – Varve