【问题标题】:ng-select: how to select multiple items having same label but different objectng-select:如何选择具有相同标签但对象不同的多个项目
【发布时间】:2020-05-23 11:52:16
【问题描述】:

我正在尝试修补 ng-select 下拉菜单中的多个选定项目。我没有指定任何 bindValue,所以项目应该按对象绑定(在https://www.npmjs.com/package/@ng-select/ng-select 中指定)

但在尝试执行此操作时,不会根据对象而是根据显示的标签来选择项目。也只有第一个标签被选中。

示例:app.component.html

<form [formGroup]="group">
    <div class="form-group" formGroupName="school">
        <ng-select labelForId="school" placeholder="Select school" [items]="schools" formControlName="code"
            bindLabel="displayLabel" multiple="true" groupBy="city">
        </ng-select>
    </div>
</form>

app.component.ts

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  group: FormGroup;
  schools = [];
  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    this.initSchools();
    const formModel = {
      school: new FormGroup({
        code: new FormControl(null, Validators.required)
      })
    }
    this.group = this.fb.group(formModel);
    this.group.patchValue({
      school: {
        code:
          [
            {
              schoolId: 'SC003',
              displayLabel: 'Test-3 school',
              city: "City1"
            },
            {
              schoolId: 'SC005',
              displayLabel: 'Test-3 school',
              city: "City5"
            }
          ]
      }
    });
  }

  initSchools() {
    this.schools.push(
      {
        schoolId: 'SC001',
        displayLabel: 'Test-1 school',
        city: "City1"
      },
      {
        schoolId: 'SC002',
        displayLabel: 'Test-2 school',
        city: "City2"
      },
      {
        schoolId: 'SC003',
        displayLabel: 'Test-3 school',
        city: "City3"
      },
      {
        schoolId: 'SC004',
        displayLabel: 'Test-4 school',
        city: "City4"
      },
      {
        schoolId: 'SC005',
        displayLabel: 'Test-3 school',
        city: "City5"
      }
    );
  }
}

在上面的例子中,只有 item 的对象

    {
      schoolId: 'SC003',
      displayLabel: 'Test-3 school',
      city: "City1"
    },

被选中,

而下面的项目没有

    {
      schoolId: 'SC005',
      displayLabel: 'Test-3 school',
      city: "City5"
    }

应该怎么做才能在下拉列表中选择这两个项目(提供唯一对象)。 这里有什么遗漏吗。任何其他方式来实现这一点。

在这里运行示例:https://stackblitz.com/edit/angular-szel64?file=src%2Fapp%2Fapp.component.ts

【问题讨论】:

    标签: angular dropdown angular-reactive-forms multi-select angular-ngselect


    【解决方案1】:

    发现 ng-select 有一个 compareWith 选项,可让您定义一个函数来比较对象。 使用它您可以绑定对象,并根据 compareWith 选项提供的功能进行选择。

    以下是我的更改,以防有人遇到同样的问题

    app.component.html

    <form [formGroup]="group">
        <div class="form-group" formGroupName="school">
            <ng-select labelForId="school" placeholder="Select school" [items]="schools" formControlName="code"
                bindLabel="displayLabel" multiple="true" groupBy="city"
          [compareWith]="compareFunction">
            </ng-select>
        </div>
    </form>
    

    app.component.ts

    ...
      compareFunction(item, selected) {
    // any logic to compare the objects and return true or false
        return item.schoolId === selected.schoolId
      }
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多