Repository navigation
Expand file tree
/
Copy pathcontroller.js
More file actions
133 lines (124 loc) · 4.21 KB
/
Copy pathcontroller.js
File metadata and controls
133 lines (124 loc) · 4.21 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
function loadData() {
const url = 'https://precios-7352b.firebaseio.com/precios.json';
getData(url);
}
function getData(url) {
const tbody = getElementById('precios');
tbody.innerHTML = '';
fetch(url)
.then((resp) => resp.json())
.then(function (data) {
let precios = data.results;
return precios.map(function (precio, index) {
let tr = createNode('tr'),
tdNombre = createNode('td'),
tdCodigo= createNode('td'),
tdCategoria = createNode('td'),
tdAttconsumidor= createNode('td'),
tdCodigoBarra= createNode('td'),
tdContenido = createNode('td'),
tdTipo= createNode('td'),
tdUnidad = createNode('td'),
tdMarca= createNode('td'),
tdProveedor= createNode('td'),
tdCalidad = createNode('td'),
tdAction = createNode('td');
aAction = createNode('button');
tdNombre.innerHTML = `${precio.nombre}`;
tdCodigo.innerHTML = `${precio.codigo}`;
tdCategoria.innerHTML = `${precio.categoria}`;
tdAttconsumidor.innerHTML = `${precio.attconsumidor}`;
tdCodigoBarra.innerHTML = `${precio.codebarra}`;
tdContenido.innerHTML = `${precio.contenido}`;
tdTipo.innerHTML = `${precio.tipo}`;
tdUnidad.innerHTML = `${precio.unidad}`;
tdMarca.innerHTML = `${precio.marca}`;
tdProveedor.innerHTML = `${precio.proveedor}`;
tdCalidad.innerHTML = `${precio.calidad}`;
aAction.innerText = "Editar";
aAction.addEventListener('click', () => setprecios(precio, index));
append(tr, tdNombre);
append(tr, tdCodigo);
append(tr, tdCategoria);
append(tr, tdAttconsumidor);
append(tr, tdCodigoBarra);
append(tr, tdContenido);
append(tr, tdTipo);
append(tr, tdUnidad);
append(tr, tdMarca);
append(tr, tdProveedor);
append(tr, tdCalidad);
append(tdAction, aAction);
append(tr, tdAction);
append(tbody, tr);
})
})
.catch(function (error) {
console.log(JSON.stringify(error));
});
}
function setprecios(precio, index) {
none('table');
block('formprecios');
indexId = index;
getElementById("listar").style.display = "none";
setElementById('nombre', precio.nombre);
setElementById('codigo', precio.codigo);
setElementById('categoria', precio.categoria);
setElementById('attconsumidor', precio.attconsumidor);
setElementById('codigobarra', precio.codebarra);
setElementById('contenido', precio.contenido);
setElementById('tipo', precio.tipo);
setElementById('unidad', precio.unidad);
setElementById('marca', precio.marca);
setElementById('proveedor', precio.proveedor);
setElementById('calidad', precio.calidad);
}
function editData() {
const url = `https://precios-7352b.firebaseio.com/precios/results/${indexId}.json`;
setData(url);
}
function setData(url) {
let data = {
nombre: getElementById('nombre').value,
codigo: getElementById('codigo').value,
categoria: getElementById('categoria').value,
attconsumidor: getElementById('attconsumidor').value,
codebarra: getElementById('codigobarra').value,
contenido: getElementById('contenido').value,
tipo: getElementById('tipo').value,
unidad: getElementById('unidad').value,
marca: getElementById('marca').value,
proveedor: getElementById('proveedor').value,
calidad: getElementById('calidad').value
}
let fetchData = {
method: 'PATCH',
body: JSON.stringify(data),
headers: new Headers()
}
fetch(url, fetchData).then(function (response) {
console.log(response);
none('formprecios');
block('table');
loadData();
})
}
function getElementById(id) {
return document.getElementById(id);
}
function setElementById(id, value) {
return document.getElementById(id).value = value;
}
function createNode(element) {
return document.createElement(element);
}
function append(parent, el) {
return parent.appendChild(el);
}
function block(id) {
document.getElementById(id).style.display = 'block';
}
function none(id) {
document.getElementById(id).style.display = 'none';
}