Shreyas Pednekar
Shreyas Pednekar

Reputation: 1305

Ionic 2- how to get checkbox selected values inside variable/array on button click?

I have gridview in which I have a list of email ids. I want to get checkbox selected email ids in an array or a variable on button click. I'm new to this technology, Please help.

enter image description here

Here is my sendmail.html code:

<div>
<ion-grid>
  <ion-row wrap class="gridHeading">
    <ion-col col-2 class="gridHeading" align="center"></ion-col>
    <ion-col col-10 class="gridHeading" align="center">CLIENT EMAIL</ion-col>
  </ion-row>
  <ion-row wrap *ngFor="let mail of custEmailIDs" class="gridCol">
    <ion-col col-2 class="gridCol"><ion-checkbox [(ngModel)]="mail.checked"></ion-checkbox></ion-col>
    <ion-col col-10 class="gridCol">{{ mail.Email_ID }}</ion-col>
  </ion-row>
</ion-grid>

<button
ion-button
full
color="other">Send</button>

also tell me what and how to bind values to checkbox so that I can get those email ids on button click. Thanks in advance.

Upvotes: 0

Views: 1509

Answers (1)

Mahi Parmar
Mahi Parmar

Reputation: 525

Here is the example code for your requirement Your html file

<ion-list *ngFor="let item of items">
      <ion-col width-50>
        <ion-checkbox (click)="clickSelectBox(item)"></ion-checkbox>
    </ion-col>
    <ion-col width-50>
          {{item.event_name}}
    </ion-col>
   </ion-list>

Your .ts file

selectedQuestions:string[] = [];

  clickSelectBox(itemKey){
    console.log(itemKey);
     const foundAt = this.selectedQuestions.indexOf(itemKey);
     console.log(foundAt);
     if (foundAt >= 0) {
        this.selectedQuestions.splice(foundAt, 1);
     } else {
        this.selectedQuestions.push(itemKey);
    }
    console.log(this.selectedQuestions);

}

try this and let me know,Hope THIS Will Work

Upvotes: 1

Related Questions