samedi 3 décembre 2016

Angular 2 initialize checkbox from hashmap

How does one initialize and manipulate a check box value? I've looked at quite a number of examples, but haven't been able to get any to work.

i'm trying to present a N x M table where the rows represent tasks, and the columns students. The idea is that checking one of the checkboxes in the table assigns a task to a student.

There is a typescript hash map which contains the value of all the checkboxes;

assigned : { [key:string]:boolean; } = {};  

the hash key is:

var key = a.TaskId + '_' + a.StudentId;

The table is generated with a nested ngFor:

<tr *ngFor="let t of tasks">

  <td>Task Name for task... &nbsp; &nbsp;</td>
  <td *ngFor="let s of students">

    <input type="checkbox" name=#_ change="onAssignmentChange(t,s)" [checked]="cbValue(t, s)">

  </td>
</tr>

the cbValue(t, s) looks like:

cbValue(taskItem, studentItem) {
   var key =  taskItem.TaskId + '_' +studentItem.StudentId;
   return this.assigned[key];
}

This doesn't work, all the checkboxes in the table come up unchecked, no matter what the values in the hash.

I've also tried:

    <input type="checkbox"  change="onAssignmentChange(t,s)" [checked]="cbValue(t, s)">

    <input type="checkbox"  change="onAssignmentChange(t,s)" [(ngModel)]=+'_'+ >

    <input type="checkbox"  change="onAssignmentChange(t,s)" [(ngModel)]="assigned[t.TaskId"+'_'+"s.StudentId"]>

none of which works.

I seem to be quite in the dark here; 'onAssignmentChange' doesn't get hit either, even though no errors show up in the console.

Also,

...  name=#_  ...

is this supposed to be a local target or something?

thanks in advance




Aucun commentaire:

Enregistrer un commentaire