-
(1) item
+ ({{cont}}) item
+ 1">({{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