Commit 4384b790cfe9be0594a1111e56b2e8f63857df6a
1 parent
afc1dce764
Exists in
master
and in
1 other branch
Funcion incrrementar
Showing
2 changed files
with
53 additions
and
47 deletions
Show diff stats
src/app/sidebar/sidebar.component.html
| ... | ... | @@ -18,9 +18,9 @@ |
| 18 | 18 | <button type="button" class="btn btn-light btn-sm float-left my-auto" (click)="deleteProducto(i)">X</button> |
| 19 | 19 | <button type="button" class="btn btn-light btn-sm my-auto">Y</button> |
| 20 | 20 | <div class="btn-group-sm btn-group float-right my-auto" role="group" aria-label="Basic example"> |
| 21 | - <button type="button" class="btn btn-light btn-sm mb-2" (click)="aumentarContador(prod.cantidad)">+</button> | |
| 21 | + <button type="button" class="btn btn-light btn-sm mb-2" (click)="aumentarContador(i)">+</button> | |
| 22 | 22 | <label for="" class="border border.white px-1">{{prod.cantidad}}</label> |
| 23 | - <button type="button" class="btn btn-light btn-sm mb-2" (click)="decrementarContador()">-</button> | |
| 23 | + <button type="button" class="btn btn-light btn-sm mb-2" (click)="decrementarContador(i)">-</button> | |
| 24 | 24 | </div> |
| 25 | 25 | </div> |
| 26 | 26 | </div> |
| ... | ... | @@ -30,9 +30,10 @@ |
| 30 | 30 | <div class="card mt-auto blue-gradient border-0"> |
| 31 | 31 | <div class="card-body row"> |
| 32 | 32 | <div class="col"> |
| 33 | - <h4 class="border-bottom border-secondary text-secondary">(1) item</h4> | |
| 33 | + <h4 class="border-bottom border-secondary text-secondary" *ngIf="cont === 1">({{cont}}) item</h4> | |
| 34 | + <h4 class="border-bottom border-secondary text-secondary" *ngIf="cont > 1">({{cont}}) items</h4> | |
| 34 | 35 | <h3 class="text-secondary">Total</h3> |
| 35 | - <h3 class="text-dark"><strong>{{5000 | currency}}</strong></h3> | |
| 36 | + <h3 class="text-dark"><strong>{{total | currency}}</strong></h3> | |
| 36 | 37 | </div> |
| 37 | 38 | <div class="col 12 p-0"> |
| 38 | 39 | <button type="button" class="btn btn-light shadow mb-2"><strong>Finalizar y pagar</strong></button> |
src/app/sidebar/sidebar.component.ts
| 1 | 1 | import { Component, OnInit } from '@angular/core'; |
| 2 | +import { timingSafeEqual } from 'crypto'; | |
| 2 | 3 | |
| 3 | 4 | @Component({ |
| 4 | 5 | selector: 'app-sidebar', |
| ... | ... | @@ -9,7 +10,8 @@ export class SidebarComponent implements OnInit { |
| 9 | 10 | |
| 10 | 11 | private cont: number = 1; |
| 11 | 12 | private min: number = 1; |
| 12 | - private max: number =50; | |
| 13 | + private max: number = 50; | |
| 14 | + private total: number = 0; | |
| 13 | 15 | |
| 14 | 16 | private productos = [ |
| 15 | 17 | { |
| ... | ... | @@ -41,59 +43,62 @@ export class SidebarComponent implements OnInit { |
| 41 | 43 | constructor() { } |
| 42 | 44 | |
| 43 | 45 | ngOnInit() { |
| 44 | - this.getProductosCarrito(); | |
| 46 | + this.getProductosCarrito(); | |
| 47 | + this.getCantidadProductos(); | |
| 48 | + this.getTotal(); | |
| 45 | 49 | } |
| 46 | 50 | |
| 47 | - getProductosCarrito(){ | |
| 51 | + getCantidadProductos(){ | |
| 52 | + var aux = 0; | |
| 53 | + for (let i = 0; i < this.productos.length; i++) { | |
| 54 | + ++aux; | |
| 55 | + } | |
| 56 | + return this.cont = aux; | |
| 57 | + | |
| 58 | + } | |
| 59 | + | |
| 60 | + getProductosCarrito() { | |
| 48 | 61 | return this.productos; |
| 49 | - | |
| 50 | 62 | } |
| 51 | 63 | |
| 52 | - public aumentarContador(cant){ | |
| 53 | - console.log("llego"); | |
| 54 | - for (const i of this.productos) { | |
| 55 | - if (cant === this.max) { | |
| 56 | - return i.cantidad; | |
| 57 | - } else{ | |
| 58 | - ++i.cantidad | |
| 59 | - return i.cantidad; | |
| 60 | - } | |
| 61 | - } | |
| 62 | - | |
| 63 | - // if (this.cont === this.max) { | |
| 64 | - // this.cont; | |
| 65 | - // return this.cont; | |
| 66 | - // } else { | |
| 67 | - // ++this.cont; | |
| 68 | - // return this.cont; | |
| 69 | - // } | |
| 70 | - | |
| 71 | - } | |
| 72 | - aumentarContador2(index){ | |
| 73 | - for (let i = 0; i < this.productos.length; i++) | |
| 64 | + getTotal(){ | |
| 65 | + var subTotal; | |
| 66 | + for (const pr of this.productos) { | |
| 67 | + subTotal = pr.cantidad * pr.precio; | |
| 68 | + } | |
| 69 | + this.total = this.total + subTotal; | |
| 70 | + this.total = this.total * this.cont; | |
| 71 | + return this.total; | |
| 72 | + } | |
| 73 | + | |
| 74 | + public aumentarContador(index) { | |
| 75 | + ++this.cont; | |
| 76 | + for (let i = 0; i < this.productos.length; i++) { | |
| 74 | 77 | if (i === index) { |
| 75 | - for (const pr of this.productos) { | |
| 78 | + return (this.productos[i].cantidad === this.max) ? | |
| 79 | + this.productos[i].cantidad : ++this.productos[i].cantidad; | |
| 76 | 80 | |
| 77 | - } | |
| 78 | - } | |
| 79 | - } | |
| 81 | + } | |
| 82 | + } | |
| 83 | + } | |
| 80 | 84 | |
| 81 | - decrementarContador(){ | |
| 82 | - if (this.cont === this.min) { | |
| 83 | - this.cont; | |
| 84 | - return this.cont; | |
| 85 | - } else { | |
| 86 | - --this.cont; | |
| 87 | - return this.cont; | |
| 85 | + decrementarContador(index) { | |
| 86 | + --this.cont; | |
| 87 | + for (let i = 0; i < this.productos.length; i++) { | |
| 88 | + if (i === index) { | |
| 89 | + return (this.productos[i].cantidad === this.min) ? | |
| 90 | + this.productos[i].cantidad : --this.productos[i].cantidad; | |
| 91 | + } | |
| 88 | 92 | } |
| 89 | 93 | } |
| 90 | 94 | |
| 91 | - deleteProducto(index){ | |
| 92 | - for (let i = 0; i < this.productos.length; i++) | |
| 95 | + deleteProducto(index) { | |
| 96 | + --this.cont; | |
| 97 | + for (let i = 0; i < this.productos.length; i++) | |
| 93 | 98 | if (i === index) { |
| 94 | - this.productos.splice(i,1); | |
| 95 | - return this.productos ; | |
| 96 | - } | |
| 97 | - | |
| 99 | + this.productos.splice(i, 1); | |
| 100 | + return this.productos; | |
| 101 | + } | |
| 102 | + | |
| 98 | 103 | } |
| 99 | 104 | } |