0

I am creating a grocery list app that has a food food name on each row. The left column displays items that are not needed and the right column displays items that are needed. Currently I can change the state by clicking on each column. However, I'd like to toggle between these two functions so that switching the state works by clicking on the column like it does now, or just the row.

The 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>

The TS for these functions:

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);
}

2 Answers 2

2

Since list is passed as a parameter, then you can reference the current state and toggle it like so:

additem(list) {
var listkey = this.listKey;
var customlistData = {
  category: list.category,
  foodname: list.foodname,
  state: (list.state=="off")?"on":"off,
};

...

}

Could even rename this function to toggleItem(list) and then move it to the parent row div:

<ion-row id="test" (press)="delete(list)" (tap)="toggleItem(list)">
...
</ion-row>
Sign up to request clarification or add additional context in comments.

Comments

0

You can create a third parameter, or a boolean array to indicate what state each item on the list is.

Then use one general function to maneuver which function to use.

   changeFoodState(itemIndex: number)
   {
      if(state[itemIndex]){
         additem(list[itemIndex])
      }else {
         unlist(list[itemIndex])
      }
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.