Show Selected Quantities

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 number of items entered, which in this case are pizza flavors.

<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>
    
asked by anonymous 30.07.2018 / 16:13

1 answer

0

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

Create a function that cycles through 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 the function so that it shows the 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>
    
01.08.2018 / 13:11