简体   繁体   English

p-dropdown 未正确显示标签

[英]p-dropdown is not displaying the labels correctly

I have trouble getting the dropdown component to work.我无法让下拉组件正常工作。 The dropdown seems to detect the items it should display because it widens the itemlist according to the number of items in the array.下拉列表似乎检测到它应该显示的项目,因为它根据数组中的项目数扩大了项目列表。 However the spaces are all blank.但是空格都是空白的。

在此处输入图片说明

This is the same dropdown box as from the example at https://www.primefaces.org/primeng/#/dropdown (the first one with header 'simple')这与https://www.primefaces.org/primeng/#/dropdown示例中的下拉框相同(第一个带有标题“简单”的下拉框)

However with me it doesn't display anything.但是对我来说它不显示任何内容。 I copy pasted the exact same code, the only difference are the imports.我复制粘贴了完全相同的代码,唯一的区别是导入。 When i go to the github repository i can see that they import当我转到 github 存储库时,我可以看到它们导入

import {SelectItem} from '../../../components/common/api';

and

import {DropdownModule} from '../../../components/dropdown/dropdown';

Where I use我在哪里使用

import {SelectItem} from 'primeng/api';

and

import {DropdownModule} from 'primeng/dropdown';

When i try to use the imports from github then it says it can find dropdownmodule and selectitem at those locations.当我尝试使用来自 github 的导入时,它说它可以在这些位置找到 dropdownmodule 和 selectitem。

Heres my code:这是我的代码:

interface City {

  name: string,

  code: string

}
export class Test implements OnInit {

 cities1: City[];

  selectedCity: City;
  constructor() {
    this.cities1 = [
      {label:'Select City', value:null},
      {label:'New York', value:{id:1, name: 'New York', code: 'NY'}},
      {label:'Rome', value:{id:2, name: 'Rome', code: 'RM'}},
      {label:'London', value:{id:3, name: 'London', code: 'LDN'}},
      {label:'Istanbul', value:{id:4, name: 'Istanbul', code: 'IST'}},
      {label:'Paris', value:{id:5, name: 'Paris', code: 'PRS'}}
  ];
   }

}

heres the html继承人的html

<p-dropdown [options]="cities1" [(ngModel)]="selectedCity" placeholder="Select a City" optionLabel="name" [showClear]="true"></p-dropdown>
<p>Selected City: {{selectedCity ? selectedCity.name : 'none'}}</p>

Anyone know how i can fix this?有谁知道我该如何解决这个问题?

Thank you谢谢

remove optionLabel and code will work -删除optionLabel和代码将工作 -

<p-dropdown [options]="cities1" [(ngModel)]="selectedCity" placeholder="Select a City" [showClear]="true"></p-dropdown>

OptionLabel : Name of the label field of an option when an arbitrary objects instead of SelectItems are used as options. OptionLabel :当使用任意对象而不是 SelectItems 作为选项时选项的标签字段的名称。

add optionLabel with the key name from the json array.使用 json 数组中的键名添加optionLabel The key you want to represent as label.要表示为标签的键。

<p-dropdown optionLabel="label" [options]="cities1" [(ngModel)]="selectedCity" placeholder="Select a City" [showClear]="true"></p-dropdown>

Try this:尝试这个:

<p-dropdown 
    [options]="cities1" 
    [(ngModel)]="selectedCity" 
    placeholder="Select a City" 
    optionLabel="value.name" 
    [showClear]="true">
</p-dropdown>

Note this: optionLabel="value.name"请注意: optionLabel="value.name"

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

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