amb-imagenes.component.ts
2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { Component, OnInit } from '@angular/core';
import { appSettings } from 'src/etc/AppSettings';
import { ProductoService } from 'src/app/services/producto.service';
import { Producto } from 'src/app/wrappers/producto';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-amb-imagenes',
templateUrl: './amb-imagenes.component.html',
styleUrls: ['./amb-imagenes.component.scss']
})
export class AmbImagenesComponent implements OnInit {
apiUrl = appSettings.apiUrl;
articulos: Producto[] = [];
private auxProductos: Producto[] = [];
private searchTerm: string = '';
private paginationData: any;
constructor(private productoService: ProductoService, private http: HttpClient) { }
ngOnInit() {
this.productoService.getAllWithPaginator()
.subscribe((res) => {
this.articulos = res.data;
this.paginationData = res.pagination;
this.filterItems();
}, error => console.error(error));
}
onFileSelected(event, articulo: Producto) {
let auxFiles: FileList = event.target.files;
Array.from(auxFiles).forEach(file => {
this.onLoad(file)
.then(result => {
articulo.imagenes.push({
name: file.name,
fromGallery: true,
imagen: result,
id_articulo: articulo.id
});
this.saveInBase({
name: file.name,
base64: result,
codigo: articulo.CodArt,
sector: articulo.CodSec
});
});
})
}
filterItems() {
this.auxProductos = this.articulos.filter(x => {
return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase()) ||
x.CodArt.toString().includes(this.searchTerm.toLowerCase()) ||
x.CodSec.toString().includes(this.searchTerm.toLowerCase());
});
}
saveInBase(img) {
this.productoService.saveInBase(img)
.subscribe(data => {
}, error => console.error(error));
}
onLoad(file) {
return new Promise((resolve, reject) => {
var fr = new FileReader();
fr.onload = function () {
resolve(fr.result);
};
fr.readAsDataURL(file);
});
}
deleteImage(imagenes, index: number) {
if (!imagenes[index].name) {
imagenes[index].name = imagenes[index].imagen;
}
this.productoService.deleteImage(imagenes[index])
.subscribe(res => {
if (res) {
imagenes.splice(index, 1);
}
}, error => console.error(error));
}
pageChanged(event: any): void {
this.productoService.getAllWithPaginator(event.page)
.subscribe((res) => {
this.articulos = res.data;
this.paginationData = res.pagination;
this.filterItems();
}, error => console.error(error));
}
}