From 4384b790cfe9be0594a1111e56b2e8f63857df6a Mon Sep 17 00:00:00 2001 From: = <=> Date: Wed, 7 Aug 2019 11:46:49 -0300 Subject: [PATCH] Funcion incrrementar --- src/app/sidebar/sidebar.component.html | 9 ++-- src/app/sidebar/sidebar.component.ts | 91 ++++++++++++++++++---------------- 2 files changed, 53 insertions(+), 47 deletions(-) diff --git a/src/app/sidebar/sidebar.component.html b/src/app/sidebar/sidebar.component.html index 8595b2a..f4ea36f 100644 --- a/src/app/sidebar/sidebar.component.html +++ b/src/app/sidebar/sidebar.component.html @@ -18,9 +18,9 @@
- + - +
@@ -30,9 +30,10 @@
-

(1) item

+

({{cont}}) item

+

({{cont}}) items

Total

-

{{5000 | currency}}

+

{{total | currency}}

diff --git a/src/app/sidebar/sidebar.component.ts b/src/app/sidebar/sidebar.component.ts index 780436b..6405d7c 100644 --- a/src/app/sidebar/sidebar.component.ts +++ b/src/app/sidebar/sidebar.component.ts @@ -1,4 +1,5 @@ import { Component, OnInit } from '@angular/core'; +import { timingSafeEqual } from 'crypto'; @Component({ selector: 'app-sidebar', @@ -9,7 +10,8 @@ export class SidebarComponent implements OnInit { private cont: number = 1; private min: number = 1; - private max: number =50; + private max: number = 50; + private total: number = 0; private productos = [ { @@ -41,59 +43,62 @@ export class SidebarComponent implements OnInit { constructor() { } ngOnInit() { - this.getProductosCarrito(); + this.getProductosCarrito(); + this.getCantidadProductos(); + this.getTotal(); } - getProductosCarrito(){ + getCantidadProductos(){ + var aux = 0; + for (let i = 0; i < this.productos.length; i++) { + ++aux; + } + return this.cont = aux; + + } + + getProductosCarrito() { return this.productos; - } - public aumentarContador(cant){ - console.log("llego"); - for (const i of this.productos) { - if (cant === this.max) { - return i.cantidad; - } else{ - ++i.cantidad - return i.cantidad; - } - } - - // if (this.cont === this.max) { - // this.cont; - // return this.cont; - // } else { - // ++this.cont; - // return this.cont; - // } - - } - aumentarContador2(index){ - for (let i = 0; i < this.productos.length; i++) + getTotal(){ + var subTotal; + for (const pr of this.productos) { + subTotal = pr.cantidad * pr.precio; + } + this.total = this.total + subTotal; + this.total = this.total * this.cont; + return this.total; + } + + public aumentarContador(index) { + ++this.cont; + for (let i = 0; i < this.productos.length; i++) { if (i === index) { - for (const pr of this.productos) { + return (this.productos[i].cantidad === this.max) ? + this.productos[i].cantidad : ++this.productos[i].cantidad; - } - } - } + } + } + } - decrementarContador(){ - if (this.cont === this.min) { - this.cont; - return this.cont; - } else { - --this.cont; - return this.cont; + decrementarContador(index) { + --this.cont; + for (let i = 0; i < this.productos.length; i++) { + if (i === index) { + return (this.productos[i].cantidad === this.min) ? + this.productos[i].cantidad : --this.productos[i].cantidad; + } } } - deleteProducto(index){ - for (let i = 0; i < this.productos.length; i++) + deleteProducto(index) { + --this.cont; + for (let i = 0; i < this.productos.length; i++) if (i === index) { - this.productos.splice(i,1); - return this.productos ; - } - + this.productos.splice(i, 1); + return this.productos; + } + } } -- 1.9.1