Commit db0af85b136ddd7432bf5479db0cbc887e33167d
1 parent
d05888154c
Exists in
master
Contadores de total y cant items
Showing
2 changed files
with
45 additions
and
22 deletions
Show diff stats
src/app/components/sidebar/sidebar.component.html
| ... | ... | @@ -37,7 +37,7 @@ |
| 37 | 37 | </div> |
| 38 | 38 | <div class="col 12 p-0"> |
| 39 | 39 | <button type="button" class="btn btn-light shadow mb-2"><strong>Finalizar y pagar</strong></button> |
| 40 | - <button type="button" class="btn btn-light shadow btn-sm">Cancelar</button> | |
| 40 | + <button type="button" class="btn btn-light shadow btn-sm" (click)="clearCar()">Cancelar</button> | |
| 41 | 41 | </div> |
| 42 | 42 | </div> |
| 43 | 43 | </div> |
| 44 | 44 | \ No newline at end of file |
src/app/components/sidebar/sidebar.component.ts
| 1 | 1 | import { Component, OnInit } from '@angular/core'; |
| 2 | -import { timingSafeEqual } from 'crypto'; | |
| 2 | + | |
| 3 | 3 | |
| 4 | 4 | @Component({ |
| 5 | 5 | selector: 'app-sidebar', |
| ... | ... | @@ -43,59 +43,82 @@ export class SidebarComponent implements OnInit { |
| 43 | 43 | constructor() { } |
| 44 | 44 | |
| 45 | 45 | ngOnInit() { |
| 46 | - this.getProductosCarrito(); | |
| 46 | + | |
| 47 | 47 | this.getCantidadProductos(); |
| 48 | 48 | this.getTotal(); |
| 49 | + | |
| 49 | 50 | } |
| 50 | 51 | |
| 51 | - getCantidadProductos(){ | |
| 52 | + getCantidadProductos() { | |
| 52 | 53 | var aux = 0; |
| 53 | 54 | for (let i = 0; i < this.productos.length; i++) { |
| 54 | 55 | ++aux; |
| 55 | 56 | } |
| 56 | 57 | return this.cont = aux; |
| 57 | - | |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - getProductosCarrito() { | |
| 61 | - return this.productos; | |
| 62 | - } | |
| 60 | + getTotal() { | |
| 63 | 61 | |
| 64 | - getTotal(){ | |
| 65 | - var subTotal; | |
| 62 | + let subTotal = 0; | |
| 66 | 63 | for (let i = 0; i < this.productos.length; i++) { |
| 67 | - this.productos[i].cantidad; | |
| 64 | + subTotal = subTotal + (this.productos[i].precio * this.productos[i].cantidad); | |
| 68 | 65 | } |
| 66 | + console.log(subTotal); | |
| 67 | + return this.total = subTotal; | |
| 69 | 68 | } |
| 70 | 69 | |
| 71 | - public aumentarContador(index) { | |
| 72 | - ++this.cont; | |
| 73 | - for (let i = 0; i < this.productos.length; i++) { | |
| 70 | + public aumentarContador(index) { | |
| 71 | + | |
| 72 | + ++this.cont; | |
| 73 | + for (let i = 0; i < this.productos.length; i++) { | |
| 74 | 74 | if (i === index) { |
| 75 | + this.total = this.total + this.productos[i].precio; | |
| 75 | 76 | return (this.productos[i].cantidad === this.max) ? |
| 76 | 77 | this.productos[i].cantidad : ++this.productos[i].cantidad; |
| 77 | - | |
| 78 | + | |
| 78 | 79 | } |
| 79 | 80 | } |
| 80 | 81 | } |
| 81 | 82 | |
| 82 | 83 | decrementarContador(index) { |
| 83 | - --this.cont; | |
| 84 | + | |
| 84 | 85 | for (let i = 0; i < this.productos.length; i++) { |
| 85 | - if (i === index) { | |
| 86 | - return (this.productos[i].cantidad === this.min) ? | |
| 87 | - this.productos[i].cantidad : --this.productos[i].cantidad; | |
| 86 | + if (i === index && this.productos[i].cantidad > 1) { | |
| 87 | + --this.productos[i].cantidad; | |
| 88 | + --this.cont; | |
| 89 | + break; | |
| 88 | 90 | } |
| 89 | 91 | } |
| 92 | + | |
| 93 | + this.getTotal() | |
| 94 | + } | |
| 95 | + | |
| 96 | + setCantidadItems() { | |
| 97 | + | |
| 98 | + this.cont = 0; | |
| 99 | + for (let i = 0; i < this.productos.length; i++) { | |
| 100 | + this.cont += this.productos[i].cantidad; | |
| 101 | + } | |
| 90 | 102 | } |
| 91 | 103 | |
| 92 | 104 | deleteProducto(index) { |
| 93 | - --this.cont; | |
| 94 | - for (let i = 0; i < this.productos.length; i++) | |
| 105 | + | |
| 106 | + for (let i = 0; i < this.productos.length; i++){ | |
| 95 | 107 | if (i === index) { |
| 108 | + this.cont -= this.productos[i].cantidad; | |
| 109 | + //Elimina del total el precio de todo el item | |
| 110 | + this.total = this.total - (this.productos[i].precio * this.productos[i].cantidad); | |
| 96 | 111 | this.productos.splice(i, 1); |
| 97 | - return this.productos; | |
| 112 | + return; | |
| 98 | 113 | } |
| 114 | + } | |
| 115 | + } | |
| 99 | 116 | |
| 117 | + clearCar(){ | |
| 118 | + let arr = []; | |
| 119 | + this.productos = arr; | |
| 120 | + this.total = 0; | |
| 121 | + this.cont = 0; | |
| 100 | 122 | } |
| 123 | + | |
| 101 | 124 | } |