<div *ngIf="testList.length > 0">
<ion-item *ngFor="let member of testList">
<ion-label>{{member?.testName || 'N/A'}}</ion-label>
<ion-checkbox color="dark" [(ngModel)]="member.checked"></ion-checkbox>
</ion-item>
</div>
[{testID: 1, testName: " test1", testAmount: "500", testDescription: ""},
{testID: 2, testName: " test2", testAmount: "500", testDescription: ""},
{testID: 3, testName: "dgdfgd", testAmount: "150", testDescription: ""},
{testID: 4, testName: "UricAcid", testAmount: "200", testDescription: ""}]
<button (click)="gettstdata()">Done</button>
[完了]ボタンをクリックしたときに複数選択などのすべてのチェックボックス値を取得する方法
<div *ngIf="testList.length > 0">
<ion-item *ngFor="let member of testList">
<ion-label>{{member?.testName || 'N/A'}}</ion-label>
<ion-checkbox color="dark" (click)="selectMember(member)" [(ngModel)]="member.checked"></ion-checkbox>
</ion-item>
</div>
<button ion-button (click)="checkAll()">Done</button>
tsで:
testList: any = [
{testID: 1, testName: " test1", checked: false},
{testID: 2, testName: " test2", checked: false},
{testID: 3, testName: "dgdfgd", checked: false},
{testID: 4, testName: "UricAcid", checked: false}
]
selectedArray :any = [];
checkAll(){
for(let i =0; i <= this.testList.length; i++) {
this.testList[i].checked = true;
}
console.log(this.testList);
}
selectMember(data){
if (data.checked == true) {
this.selectedArray.Push(data);
} else {
let newArray = this.selectedArray.filter(function(el) {
return el.testID !== data.testID;
});
this.selectedArray = newArray;
}
console.log(this.selectedArray);
}
@Patricio Vargasの回答は機能していますが、(ionChange)
の代わりに(change)
を使用する必要があります
また、ifステートメントの条件を(event.target.checked)
から(event.checked)
に変更します
<div *ngIf="testList.length > 0">
<ion-item *ngFor="let member of testList">
<ion-label>{{member?.testName || 'N/A'}}</ion-label>
<ion-checkbox color="dark" (ionChange)="addCheckbox($event,member.testName)"></ion-checkbox>
</ion-item>
</div>
<button (click)='getCheckedBoxes()'>Done</button>
.tsファイル内
checked = [];
//Adds the checkedbox to the array and check if you unchecked it
addCheckbox(event, checkbox : String) {
if ( event.checked ) {
this.checked.Push(checkbox);
} else {
let index = this.removeCheckedFromArray(checkbox);
this.checked.splice(index,1);
}
}
//Removes checkbox from array when you uncheck it
removeCheckedFromArray(checkbox : String) {
return this.checked.findIndex((category)=>{
return category === checkbox;
})
}
//Empties array with checkedboxes
emptyCheckedArray() {
this.checked = [];
}
getCheckedBoxes() {
//Do whatever
console.log(this.checked);
}
これを実現する方法はいくつかあります。
-使用(変更)できます。チェックボックスを操作するときに一般的なものに必要ないくつかの追加のメソッドを記述しました
<div *ngIf="testList.length > 0">
<ion-item *ngFor="let member of testList">
<ion-label>{{member?.testName || 'N/A'}}</ion-label>
<ion-checkbox color="dark" (change)="addCheckbox($event,member.testName)"></ion-checkbox>
</ion-item>
</div>
<button (click)='getCheckedBoxes()'>Done</button>
.tsファイル内
checked = [];
//Adds the checkedbox to the array and check if you unchecked it
addCheckbox(event, checkbox : String) {
if ( event.target.checked ) {
this.checked.Push(checkbox);
} else {
let index = this.removeCheckedFromArray(checkbox);
this.checked.splice(index,1);
}
}
//Removes checkbox from array when you uncheck it
removeCheckedFromArray(checkbox : String) {
return this.checked.findIndex((category)=>{
return category === checkbox;
})
}
//Empties array with checkedboxes
emptyCheckedArray() {
this.checked = [];
}
getCheckedBoxes() {
//Do whatever
console.log(this.checked);
}