这是我的代码,一旦选中/取消选中select all ion-option,我想检查/取消选中我选择弹出窗口中的所有选项。
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>
答案 0 :(得分:0)
只需在选中的itens中插入所有的itens
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;
}
在select处输入selectAll值
<ion-option (ionSelect)="selectAllItems()" [value]="selectAll">
Select All
</ion-option>