Commit 4ae1292d7432c0e4596a0c8a54794ab7f5f68635
1 parent
db0af85b13
Exists in
master
and in
1 other branch
Arreglos minimos
Showing
1 changed file
with
5 additions
and
6 deletions
Show diff stats
src/app/components/sidebar/sidebar.component.ts
| 1 | import { Component, OnInit } from '@angular/core'; | 1 | import { Component, OnInit } from '@angular/core'; |
| 2 | 2 | ||
| 3 | 3 | ||
| 4 | @Component({ | 4 | @Component({ |
| 5 | selector: 'app-sidebar', | 5 | selector: 'app-sidebar', |
| 6 | templateUrl: './sidebar.component.html', | 6 | templateUrl: './sidebar.component.html', |
| 7 | styleUrls: ['./sidebar.component.scss'] | 7 | styleUrls: ['./sidebar.component.scss'] |
| 8 | }) | 8 | }) |
| 9 | export class SidebarComponent implements OnInit { | 9 | export class SidebarComponent implements OnInit { |
| 10 | 10 | ||
| 11 | private cont: number = 1; | 11 | private cont: number = 1; |
| 12 | private min: number = 1; | 12 | private min: number = 1; |
| 13 | private max: number = 50; | 13 | private max: number = 50; |
| 14 | private total: number = 0; | 14 | private total: number = 0; |
| 15 | 15 | ||
| 16 | private productos = [ | 16 | private productos = [ |
| 17 | { | 17 | { |
| 18 | "id": 1, | 18 | "id": 1, |
| 19 | "desc": "Galletas Oreo", | 19 | "desc": "Galletas Oreo", |
| 20 | "cod": 225412, | 20 | "cod": 225412, |
| 21 | "precio": 15, | 21 | "precio": 15, |
| 22 | "img": "./assets/descarga.jpg", | 22 | "img": "./assets/descarga.jpg", |
| 23 | "cantidad": 1 | 23 | "cantidad": 1 |
| 24 | }, | 24 | }, |
| 25 | { | 25 | { |
| 26 | "id": 2, | 26 | "id": 2, |
| 27 | "desc": "Coca cola 500ml", | 27 | "desc": "Coca cola 500ml", |
| 28 | "cod": 512632, | 28 | "cod": 512632, |
| 29 | "precio": 40, | 29 | "precio": 40, |
| 30 | "img": "./assets/descarga.jpg", | 30 | "img": "./assets/descarga.jpg", |
| 31 | "cantidad": 1 | 31 | "cantidad": 1 |
| 32 | }, | 32 | }, |
| 33 | { | 33 | { |
| 34 | "id": 3, | 34 | "id": 3, |
| 35 | "desc": "Pancho grande", | 35 | "desc": "Pancho grande", |
| 36 | "cod": 775987, | 36 | "cod": 775987, |
| 37 | "precio": 45, | 37 | "precio": 45, |
| 38 | "img": "./assets/descarga.jpg", | 38 | "img": "./assets/descarga.jpg", |
| 39 | "cantidad": 1 | 39 | "cantidad": 1 |
| 40 | } | 40 | } |
| 41 | ]; | 41 | ]; |
| 42 | 42 | ||
| 43 | constructor() { } | 43 | constructor() { } |
| 44 | 44 | ||
| 45 | ngOnInit() { | 45 | ngOnInit() { |
| 46 | 46 | ||
| 47 | this.getCantidadProductos(); | 47 | this.getCantidadProductos(); |
| 48 | this.getTotal(); | 48 | this.getTotal(); |
| 49 | 49 | ||
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | getCantidadProductos() { | 52 | getCantidadProductos() { |
| 53 | var aux = 0; | 53 | var aux = 0; |
| 54 | for (let i = 0; i < this.productos.length; i++) { | 54 | for (let i = 0; i < this.productos.length; i++) { |
| 55 | ++aux; | 55 | ++aux; |
| 56 | } | 56 | } |
| 57 | return this.cont = aux; | 57 | return this.cont = aux; |
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | getTotal() { | 60 | getTotal() { |
| 61 | 61 | ||
| 62 | let subTotal = 0; | 62 | let subTotal = 0; |
| 63 | for (let i = 0; i < this.productos.length; i++) { | 63 | for (let i = 0; i < this.productos.length; i++) { |
| 64 | subTotal = subTotal + (this.productos[i].precio * this.productos[i].cantidad); | 64 | subTotal = subTotal + (this.productos[i].precio * this.productos[i].cantidad); |
| 65 | } | 65 | } |
| 66 | console.log(subTotal); | 66 | console.log(subTotal); |
| 67 | return this.total = subTotal; | 67 | return this.total = subTotal; |
| 68 | } | 68 | } |
| 69 | 69 | ||
| 70 | public aumentarContador(index) { | 70 | public aumentarContador(index) { |
| 71 | 71 | ||
| 72 | ++this.cont; | 72 | ++this.cont; |
| 73 | for (let i = 0; i < this.productos.length; i++) { | 73 | for (let i = 0; i < this.productos.length; i++) { |
| 74 | if (i === index) { | 74 | if (i === index) { |
| 75 | this.total = this.total + this.productos[i].precio; | 75 | this.total = this.total + this.productos[i].precio; |
| 76 | return (this.productos[i].cantidad === this.max) ? | 76 | return (this.productos[i].cantidad === this.max) ? |
| 77 | this.productos[i].cantidad : ++this.productos[i].cantidad; | 77 | this.productos[i].cantidad : ++this.productos[i].cantidad; |
| 78 | 78 | ||
| 79 | } | 79 | } |
| 80 | } | 80 | } |
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | decrementarContador(index) { | 83 | decrementarContador(index) { |
| 84 | 84 | ||
| 85 | for (let i = 0; i < this.productos.length; i++) { | 85 | for (let i = 0; i < this.productos.length; i++) { |
| 86 | if (i === index && this.productos[i].cantidad > 1) { | 86 | if (i === index && this.productos[i].cantidad > 1) { |
| 87 | --this.productos[i].cantidad; | 87 | --this.productos[i].cantidad; |
| 88 | --this.cont; | 88 | --this.cont; |
| 89 | break; | 89 | break; |
| 90 | } | 90 | } |
| 91 | } | 91 | } |
| 92 | 92 | ||
| 93 | this.getTotal() | 93 | this.getTotal() |
| 94 | } | 94 | } |
| 95 | 95 | ||
| 96 | setCantidadItems() { | 96 | setCantidadItems() { |
| 97 | 97 | ||
| 98 | this.cont = 0; | 98 | this.cont = 0; |
| 99 | for (let i = 0; i < this.productos.length; i++) { | 99 | for (let i = 0; i < this.productos.length; i++) { |
| 100 | this.cont += this.productos[i].cantidad; | 100 | this.cont += this.productos[i].cantidad; |
| 101 | } | 101 | } |
| 102 | } | 102 | } |
| 103 | 103 | ||
| 104 | deleteProducto(index) { | 104 | deleteProducto(index) { |
| 105 | 105 | ||
| 106 | for (let i = 0; i < this.productos.length; i++){ | 106 | for (let i = 0; i < this.productos.length; i++) { |
| 107 | if (i === index) { | 107 | if (i === index) { |
| 108 | this.cont -= this.productos[i].cantidad; | 108 | this.cont -= this.productos[i].cantidad; |
| 109 | //Elimina del total el precio de todo el item | 109 | //Elimina del total el precio de todo el item |
| 110 | this.total = this.total - (this.productos[i].precio * this.productos[i].cantidad); | 110 | this.total = this.total - (this.productos[i].precio * this.productos[i].cantidad); |
| 111 | this.productos.splice(i, 1); | 111 | this.productos.splice(i, 1); |
| 112 | return; | 112 | return; |
| 113 | } | 113 | } |
| 114 | } | 114 | } |
| 115 | } | 115 | } |
| 116 | 116 | ||
| 117 | clearCar(){ | 117 | clearCar() { |
| 118 | let arr = []; | 118 | |
| 119 | this.productos = arr; | 119 | this.productos = []; |
| 120 | this.total = 0; | 120 | this.total = 0; |
| 121 | this.cont = 0; | 121 | this.cont = 0; |
| 122 | } | 122 | } |
| 123 | |||
| 124 | } | 123 | } |
| 125 | 124 |