【问题标题】:Ionic 2 how to check all checkboxes on button click and get checked checkboxes' valueIonic 2如何在按钮单击时检查所有复选框并获取选中复选框的值
【发布时间】:2017-03-31 04:29:59
【问题描述】:

我在 Angular 2 和 Ionic 2 中相当新,我有一个小问题,任何人都可以告诉我如何检查按钮单击时的所有复选框吗?以及如何在另一个按钮单击时获取值 oc 选中的复选框。

这是我的 html 的样子

<div class="card item-icon-right"  *ngFor="let item of items" >
    <ion-checkbox *ngIf="showCheckBoxes" [(ngModel)]="item.id" mode="ios"></ion-checkbox>
</div>
<button class="button button-calm">Check All</button>
<button class="button button-primary">Get selected Values</button>

谢谢

【问题讨论】:

    标签: angular ionic2


    【解决方案1】:

    你可以有两个复选框元素

    1. 全选 - 一键生成所有元素(只需切换)
    2. 选择特定的

      <div class="card item-icon-right"  *ngFor="let item of items" >
           <ion-checkbox *ngIf="showCheckBoxes && selectedAll" [(ngModel)]="item.id" mode="ios" [checked]="selectedAll"></ion-checkbox>
      
           <ion-checkbox *ngIf="showCheckBoxes && !selectedAll" [(ngModel)]="item.id" mode="ios" ></ion-checkbox>
      </div>
      

      检查所有 获取选定的值

      checkAll(){
          this.selectedAll=true;
          console.log(this.items);
      }
      

    当您选择所有项目时,您将使用整个数组。

    【讨论】:

    • 谢谢,你能解释一下为什么循环中有两个 组件吗??
    • 答案中的前两行解释了这一点。
    • 是的,谢谢,如何获取选中复选框的项目 ID 数组?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 2021-12-06
    • 2015-12-20
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 2014-08-07
    相关资源
    最近更新 更多