简体   繁体   English

PrimeNG multi select 中的多选显示空值

[英]Multiselect in PrimeNG multi select showing empty values

I have a multiselect component:我有一个多选组件:

<p-multiSelect class="multiple-select" display="chip" [options]="allTags" name="selectedTags" [(ngModel)]="selectedTags" optionLabel="tag" optionValue="tag"></p-multiSelect>

A single tag in the allTags array: allTags 数组中的单个标签:

{ tag: 'someTag' }

And the selectedTags array contains strings. selectedTags 数组包含字符串。 when adding tags, it works perfectly.添加标签时,它可以完美运行。 The issue arises when I want to preselect some tags.当我想预选一些标签时会出现问题。

  preselectTags(tags: string[]) {
    tags.map(x => this.selectedTags.push(x));

  }

Ive tried forEach, and plain re assigning instead of array.map but every time I see something like this:我试过forEach,并简单地重新分配而不是array.map,但每次我看到这样的东西:

在此处输入图像描述

Console.logging the selectedTags after filling it returns an array of correct strings. Console.logging selectedTags 在填充后返回一个正确的字符串数组。 what am i doing wrong?我究竟做错了什么?

PS. PS。 thanks for advance.感谢提前。

If I'm not wrong, I suppose that selectedTags array should have the same structure as allTags so, when you make a push you have to build that structure.如果我没记错的话,我认为selectedTags数组应该与allTags具有相同的结构,因此,当您进行推送时,您必须构建该结构。

preselectTags(tags: string[]) {
   tags.map(x => this.selectedTags.push({val:x}));
}

Hope you helpfully, if don't, please give the prints of each array structure.希望对您有所帮助,如果没有,请提供每个数组结构的打印。 Regards问候

You would need to do a find of the options in order to filter out unwanted items/selecting the tags..您需要查找选项以过滤掉不需要的项目/选择标签..

So depending on what the tags array is, you could loop over them, or try:因此,根据标签数组是什么,您可以遍历它们,或尝试:

preselectTags(tags: string[]) {

  this.selectedTags = tags.map(x => this.allTags.find(y => y.val === x.val));
}

It would be easier to help with the structure of said arrays....对上述 arrays 的结构提供帮助会更容易......

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

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