Angular 2. PrimeNG listbox not working
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
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.
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[]
Glad to hear issue is resolved.
I have the same problem. I'm using SelectItem[] though. So weird.
@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" >
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
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'}">
Remember it works, I do not know if the use is correct