Sfoglia il codice sorgente

crud totalmente implementado para modulo de productos

robert2206 8 anni fa
parent
commit
52c704b4d6

+ 16 - 3
src/defaults/default-listable.ts

@@ -5,6 +5,7 @@ export abstract class DefaultListable<T> implements IListable<T> {
     private isForSearch: boolean;
     private isForDelete: boolean;
     private _elements: Array<T>;
+    private _elementsAux: Array<T>;
     private _selectedIndex: number;
 
     constructor(
@@ -12,6 +13,7 @@ export abstract class DefaultListable<T> implements IListable<T> {
         this.isForSearch = false;
         this.isForDelete = false;
         this._elements = [];
+        this._elementsAux = [];
         this._selectedIndex = -1;
     }
 
@@ -55,7 +57,7 @@ export abstract class DefaultListable<T> implements IListable<T> {
      */
     remove(T: any): void {
         let index = this.elements.indexOf(T);
-        this._elements.slice(index, 1);
+        this._elements.splice(index, 1);
     }
 
     /**
@@ -76,8 +78,13 @@ export abstract class DefaultListable<T> implements IListable<T> {
     /**
      *
      */
-    search(by: string): Array<T> {
-        return null;
+    search(event: any): void {
+        let value: string = event.target.value;
+        this._elements = this._elementsAux;
+
+        this.elements = this.elements.filter(item => {
+            return (item as any).name.toLowerCase().indexOf(value.toLowerCase()) > -1;
+        });
     }
 
     /**
@@ -85,6 +92,12 @@ export abstract class DefaultListable<T> implements IListable<T> {
      */
     toggleSearch(): void {
         this.isForSearch = !this.isForSearch;
+
+        if (this.isForSearch) {
+            this._elementsAux = this.elements;
+        } else {
+            this._elements = this._elementsAux;
+        }
     }
 
     /**

+ 1 - 1
src/interfaces/searchable-interface.ts

@@ -3,7 +3,7 @@ export interface ISearchable<T> {
     /**
      *
      */
-    search(by: string): Array<T>;
+    search(event: any): void;
 
     /**
      *

+ 19 - 3
src/pages/product-details/product-details.ts

@@ -26,7 +26,18 @@ export class ProductDetailsPage {
         public navCtrl: NavController,
         public params: NavParams,
         public data: DataProvider
-    ) { }
+    ) { 
+        this.initialize();
+    }
+
+    /**
+     *
+     */
+    private initialize() {
+        if (!(this.params.data instanceof Array)) {
+            this.product = this.params.data;
+        }
+    }
 
     /**
      *
@@ -46,9 +57,14 @@ export class ProductDetailsPage {
             return;
         }
 
-        this.data.save('product', this.product).then(result => {    
-            this.params.data.push(result.products[0]);
+        this.data.save('product', this.product).then(result => {
+            if (this.params.data instanceof Array) {
+                this.params.data.push(result.products[0]);
+            } 
+            
             this.navCtrl.pop(this);
+        }).catch(e => {
+            console.log(e);
         });
     }
 }

+ 1 - 1
src/pages/product-list/product-list.html

@@ -41,7 +41,7 @@
 
 <ion-content>
 
-    <ion-card *ngFor="let p of list(); let i = index;" (press)="toggleDelete(i)">
+    <ion-card *ngFor="let p of list(); let i = index;" (tap)="openItem(i)" (press)="toggleDelete(i)">
         <ion-item>
             <ion-avatar item-left>
                 <img src="./assets/images/product.png" />

+ 19 - 11
src/pages/product-list/product-list.ts

@@ -1,5 +1,5 @@
 import { Component } from '@angular/core';
-import { NavController, AlertController } from 'ionic-angular';
+import { NavController, AlertController, ToastController } from 'ionic-angular';
 import { DefaultListable } from '../../defaults/default-listable';
 import { INavigable } from '../../interfaces/navigable-interface';
 import { Product } from '../../models/product';
@@ -16,7 +16,8 @@ export class ProductListPage extends DefaultListable<Product> implements INaviga
     constructor(
         public navCtrl: NavController,
         public data: DataProvider,
-        public alertCtrl: AlertController
+        public alertCtrl: AlertController,
+        public toastCtrl: ToastController
     ){
         super();
         this.initialize();
@@ -41,15 +42,15 @@ export class ProductListPage extends DefaultListable<Product> implements INaviga
     /**
      *
      */
-    openItem(product: any) {
-        
+    openItem(index: number) {
+        this.navCtrl.push(ProductDetailsPage, this.elements[index]);
     }
 
     /**
      *
      */
     removeItem() {
-        let confirm = this.alertCtrl.create({
+        this.alertCtrl.create({
             title: "Confirmar",
             message: "Desea borrar este producto?",
             buttons: [
@@ -63,17 +64,24 @@ export class ProductListPage extends DefaultListable<Product> implements INaviga
                     text: "Aceptar",
                     handler: () => {
                         let item = this.elements[this.selectedIndex];
+                        this.toggleDelete(-1);
 
-                        this.data.delete("product", item).then(() => { 
-                            
-                        }).catch(() => {
+                        this.data.delete("product", item).then(result => {
+                            this.elements.splice(this.selectedIndex, 1);
 
+                            this.toastCtrl.create({
+                                message: "Producto eliminado",
+                                duration: 500
+                            }).present();
+                        }).catch(() => {
+                            this.toastCtrl.create({
+                                message: "No se pudo eliminar el producto",
+                                duration: 500
+                            }).present();
                         });
                     }
                 }
             ]
-        });
-
-        confirm.present();
+        }).present();
     }
 }