最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - p-dropdown is not displaying the labels correctly - Stack Overflow

programmeradmin1浏览0评论

I have trouble getting the dropdown ponent 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 (the first one with header 'simple')

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

import {SelectItem} from '../../../ponents/mon/api';

and

import {DropdownModule} from '../../../ponents/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.

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

<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

I have trouble getting the dropdown ponent 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/primeng/#/dropdown (the first one with header 'simple')

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

import {SelectItem} from '../../../ponents/mon/api';

and

import {DropdownModule} from '../../../ponents/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.

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

<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

Share Improve this question edited Jun 5, 2018 at 11:48 Jasper de Vries 20.3k6 gold badges67 silver badges107 bronze badges asked Jun 5, 2018 at 11:45 MauriceMaurice 7,40111 gold badges59 silver badges124 bronze badges 1
  • haha wanted to change it to primeNG but you beat me to it, thanks. – Maurice Commented Jun 5, 2018 at 11:52
Add a ment  | 

3 Answers 3

Reset to default 3

remove optionLabel and code will work -

<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.

add optionLabel with the key name from the json array. 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"

发布评论

评论列表(0)

  1. 暂无评论