简体   繁体   中英

ionic2 how to get checkbox status

here is the context:

I have a list of users.

I also have a list of items and each item holds an Array of users.

The items list is displayed like that:

  <ion-list>
    <ion-item *ngFor="let item of items">
      <ion-checkbox item-left [checked]="isChecked(item)" *ngIf="selected_user;"></ion-checkbox>
      <ion-label>
        {{ item.name }}
        <br>
        <ion-badge *ngFor="let user of item.users">{{ user.name }}</ion-badge>
      </ion-label>
    </ion-item>
  </ion-list>

Basically the checkboxes are revealed when a user is selected somewhere else.

What I am trying to do is to modify for each item its users array according to the status of the checkbox.

For instance, if item_a.users contain user1 and selected_user is user1 its checkbox revealed as checked (I have that already) but now if item_b and item_c will be checked I want to add user1 to their users array and if item_a is unchecked I want to remove user1 from its users array.

Is there any event to catch here? Is there a way to get all the status together in some data structure or I have to handle it individually for each item?

Thanks.

Check the API docs here You can use ionChange event.

In your html,

 <ion-checkbox item-left [checked]="isChecked(item)" (ionChange)="insertUserToArray($event)" *ngIf="selected_user;"></ion-checkbox>

In component add function:

insertUserToArray(item){
//check item.user and do stuff
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM