typescript - 选中/未选中时如何选中/取消选中ion-select中的所有选项选择ionic3中的所有选项

标签 typescript ionic3 angular5

这是我的代码,一旦选中/取消选中 select all ion-option,我想使用它来选中/取消选中我的 select pop 中的所有选项。

items:any =[
      {id:1, value:"Apple"}
      {id:2, value:"Banana"}
      {id:3, value:"Stawberry"}
      {id:4, value:"PineApple"}
      {id:5, value:"Grapes"}
      ];
    selectedItems:any;
    selectAll:boolean;
    
    /**
     * This select all the items in the ion-select  popup
     **/
    selectAllItems() {
      if(!selectAll) {
        this.items.filter(Obj => {
          this.selectedItems.push(Obj.id);
        });
        this.selectedItems.push(0);
        this.selectAll = true;
      } else {
        this.selectedItems = this.items[0].id;
      }
    }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<ion-item>
        <ion-label>Multiselect</ion-label>
        <ion-select [(ngModel)]="selectedItems">
          <ion-option (ionSelect)="selectAllItems()" [value]="0">
            Select All
          </ion-option>
          <ion-option *ngFor="let item of items" [value]="item.id">
            {{item.value}}
          </ion-option>
        </ion-select>
      </ion-item>
当我使用 (ionSelect) 时,模型正在更改,但所有选项都没有被选中,当我取消选中时,ionSelect 事件没有被触发。 这是在 ionic-3 中完成的。任何人都可以帮助我。提前致谢

最佳答案

只需在选定的项目中插入项目的所有 ID

constructor(private ngZone: NgZone){
}

selectAllItems() {
    if (!selectAll) {
      this.ngZone.run(() => {
        for (let item of items) {
          this.selectedItems.push(item.id)
          this.selectAll = true;
        }
      })
    } else {
      this.selectedItems = [];
      this.selectAll = false;
    }

然后在选择处放置 selectAll 值

<ion-option (ionSelect)="selectAllItems()" [value]="selectAll">
            Select All
          </ion-option>

关于typescript - 选中/未选中时如何选中/取消选中ion-select中的所有选项选择ionic3中的所有选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50171402/

相关文章:

angularjs - typescript 错误没有成员

angular - 如何处理( ionic 选择)确定按钮?

android - 在 Ionic2/3 项目中哪里可以找到 SQLite 数据库?

javascript - 如何在 ionic 3 中将视频文件从 ios 转换为 base64

xml - Ionic 3 项目没有 config.xml

javascript - 如何在 Angular 6 中使用响应式(Reactive)表单隐藏字段后提交表单?

javascript - Typescript 无法正确编译为 commonjs/es2015

Angular 5 按日期排序

css - Angular 5 material flex-layout fxLayoutAlign ="space-evenly center"不工作

angular - Typescript 中的 IIFE 函数