First multiselect dropdown :-
<p-multiSelect (onChange)="ondestinationSelect($event, dataKey)" [options]="destinations" [showToggleAll]="false" [filter]="false" optionLabel="destination_name" dataKey="id" defaultLabel="Select Destination(s)" maxSelectedLabels="0" selectedItemsLabel="{0} Destination(s) Selected"></p-multiSelect>
Based on values selected/deselected from above dropdown it should add/remove values from below dropdown :-
<p-multiSelect (onChange)="onresidenceSelect($event, dataKey)" [options]="residences" [showToggleAll]="false" [filter]="false" [optionLabel]="residence_name" [dataKey]="id" defaultLabel="Select Residence(s)" maxSelectedLabels="0" selectedItemsLabel="{0} Residence(s) Selected"></p-multiSelect>
Eg. values of first drop down(this is a response of service):
0:{id: "1", destination_name: "Matheran"}
1:{id: "2", destination_name: "Mumbai destination"}
2:{id: "3", destination_name: "Lonavla"}
3:{id: "4", destination_name: "Nashik"}
values of second drop down when second option from above values is selected :
0:{id: "1", residence_name: "Mumbai Residence 1", destination_id: "2"}
1:{id: "2", residence_name: "Mumbai Residence 2", destination_id: "2"}
2:{id: "3", residence_name: "saegrg asrgsdg", destination_id: "2"}
3:{id: "4", residence_name: "Test residence 1", destination_id: "2"}
4:{id: "6", residence_name: "sdbdfb dfgsgfb ddfgdf", destination_id: "2"}
5:{id: "8", residence_name: "Residence Name test 1.11 update 1.0", destination_id: "2"}
6:{id: "13", residence_name: "Residence Name test 2", destination_id: "2"}
7:{id: "15", residence_name: "drbbg", destination_id: "2"}
8:{id: "16", residence_name: "dkjg dgjj", destination_id: "2"}
9:{id: "17", residence_name: "test res 1", destination_id: "2"}
10:{id: "18", residence_name: "test 2", destination_id: "2"}
11:{id: "19", residence_name: "test 2", destination_id: "2"}
Now, when option 2 from 1st drop down is deselected then 2nd dropdown should be empty. But in my case it not getting empty.
Here is the code that I tried :
ondestinationSelect(event) {
let destinationIdArray = <FormArray>this.addteammateform.get('destinationsresponsiblefor');
let destinationId = new FormControl();
if(event.value.length > destinationIdArray.length) {
this.usermanagementService.get_residences(event.itemValue.id).subscribe(response => {
if(response) {
this.residences = response;
}
});
destinationId.setValue(event.itemValue.id);
destinationIdArray.push(destinationId);
}
else {
let i = 0;
if(this.residences != undefined) {
while(i < this.residences.length) {
if(this.residences[i].destination_id == event.itemValue.id) {
this.residences.splice(this.residences[i], 1);
}
else {
++i;
}
}
}
let destinationIndex = destinationIdArray.controls.indexOf(event.itemValue.id);
destinationIdArray.removeAt(destinationIndex);
}
}