valor-software / ng2-select

Angular based replacement for select boxes
http://valor-software.github.io/ng2-select/
MIT License
675 stars 587 forks source link

pass extra data to list value and read it at select funciton #928

Open ahmadkhalaf1 opened 6 years ago

ahmadkhalaf1 commented 6 years ago

Hello i am passing this object to the ng-select items image

but when i select from the list and go to select function i only get Text and id , i need to get the extra data like nickName , status , userId and so on ? any idea how?

optimistex commented 6 years ago

Try to store the array of objects in a variable (for example userItems). Then you can do that:

userItems.find(user => user.id === userId);

And... You can try to use that fork: https://github.com/optimistex/ng2-select-ex There is returning just ID. Your case may be looked like this:

<ngx-select [items]="userItems" 
                   [ngModel]="selectedUserId" (ngModelChange)="userChange($event)">
</ngx-select>
export class SomeComponent {
   public selectedUserId: any[] = [];
   public userChange(userId: string) {
      const user = selectedUserId.find(item => item.id === userId);
      console.log('user data', user);
   }
}