Search code examples
angulartypescriptdropdownboxngx-select-dropdown

ngx-select-dropdown set displayKey with multiple values


I am using ngx-select-dropdown with search feature and I want to set multiple displayKey values like: firstname and lastname.

Below is my config object:

dropdownconfig = {
    displayKey: 'firstName, lastName',
    search: true,
    placeholder: 'Select User',
    height: '150px'
};

html:

<ngx-select-dropdown (change)="selecteAdmin($event)" [config]="dropdownconfig" [options]="allUserData" [(ngModel)]="singleSelect" [multiple]="false" ></ngx-select-dropdown>

How to display multiple values for displayKey? Also, need validation on that drop-down.


Solution

  • Create custom field in your array object. And use it as key.

    dropdownconfig = {
        displayKey: "custom",
        search: true,
        placeholder: "Select User",
        height: "150px"
      };
    
    
      allUserData = [
        { firstName: 'first1', lastName: 'last1'}, 
        { firstName: 'first2', lastName: 'last2'},
        { firstName: 'first3', lastName: 'last3'},
        { firstName: 'first4', lastName: 'last4'}
      ]
    
      ngOnInit() {
        for (let user of this.allUserData) {
          user['custom'] = user.firstName + ' ' + user.lastName;
        }
      }
    

    Working Stackblitz