How to set default value for PrimeNG p-dropdown

后端 未结 8 927
南笙
南笙 2020-12-09 17:53

I am using PrimeNG in my angular5 app. I have issue with p-dropdown

Question

I have p-dropdown for showing countries. I bind the select opti

相关标签:
8条回答
  • 2020-12-09 18:34

    My solution was to have the countries loaded in the controller before setting the form field (ngModel or formControl). Also keep the same type of the key. Don't use number for the form control and string for the list:

    // first get the cities from the server
    response.cities.forEach(city => {
                          this.dropdowns['cities'].push({ value: city.id, label: element.city }); });
    
    // when setting the form
    city_id: new FormControl(this.user.city_id)
    

    In the code above this.user.city_id and city.id has the same type number

    0 讨论(0)
  • 2020-12-09 18:45

    You can set default value of PrimeNG Dropdown by using ngModel as shown on the following approach:


    component.html:

    <p-dropdown [options]="cities" name="selectedCity" [(ngModel)]="selectedCity"></p-dropdown>
    


    component.ts:

    selectedCity: string = 1; //Id value of the City to be selected
    


    If it is not fixed due to version problems, try this:

    this.cities.value = this.selectedCity;  
    

    Hope this helps...

    0 讨论(0)
  • 2020-12-09 18:46

    This may be caused if PrimeNG doesn't know to which field to bind the "selectedCountry", ie. your "countries" model for the dropdown control has more then key and value properties.

    In my case, I had to explicitly "tell" to each dropdown field that the property for values is "value". I used the p-dropdown dataKey property for this.

    So, in my dropdown control, I added something like this:

    <p-dropdown dataKey="value" ></p-dropdown>
    

    You can read more here.

    0 讨论(0)
  • 2020-12-09 18:47

    I use this solution to fix this

    html:

    <p-dropdown name="country" [options]="countries" [(ngModel)]="country" placeholder="select country" (onChange)="changeCountry()"></p-dropdown>
    

    ts:

        public country;
        public countries;
        ngOnInit() {
            this.applicant.country = 'India';
            this.getCountry().then(()=>{
              this.country = this.applicant.country
            })
        } 
        getCountry(){
          return new Promise( (resolve,reject) => {
            this.UserService.getallcountries().subscribe(result => {
              this.cnt.forEach(element => {
                this.countries.push({
                  label: element.name,
                  value: element.id
                });
              });
              resolve();
            }, error =>{
              reject();
            });
          })          
        }
        changeCountry(){
         this.country = this.applicant.country;
        }
    

    it work at primeng 6.1.3

    0 讨论(0)
  • 2020-12-09 18:48

    I just had a similar problem. I solved this with the html attribute "optionLabel". If we read the PrimeNG documentation it says this : Name of the label field of an option when an arbitrary objects instead of SelectItems are used as options.

    Official documentation

    Hope it helps

    0 讨论(0)
  • 2020-12-09 18:49

    I been having this issue too and after several minutes debugging, I have found that some of the common reason for this problem can be:

    1) Type mismatch - The drop-down can be binding to integers and [(ngModel)] property can be an string.

    For example:

    <p-dropdown [options]="countries" [(ngModel)]="selectedCountry"></p-dropdown>
    

    Where

    countries = [1,2,3]
    

    and

    selectedCountry = '1'
    

    2) Uppercase- Lower-Case - The drop-down can be binding to an string that is lower case and [(ngModel)] property can be on Uppercase or a combination of both.

    For example:

    countries = ['United States', 'England', 'Bolivia']
    

    and

    selectedCountry = 'united states'
    

    It has to be an exact match to work as expected, in this case 'United States'

    0 讨论(0)
提交回复
热议问题