mercredi 29 juillet 2020

check checkbox only if values in array A exist in array B

I'm checking through an array to check if some selected values exists in there then a checkbox should be checked. Though the values are found in there, the checkbox it's not checked. When I log the results, its able to produce the correct response ('hello') in the console meaning the code works perfectly but I don't know why its not able to check the checkbox

this.selected = [ 1, 8032, 8042, 8047, 8021, 7986];

this.srvresp = [{id:11, name:'AA'}, {id:8032, name:'BB'}, {id:10, name:'CC'}, {id:409, name:'DD'}, {id:300, name:'EE'} ,{id:302, name:'FF'}, {id:478, name: 'GG'}, {id:8042, name: 'HH'}, {id:8047, name:'II'}, {id:8021, name:'JJ'}, {id:7986, name:'KK'}];

for (const  s of this.srvresp) {
  for (const  selected of this.selected) {
    if (s.id == selected) {
      console.log('hello')
      s.checked = true;
    }
  }
}

HTML

<div *ngFor="let item of srvresp; index as ind">
 <input [(ngModel)]="item.checked" formControlName="selectedStudents" [value]="item.id" type="checkbox" class="custom-control-input" id="-item-">

</div>



Aucun commentaire:

Enregistrer un commentaire