一个学生想要注册一个班级或一个以上班级。由于班级列表是动态的,因此我使用ngFor以带有复选框的表格式显示,然后选择了班级。但是,不知道如何在学生复选框选择班级时获得价值。
我尝试使用select选项,但是操作不顺利。
Suspense
我希望当学生注册任何班级时,班级的容量将减少一并更新到Firebase数据库。
一个学生想要注册一个班级或一个以上班级。由于班级列表是动态的,因此我使用ngFor以带有复选框的表格式显示,然后选择了班级。但是,不知道如何在学生复选框选择班级时获得价值。
我尝试使用select选项,但是操作不顺利。
Suspense
我希望当学生注册任何班级时,班级的容量将减少一并更新到Firebase数据库。
由于有多个复选框,因此需要一个数组来跟踪这些复选框。
组件:
checkboxes = [];
使用false
初始化所有复选框,以使它们在开始时未被选中。
ngOnInit() {
// This array will be the same length as the iterable object used in *ngFor.
this.checkboxes = new Array(this.classItem.length).fill(false);
}
模板:
在模板中,将每个复选框绑定到各自的值。
<tr *ngFor="let item of classItem; let i = index">
...
<ion-checkbox style="text-align: center;" (onchange)="checkboxChange(i)"
[(ngModel)]="checkboxes[i]"></ion-checkbox>
...
</tr>
选中或取消选中复选框时,请修改班级的可用容量。
checkboxChange(i) {
if (this.checkboxes[i]) {
this.capacity++;
} else {
this.capacity--;
}
}
,
这是我为您解决的问题。首先,我在具有ClassDescription接口的ClassDescription类型的checkItem数组中添加了带有模拟数据的ts文件文件。
请注意,我改用了普通的html input复选框,我刚刚在ionic文档上进行了验证,它的工作原理与下面对Ionic Checkbox
的引用相同
<table align="center" >
<tr class="heading" >
<th style="padding: 5px;">Code</th>
<th style="padding: 5px;">Name</th>
<th style="padding: 5px;">Capacity</th>
<th style="padding: 5px;">Remaining</th>
<th style="padding: 5px;">Location</th>
<th style="padding: 5px;">Instructor</th>
<th style="padding: 5px;">Register?</th>
</tr>
<tr *ngFor="let item of classItem">
<td style="border-right-style: dashed;">{{ item.code }}</td>
<td>{{ item.name }}</td>
<td>{{ item.capacity }}</td>
<td>{{ item.remaining }}</td>
<td>{{ item.location }}</td>
<td>{{ item.instructor }}</td>
<td>
<input type="checkbox" [(ngModel)]="item.register" (change)="onChecked(item)"
[disabled]="(item.remaining === 0) && item.register == !true">
<br>
</td>
</tr>
</table>
我的Stackblitz解决方案在这里Stackblitz solution
。接下来,您可以在onChecked函数中为firebase添加逻辑。
import { Component } from '@angular/core';
interface ClassDescription {
code : string;
name : string ;
capacity: string ;
remaining : number ;
location : string ;
instructor : string;
register: boolean;
}
@Component({
selector: 'my-app',templateUrl: './app.component.html',styleUrls: [ './app.component.css' ]
})
export class AppComponent {
onChecked(item) {
if(item.register)
{
item.remaining = item.remaining - 1 ;
}
else{
item.remaining = item.remaining + 1 ;
}
}
classItem: ClassDescription[]= [
{
'code' : '101','name' : 'Micro services','capacity': '30','remaining' : 5,'location' : 'Engineering dept','instructor' : 'Dr. Neha','register': false
},{
'code' : '102','name' : 'Computer Science','remaining' : 0,'location' : 'Mcarthy hall','instructor' : 'Dr. Rob','register': false
},{
'code' : '103','name' : 'Programming fundamental','location' : 'Harvard hall','instructor' : 'Dr. Steven','register': false
}
];
}
,
<table align="center" >
<tr class="heading" >
<th style="padding: 5px;">Code</th>
<th style="padding: 5px;">Name</th>
<th style="padding: 5px;">Capacity</th>
<th style="padding: 5px;">Remaining</th>
<th style="padding: 5px;">Location</th>
<th style="padding: 5px;">Instructor</th>
<th style="padding: 5px;">Register?</th>
</tr>
<tr *ngFor="let item of classItem;let i = index">
<td style="border-right-style: dashed;">{{ item.code }}</td>
<td>{{ item.name }}</td>
<td>{{ item.capacity }}</td>
<td>{{ item.remaining }}</td>
<td>{{ item.location }}</td>
<td>{{ item.instructor }}</td>
<td>
<ion-checkbox style="text-align: center;" (ionChange)="checkboxStateChanged(i)" [checked]="item.checked"></ion-checkbox>
</td>
</tr>
</table>
您的提示:
classSelectedIndexes : number[] = []; //storing the selected item indexes just in case
checkboxStateChanged(index : number){
this.item[i].checked= !this.item[i].checked;
if(this.item[i].checked){
//do something if true
}
else{
//do something if false
}
}
您的checked
中应该有一个classItem
属性,所有属性都初始化为false
即您的classItem
的类型应为
interface ClassType {
code : string;
name : string ;
capacity: string ;
remaining : number ;
location : string ;
instructor : string;
checked: boolean;
}