primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Angular 2. PrimeNG listbox not working

Open arjayfuentes opened this issue 9 years ago • 6 comments

I have a reactive form with two controls. One control is a dropdown that selects a country. The other control is a list of checkbox that selects the cities of the seleted country. I used primeNg's listbox in the last control (selection of cities). Now, I'm having a problem using primeng listbox. The problem is once I select a country all other countries is also selected. I'm also getting a null values in the second control of the form (slection if cities) even if there is a selected cities.

import { Component } from '@angular/core';
import { SelectItem } from 'primeng/primeng';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'my-app',
  templateUrl: 'app/app.template.html'
})
export class AppComponent {

    countryForm : FormGroup;

    countries: Country [] ;

    constructor(public fb: FormBuilder) { 
       this.countries = [
        {name:'USA', continent:'North America', cities :[
           {name: 'New York'} , { name: 'Los Angeles'} 
          ]},
        {name:'UK', continent:'Europe', cities :[
           {name: 'London'} , { name: 'Manchester'} , { name: 'Cambridge'} ,  { name: 'Bristol'}
          ]},
        {name:'Philippines', continent:'Asia', cities :[
           {name: 'Manila'} , { name: 'Davao'} , { name: 'Cebu'}
          ]},
         {name:'Japan', continent:'Asia', cities :[
           {name: 'Tokyo'} , { name: 'Kyoto'} , { name: 'Yokohama'}
          ]},
      ]
    }

    listOfCities: City [];

    ngOnInit() {
      this.countryForm= this.fb.group({
        selectedCountry:null,
        selectedCities: null
      });
      this.countryForm.controls['selectedCountry'].valueChanges.subscribe(
        value => {
          if(this.countryForm.controls['selectedCountry'].value!=null){
            this.listOfCities = this.countryForm.controls['selectedCountry'].value.cities;
          }
          else{
            this.listOfCities = [];
          }
      });
    }

    resetCountrySelection(){
      this.countryForm.controls['selectedCountry'].setValue(null);
      this.countryForm.controls['selectedCities'].setValue([]);

    }
}

Here's the plunkr http://plnkr.co/edit/yO4mNQveTyJgsvF9wUFE?p=preview

arjayfuentes avatar Jun 13 '17 22:06 arjayfuentes

The problem is once I select a country all other countries is also selected

I'm also having this issue. The plunker above perfectly describes the issue.

claudiuconstantin avatar Jun 14 '17 08:06 claudiuconstantin

I think figured it out: it seems that the data behind the listbox must be an array of SelectItem, other custom data types will cause some weird behavior. In your case listOfCities must be a SelectItem[]

claudiuconstantin avatar Jun 14 '17 10:06 claudiuconstantin

Glad to hear issue is resolved.

cagataycivici avatar Jun 15 '17 10:06 cagataycivici

I have the same problem. I'm using SelectItem[] though. So weird.

upondeep avatar Apr 18 '18 18:04 upondeep

@cagataycivici @claudiuconstantin Hi I am getting the same issue,

This is my HTML code: <p-listbox [options]="availableDimensions" [(ngModel)]="selectedDimensionsName" filter="filter" multiple="multiple" checkbox="checkbox" > {{dimension}}

This is my available dimension:

availableDimensions = ["Coke", "Pepsi", "Domino", " Display-fanta", "Video", "Direct", "Display", "Display"]

Declaration: availableDimensions: string[]; selectedDimensionsName: string[];

Getting something like this when I selected one single value all the value are selected stack

Shrinivassab avatar Jul 26 '18 15:07 Shrinivassab

I do not know if they have solved it, this is a possible solution works for me

I declare a variable of type "SelectItem" items: SelectItem[] = [];

In ngOnInit

for(var i = 0; i < this.availableDimensions.length; i++){ this.items.push({label: this.availableDimensions[i].name, value: this.availableDimensions[i].name}); }

In the HTML <p-listbox [options]="items" [(ngModel)]="selectedDimensionsName" [filter]="true" [multiple]="true" [checkbox]="true" [style]="{'width':'100%','max-height':'250px'}"> {{dimension.label}}

Remember it works, I do not know if the use is correct

felipegarciaosorio avatar Apr 12 '19 04:04 felipegarciaosorio