这是我的代码,一旦选中/取消选中 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>
最佳答案
只需在选定的项目中插入项目的所有 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/