我想在切换时建立一个对象数组
var data = [
{ catId : Vegitarian,
subcats :[
{
category : "potatoes",
rank : 23,
catId : "Vegitarian"
},
{
category : "tomatoes",
rank : 2,
catId : "Vegitarian"
}
]
},
{ catId : "Non-vegetarian",
subcats :[
{
category : "appetizers",
rank : 3,
catId : "Non-vegetarian"
},
{
category : "nonveg-appitizers",
rank : 52,
catId : "Non-vegetarian"
}
]
}
]
<div *ngFor="let cats of data">
<ion-row >
<ion-toggle [(ngModel)]="cats.checked" (ionChange)="checkCats()"></ion-toggle>
</ion-row>
<div *ngFor="let subCats of cats.subcats">
<ion-toggle [(ngModel)]="subCat.checked" (ionChange)="checkCats()"></ion-toggle>
</div>
</div>
我的要求是当我切换cats.checked
时,下面所有subcats
也必须选择,当我取消选择cats.checked
时,所有subCats
也有取消选择到这里我的代码工作正常,但当我取消选择单subCats
时,重新cats.checked
必须取消选择,如果我再次选择猫然后关联所有subCats啊选择
this.result=[];
checkCats(catObj) {
this.result = this.data.filter(cats => {
if ((cats.checked == true) && (cats.subcats.length != 0)) {
cats.subcats.filter(subCat => {
subCat.checked = true
})
return cats
} else if ((cats.checked == true) && (cats.subcats.length == 0)) {
return cats
} else if ((cats.checked == false) && (cats.subcats.length != 0)) {
cats.subcats.filter(subCat => {
subCat.checked = false
})
}
})
}
potatoes
,我也会获得整个对象
[
{
"catId": "vegetarian",
"subcats": [
{
"subCatId": "potatoes",
"catId": "vegetarian",
"rank ": 23,
"checked": true
},
{
"subCatId": "tomatoes",
"rank ": 2,
"catId": "vegetarian",
"checked": true
}
]
}
]