如何在ng-multiselect-dropdown中传递值

时间:2019-08-12 11:28:21

标签: angular

这是我之前所做的,无论我选择的值= {{{user.id}}“”传递到offerCode.user_ids的下拉列表

<select name="user_ids" class="form-control" [(ngModel)]="offerCode.user_ids" multiple>
            <option value="null" disabled selected="selected">Select User(s)</option>
            <option *ngFor="let user of allUsers" value="{{user.id}}">{{user.id}} {{user.first_name}} {{user.last_name}} - {{user.email}}</option>
</select>

现在我正在使用ng-multiselect https://www.npmjs.com/package/ng-multiselect-dropdown 除了将idField:'id'和textField:'text'中的值作为单个对象传递到offerCode.user_ids之外,其他所有方法都工作正常。 我只想传递单个用户ID

<ng-multiselect-dropdown name="user_ids"
          [placeholder]="'Select User(s)'"
          [data]="allUsers"
          [(ngModel)]="offerCode.user_ids"
          [settings]="dropdownSettings"
          (onSelectAll)="onSelectAll($event)"
          >
</ng-multiselect-dropdown>

ngOnInit() {
    this.authService.getAllUsers().subscribe((res: any)=>{

      if(res.status === 'success') {
        this.allUsers = res.data;
        for (let index = 0; index < this.allUsers.length; index++) {
          const user = this.allUsers[index];
          user['text'] = user.first_name + " " + user.last_name + " " + user.email;
        }
      } else {
        this.toastr.error('get all users - success not returned');
      }
    },(err)=>{
      this.toastr.error("Couldn't get users");
});
this.dropdownSettings = {
      singleSelection: false,
      idField: 'id',
      textField: 'text',
      selectAllText: 'Select All',
      unSelectAllText: 'UnSelect All',
      itemsShowLimit: 3,
      allowSearchFilter: true
    };
}

我尝试了此方法,但是它不起作用,该对象保持不变。

onSelectAll(items: any) {
  for (let index = 0; index < items.length; index++) {
    const user = items[index];
    this.offerCode.user_ids = []
    this.offerCode.user_ids.push(user.id)
  }
  console.log(this.offerCode)
}

0 个答案:

没有答案