Reputation: 9084
I am in the need of splicing a data from the array, for which i have used the following,
Html:
<div id="app">
<select v-model="facilityAvailable" name="facilities" multiple="multiple" id="facilities" size="4" class="form-control">
<option v-for="availability in availableFacilities" v-bind:value="availability">{{availability.label}}--</option>
</select>
<a @click="removeFacilities" class="btn btn-default remove_option" rel="facilities2" id="remove"><i class="fa fa-arrow-left"></i></a>
<a @click="addFacilities" class="btn btn-default add_option" rel="facilities2" id="add"><i class="fa fa-arrow-right"></i></a>
<select v-model="facilitySelected" name="facilities" multiple="multiple" id="facilities2" size="4" class="form-control">
<option v-for="facility in selectedFacilities" v-bind:value="facility">{{facility.label}}</option>
</select>
</div>
Script:
new Vue({
el: "#app",
data: {
facilityAvailable: [],
facilitySelected:[],
availableFacilities: [{
value: 0,
label: 'Double (Non a/c)'
},
{
value: 1,
label: 'Premium Double (a/c)'
},
{
value: 2,
label: 'Standard Double (a/c)'
}
],
selectedFacilities: [],
},
methods: {
removeFacilities() {
this.availableFacilities = this.facilitySelected.concat(this.availableFacilities);
this.selectedFacilities.splice(this.availableFacilities.value,1);
},
addFacilities() {
this.selectedFacilities = this.facilityAvailable.concat(this.selectedFacilities);
this.availableFacilities.splice(this.selectedFacilities.value,1);
}
}
})
Here i am doing like transfer of one element from one select box to the other to another select box, for which i have used concat
for adding to another and splice
to remove that element.. Everything was fine upto this. But when i splice
, it is splicing in the order of 0,1,2
i need to splice based on the index number of that particular element. In clear, i can select and remove the element in any order for which that particular element needs to be removed and gets transferred into second select box, whereas now if i remove it in the order as it was , its working fine but whereas if i change the order and splice its not working. Also when i select multiple elements and remove, the same thing happening. Kindly help me to solve this issue.
The fiddle link was, https://jsfiddle.net/pmvp3td6/9/
Upvotes: 1
Views: 1838
Reputation: 1194
I'm not sure i understood right what you are looking for but try with that in your addFacilities :
addFacilities() {
let i = 0;
for(i; i<this.facilityAvailable.length;i++){
this.selectedFacilities.push(this.facilityAvailable[i]);
this.availableFacilities = this.availableFacilities.filter(facilities => facilities.value != this.facilityAvailable[i].value);
}
}
If that's what you want you can do something similar in the remove part
Upvotes: 1
Reputation: 3289
Have a look at Element-UI's Transfer component. This should solve your problem.
Upvotes: 0