简体   繁体   English

Angular 4下拉多选不显示属性数据

[英]Angular 4 dropdown multiselect not showing property data

I just installed angular 4 multiselect dropdown to show the data that i am getting from JSON script using services. 我刚刚安装了angular 4 multiselect下拉列表,以显示使用服务从JSON脚本获取的数据。 I am getting the data in my property but now i want to show it in a multiselect dropdown so that i can use multiple values and assign them to my next property. 我正在属性中获取数据,但现在我想在多选下拉列表中显示它,以便可以使用多个值并将它们分配给下一个属性。 So in the below code i am calling a method of getSubject and it is returning me the data in this.subject property. 因此,在下面的代码中,我正在调用getSubject的方法,它向我返回了this.subject属性中的数据。

this._curriculumService.getSubject(this.appSession.tenant.tenancyName)
.finally(() => {this.saving = false;})
.subscribe((result: listResultDtoOfSubjectDto) => {
  this.subjects = result.items;

  this.id = this.subjects.map(a => a.code);
  this.itemName = this.subjects.map(a => a.name);

})

Now i want to show this data inside inside angular 4 dropdown multiselect and here is a code for that in my component.ts file. 现在,我想在angular 4下拉多选框内显示此数据,这是我component.ts文件中的代码。 The problem is that the dropdown asked for a specific id and name of the property and only then it will be able to show the data in dropdown but in my case i have a name and code returning in this.subjects . 问题是下拉列表要求属性的特定ID和名称,只有这样,它才能显示下拉列表中的数据,但是在我的情况下,我在this.subjects返回了namecode So how can i show my data in this dropdown 那么如何在此下拉列表中显示我的数据

optionsModel: number[];
myOptions: IMultiSelectOption[];
this.myOptions = [
  { id: 1, name: 'Physics' },
  { id: 2, name: 'English' },
  { id: 3, name: 'English' },
  { id: 4, name: 'Programming'}
];

HTML Code HTML代码

  <div class="form-line focused">
   <div class="col-sm-4">
    <div class="form-group form-float">
     <div class="form-line focused">
      <ss-multiselect-dropdown 
        [options]="myOptions"
        [(ngModel)]="optionsModel" 
        (ngModelChange)="onChange($event)">
      </ss-multiselect-dropdown>
     </div>
    </div>
   </div>
  </div>

So for that don't declare type of your myOptions as IMultiSelectOption[] , instead keep it any or whatever you're receiving from service. 因此,请勿将myOptions类型声明为IMultiSelectOption[] ,而应保留从服务接收到的任何内容。 As this plugin requires each options to have the id thus, you can add that property to myOptions objects after it's received from service response. 由于此插件要求每个选项都具有ID,因此,您可以在从服务响应接收到该属性后将该属性添加到myOptions对象中。 So, make sure that property should be unique valued (eg subject code). 因此,请确保该属性应具有唯一值(例如,主题代码)。 Let this.subjects is an array of objects you got from service: 让this.subjects是您从服务中获得的对象数组:

optionsModel: number[];
subjects: any;
this.subjects = [
  { code: 11, name: 'Physics' },
  { code: 12, name: 'English' },
  { code: 13, name: 'English' },
  { code: 14, name: 'Programming'}
];
this.subjects.forEach(function(e) { e.id = e.code }); 

The last line will add id property to each object with value same to subject code. 最后一行将为每个对象添加id属性,其值与主题代码相同。 Now the dropdown will work as expected. 现在,下拉列表将按预期工作。

Demo Example 示范范例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM