Mostar Selected Quantities

Asked

Viewed 28 times

0

changeQty(item, varia, valor){

  let eMax = 0;
  for (var i = 0; i < this.selectedVariation.length; ++i) {
    if (this.selectedVariation[i].idPV == item.idPV) {
      // console.log(this.selectedVariation[i]);
      eMax++;
    }
  }

  if (valor > 0) {
    if (eMax < varia.eMax) {
      this.selectedVariation.push(item);
    }else{
      console.log('Número máximo excedido');
    }
  }else if (valor < 0){

    let index = this.selectedVariation.findIndex(indice => indice.idPVI === item.idPVI);
    // console.log(index);
    if (index >= 0) {
      this.selectedVariation.splice(index, 1);
    }
  }

  console.log(this.selectedVariation);

}

I would like to display the amount of items entered, which in this case are flavors of pizzas.

<ion-list *ngIf="varia.eMax > 1">
            <ng-container *ngFor="let item of itensVaria">
                <ion-item class="compact" *ngIf="item.idPV == varia.idPV">
                  <ion-row no-padding>
                    <ion-col col-7 style="text-align: left;">
                      <h2 class="h2">{{item.descri}}</h2>
                      <strong class="danger">+ R$ {{money(item.preco)}}</strong>
                    </ion-col>

                    <ion-col col-5>
                      <button ion-button icon-only clear color="danger" [disabled]="!saborJaAdicionado(item)" (click)="changeQty(item, varia, -1)">
                        <ion-icon name="remove-circle"></ion-icon>
                      </button>

                      <!-- <button ion-button clear color="danger"> qtd do item </button> -->

                      <button ion-button icon-only clear color="danger" [disabled]="saborJaAdicionado(item)" (click)="changeQty(item, varia, 1)">
                        <ion-icon name="add-circle"></ion-icon>
                      </button>
                    </ion-col>
                  </ion-row>

                </ion-item>
            </ng-container>

        </ion-list>
  • Sig you want to display the total number of inserted flavors or for a particular flavor?

  • For a certain flavor because there is a "*ngFor", has a commented part in html that is where I want to display this information!

1 answer

0


The idea of the solution is basically the same as the one you did to disable the button.

Creates a function that traverses selectedVariation by passing the item and makes a Count of how many times it appears in the list.

countItems(item){
  let totalItems = 0;
  this.selectedVariation.foreach(e => {
    if (e === item) 
      totalItems++;
  }

 return totalItems;

}

In Html Pass function to display value

<ion-col col-5>
                      <button ion-button icon-only clear color="danger" [disabled]="!saborJaAdicionado(item)" (click)="changeQty(item, varia, -1)">
                        <ion-icon name="remove-circle"></ion-icon>
                      </button>

                       {{ countItems(item) }}

                      <button ion-button icon-only clear color="danger" [disabled]="saborJaAdicionado(item)" (click)="changeQty(item, varia, 1)">
                        <ion-icon name="add-circle"></ion-icon>
                      </button>
                    </ion-col>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.