如何在 Ionic 3 中获取 ion-checkbox 多个 select 值
How to get ion-checkbox multiple select value in Ionic 3
<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>
如何在单击“完成”按钮时获取所有复选框值,例如 multi select
有几种方法可以实现这一点。
-你可以使用(改变),我给你写了一些额外的方法,当你使用复选框时,你会需要一些常见的东西
<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);
}
<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" [(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>
如何在单击“完成”按钮时获取所有复选框值,例如 multi select
有几种方法可以实现这一点。
-你可以使用(改变),我给你写了一些额外的方法,当你使用复选框时,你会需要一些常见的东西
<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);
}
<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);
}