Lavanya Parvathi Lavanya Parvathi - 2 months ago 168
TypeScript Question

How to get selected only single checkbox from multiple checkboxes using ionic 2 and angular2

Presently working on ionic2 and angular2
I am having fields of 7 checkboxes. I need to select only one particular checkbox the remaining checkboxes should be disabled.Similarly, I need to get for remaining 6 checkboxes also. How can I do that,
Here is the code I have used in my HTML

// payment.HTML

<ion-card class="ion-card">
<ion-item>
<ion-label>Cash On Delivery</ion-label>
<ion-checkbox item-right checked="true" [(ngModel)]="checked"></ion-checkbox>
</ion-item>

<ion-item no-lines>
<ion-label>PayuMoney Wallet</ion-label>
<ion-checkbox item-right checked="false"[(ngModel)]="checked"></ion-checkbox>
</ion-item>

<ion-item no-lines>
<ion-label>Pay with Paytm Wallet</ion-label>
<ion-checkbox item-right checked="false" [(ngModel)]="checked"></ion-checkbox>
</ion-item>

<ion-item no-lines>
<ion-label>Credit/ Debit Card</ion-label>
<ion-checkbox item-right checked="false" [(ngModel)]="checked"></ion-checkbox>
</ion-item>

<ion-item no-lines>
<ion-label>Net Banking</ion-label>
<ion-checkbox item-right checked="false" [(ngModel)]="checked"></ion-checkbox>
</ion-item>

<ion-item no-lines>
<ion-label>PayZapp</ion-label>
<ion-checkbox item-right checked="false" [(ngModel)]="checked"></ion-checkbox>
</ion-item>

<ion-item no-lines>
<ion-label>Pay using MobiKwiK Wallet</ion-label>
<ion-checkbox item-right checked="false" [(ngModel)]="checked"></ion-checkbox>
</ion-item>

</ion-card>

Answer

This might do what you want (not tested):

<ion-card class="ion-card">
  <ion-item *ngFor="let item of options; let i=index">
    <ion-label>{{item}}</ion-label>
    <ion-checkbox item-right [ngModel]="checkedIdx == i"(ngModelChange)="$event ? checkedIdx = i : checkedIdx = -1" [disabled]="checkedIdx >= 0 && checkedIdx != i"></ion-checkbox>
  </ion-item>
</ion-card>
class MyComponent {
  checkedIdx=0;

  options = [
    'Cash On Delivery',
    'PayuMoney Wallet',
    'Pay with Paytm Wallet',
    'Credit/ Debit Card',
    'Net Banking',
    'PayZapp',
    'Pay using MobiKwiK Wallet'
  ];
}
Comments