I have checkbox (with selected value by user, checked ). So I need to edit this checkbox and get all the selected value into an array and send it to post.
Html
<div *ngFor="let item of officeLIST">
<div *ngIf ="item.officeID== userdata.officeID">
<input #office type="checkbox" id="office" name="office" class="form-control" value="" checked (click)="officeSelection($event,$event.target.value)" >
</div>
<div *ngIf ="item.officeID!= userData.officeID">
<input #office type="checkbox" id="office" name="office" class="form-control" value="" (click)="officeSelection($event,$event.target.value)">
</div>
</div>
<button type="button" class="btn btn-primary"(click)="updateOffice(userData.officeID)"> Save </button>
Click function (to add selected value and to remove unselected value)
officeSelection(event: any, value) {
if (event.target.checked === true)
{
this.officeArray.push(value);
}
else {
this.officeArray.splice(this.officeArray.indexOf(value), 1);
}
}
Update Function
updateOfficeDetail(officeid) {
if(officeid= 1)
{
this.officeArray.push('stars dept');
}
else if(officeid= 2)
{
this.officeArray.push('moon dept');
}
else
{
this.officeArray.push('Sun dept');
}
}
Problem is if I update the data without changing current value of checkbox,eg: sun dept,
this sun dept will be push into officeArray .
but if again I want to update the data by editing the checkbox(choose another value), so the previous selected value and current selected value will be push into this officeArray
if user did not edit the checkbox (just proceed with current selected value)>>>> the officeArray will look like this
["sun"]
and if the user tried to update the checkbox by clicking one more checkbox, the array will look like this
["sun","moon","sun"]
I just want ["sun","moon"] in the officeArray. should i remove? or clear something?
I tried to do some research and a lot questions similar to me. but somehow all the solutions did not work for me. any idea?
Thank you
Aucun commentaire:
Enregistrer un commentaire