I have an open dropdown with multiple options. I created a simple pickList out of with two. I am using splice to remove selected values from the array after button click but when the selection is multiple, splice seems to remove all but the ones selected.
For example: in my list to the right - if i choose San Francisco, Miami, Boston and Las Vegas then move them over to the selected box on the right. This works perfect.
The issue is choosing Boston & Miami then clicking the left arrow to remove, removes all but the selected ones. I have used splice before but I do not remember it ignoring the items in array.
And my code component looks like this:
<select [(ngModel)]="foundLocations" multiple="multiple">
<option *ngFor="let locOption of locations" [ngValue]="locOption" >
{{ locOption }}
</option>
</select>
<div class="selectButtons">
<button (click)="selectThese()">></button>
<br/>
<button (click)="removeThese()"><</button>
</div>
<select [(ngModel)]="selectedLocations" multiple="multiple">
<option *ngFor="let chosen of pickedLocationItems" [ngValue]="chosen" >
{{ chosen }}
</option>
</select>
and in my component looks like:
foundLocations: any;
selectedLocations: any = [];
pickedLocationItems: any = [];
locations: any = ["San Francisco", "Seattle", "Las Vegas", "Toronto", "Boston", "Miami", "Altantic City"];
selectThese() {
for (var i = 0; i < this.foundLocations.length; i++) {
this.checkSelLocation(this.foundLocations[i]);
}
}
checkSelLocation(x: any) {
console.log("Check sel locations");
console.log(x);
this.pickedLocationItems.push(x);
}
removeThese() {
for (var g = 0; g < this.selectedLocations.length; g++) {
this.pickedLocationItems.splice(g, 1);
}
}
I have a Stackblitz example here.
