这是手风琴列表菜单,在菜单的第3层,我为每个项目都选中了一个复选框。这段代码已经从选定的项目中获取价值,但是问题是当我取消选择时,它使我不断获得价值。如何防止复选框选择同一项目,如果选中该复选框,则取消该复选框?
form.html
<!-- Third-Level -->
<ion-item *ngFor="let item of child.children" detail-none class="child-item"
text-wrap no-lines>
<ion-label>
{{ item.name }}
<p style="color: #0077ff;">
{{ item.open ? 'Item Selected' : '' }}
</p>
</ion-label>
<!-- <ion-checkbox item-end (click)="tofix(item)"></ion-checkbox> -->
<ion-checkbox item-end [(ngModel)]="item.open" (click)="tofix(item)"></ion-checkbox>
</ion-item>
form.ts
export class FormPage implements OnInit{
data: any[];
@Input('item') item: any;
constructor(
public navCtrl: NavController,
public navParams: NavParams,
private http: Http,
private toastCtrl: ToastController) {
this.http.get('assets/data/menus.json')
.map(res => res.json().items)
.subscribe(data => this.data = data);
this.title = navParams.get('title');
this.subtitle = navParams.get('subtitle');
}
toggleSection(i) {
this.data[i].open = !this.data[i].open;
}
toggleItem(i, j) {
this.data[i].children[j].open = !this.data[i].children[j].open;
}
ngOnInit() {
}
async tofix(item){
const toast = await this.toastCtrl.create({
message: `Added item to be fix : ${item.name}`,
duration: 2000
});
this.SelectedItemToFix += `${item.name}`;
toast.present();
}
ionViewDidLoad() {
console.log('ionViewDidLoad FormPage');
}
}
答案 0 :(得分:3)
您可以访问商品的open
属性,然后决定显示什么,或者是否显示小吃栏。
我还猜测红色按钮值存储在SelectedItemToFix
中,因此只有新项目将存储在按钮中。
async tofix(item){
this.SelectedItemToFix = item.open ? `${item.name}` : '';
// If you dont want to display the snackbar
// if(!item.open) return;
const toast = await this.toastCtrl.create({
message: `${item.open ? 'Added' : 'Removed'} item ${item.open ? 'to' : 'from'} be fix : ${item.name}`,
duration: 2000
});
toast.present();
}