我正在创建一个购物清单应用程序,每行都有食品名称。左列显示不需要的项目,右列显示所需的项目。目前,我可以通过单击每列来更改状态。但是,我想在这两个函数之间切换,以便通过单击像现在这样的列或只是行来切换状态。
HTML:
<ion-row id="test" (press)="delete(list)">
<ion-col class="offFoods" [ngClass]="{'grey-out': list.state == 'on'}" (tap)="additem(list)">
{{list.foodname}}
</ion-col>
<ion-col class="onFoods" [ngClass]="{'grey-out': list.state == 'off'}" (tap)="unlist(list)">
{{list.foodname}}
</ion-col>
</ion-row>
这些功能的TS:
additem(list) {
var listkey = this.listKey;
var customlistData = {
category: list.category,
foodname: list.foodname,
state: "on",
};
var user = firebase.auth().currentUser;
var uid = user.uid;
var foodid = list.$key;//Must click Banana
if(this.plt.is('ios')) {
this.taptic.selection();
}
if (this.plt.is('android')) {
this.vibration.vibrate(75);
}
return firebase.database().ref('userlists' + '/' + listkey + '/' + 'list' + '/' + foodid).set(customlistData);
}
unlist(list) {
var listkey = this.listKey;
var customlistData = {
category: list.category,
foodname: list.foodname,
state: "off",
};
var user = firebase.auth().currentUser;
var uid = user.uid;
var foodid = list.$key;//Must click Banana
if(this.plt.is('ios')) {
this.taptic.selection();
}
if (this.plt.is('android')) {
this.vibration.vibrate(75);
}
return firebase.database().ref('userlists' + '/' + listkey + '/' + 'list' + '/' + foodid).set(customlistData);
}
答案 0 :(得分:2)
由于列表作为参数传递,因此您可以引用当前状态并将其切换为:
additem(list) {
var listkey = this.listKey;
var customlistData = {
category: list.category,
foodname: list.foodname,
state: (list.state=="off")?"on":"off,
};
...
}
甚至可以将此函数重命名为toggleItem(list),然后将其移动到父行div:
<ion-row id="test" (press)="delete(list)" (tap)="toggleItem(list)">
...
</ion-row>
答案 1 :(得分:0)
您可以创建第三个参数或布尔数组来指示列表中每个项目的状态。
然后使用一个通用函数来操纵使用哪个函数。
changeFoodState(itemIndex: number)
{
if(state[itemIndex]){
additem(list[itemIndex])
}else {
unlist(list[itemIndex])
}
}