mercredi 9 décembre 2020

i want to uncheck a checkbox on remove filed using splice

this is my html file: The terms i got from api. On selection of checkbox , it will show selected value in selectedTermsArray.


<div class="termsList">
 <span class="suggestermsSpan" >Suggested terms </span> <br>
    <div *ngFor="let list of incentiveTermsArray" class="suggestedTerms">      
       <input name="checkboxName" class="checkmark" id="checkbox1" type="checkbox" value="" (change)="$event.target.checked? (isfrmChecked = true) : isfrmChecked = false; onCheckboxSelected($event,isfrmChecked);">
         
    </div>
</div>

this is also my html file:

the terms that are selected on checkbox selection of above html code. When I click on remove field value close icon. value is removed from selectedtermsArray. But, checkbox is not getting unchecked.

<div>
   <div *ngFor="let selectedValue of selectedTermsArray ; let i = index" class="suggestedTerms">     
      
     <span></span>
     <img src="../../assets/Images/cancel-icon.png" class="closecls" (click)="removeField(i)">

      <!-- <mat-icon class="closeicon" (click)="removeField(i)">close</mat-icon>  -->
                                       
   </div>
</div>


this is my ts file..


 selectedTermsArray: string[] = [];
 isfrmChecked:any;

 onCheckboxSelected(event: any, isChecked: boolean)
 {
   if (isChecked) {
     this.selectedTermsArray.push(event.target.value);
   }
   else {
     let index = this.selectedTermsArray.indexOf(event.target.value);
     this.selectedTermsArray.splice(index, 1);
   }
 }

 removeField(index) {
         this.selectedTermsArray.splice(index, 1);
 }



Aucun commentaire:

Enregistrer un commentaire