-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript2.js
271 lines (223 loc) · 8.67 KB
/
script2.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
* Created by Drei on 6/21/2016.
*/
var tableExistsAlready = false;
var solutionExistsAlready = false;
var processExistsAlready = false;
function crearTablaProceso(procesoSolucion) {
var body = document.getElementById("mainForm");
var divProceso = document.createElement('div');
divProceso.setAttribute('id','divProceso');
var table = [];
var tableBody = [];
var p = [];
var textnode = [];
if (!processExistsAlready) {
/*var br = document.createElement('br');*/
for (var i=0;i<procesoSolucion.length-1;i++) {
p.push(document.createElement('p'));
table.push(document.createElement('table'));
tableBody.push(document.createElement('tbody'));
table[i].className='t'+ i;
textnode.push(document.createTextNode("Iteración N° " + (parseInt(i)+1)));
procesoSolucion[i].forEach(function(rowData) {
var row = document.createElement('tr');
rowData.forEach(function(cellData) {
var cell = document.createElement('td');
cell.appendChild(document.createTextNode(cellData));
row.appendChild(cell);
});
tableBody[i].appendChild(row);
});
p[i].appendChild(textnode[i]);
p[i].className = 'pProceso';
table[i].appendChild(tableBody[i]);
table[i].className+=' tableProceso pure-table pure-table-bordered';
divProceso.appendChild(p[i]);
/*p.appendChild(br);*/
divProceso.appendChild(table[i]);
}
body.appendChild(divProceso);
processExistsAlready =true;
}
else{
var proceso = document.getElementById('divProceso');
body.removeChild(proceso);
processExistsAlready = false;
crearTablaProceso(procesoSolucion);
}
}
function crearTablaSolucion(tableData) {
var body = document.getElementById("mainForm");
var texto = document.createElement('input');
texto.setAttribute('id','inputSol');
var divSolucion = document.createElement('div');
divSolucion.setAttribute('id','divSolucion');
var table = document.createElement('table');
table.className='tableSol';
var tableBody = document.createElement('tbody');
//text
var p = document.createElement('p');
p.className='pure-u-1-3';
var textNode = document.createTextNode("Solución");
p.appendChild(textNode);
if (!solutionExistsAlready) {
tableData.forEach(function(rowData) {
var row = document.createElement('tr');
rowData.forEach(function(cellData) {
var cell = document.createElement('td');
cell.appendChild(document.createTextNode(cellData));
row.appendChild(cell);
});
tableBody.appendChild(row);
});
divSolucion.appendChild(p);
table.appendChild(tableBody);
table.className+=' pure-table pure-table-bordered';
divSolucion.appendChild(table);
//input stuff
texto.value='Costo mínimo total = '+total;
texto.readOnly=true;
texto.className='inputSol pure-input pure-input-1';
divSolucion.appendChild(texto);
body.appendChild(divSolucion);
solutionExistsAlready =true;
}
else{
var divSol = document.getElementById('divSolucion');
body.removeChild(divSol);
solutionExistsAlready = false;
crearTablaSolucion(tableData);
}
}
function generarTabla(){
// get the reference for the body
var body = document.getElementById("mainForm");
// creates a <table> element and a <tbody> element
var tbl = document.createElement("table");
tbl.className='pure-table pure-table-bordered pure-u-1';
tbl.setAttribute('id','tablaGenerada');
/*var tblBody = document.createElement("tbody");*/
var Filas = document.getElementById('inputFilas');
var x = parseInt(Filas.value) + 2 ;
var Columnas = document.getElementById('inputColumnas');
var y = parseInt(Columnas.value) + 2 ;
if(solutionExistsAlready){
body.removeChild(document.getElementById('divSolucion'));
solutionExistsAlready = false;
}
if(processExistsAlready){
body.removeChild(document.getElementById('divProceso'));
processExistsAlready = false;
}
if (!tableExistsAlready) {
// creating all cells
tableExistsAlready=true;
for (var i = 0; i < x; i++) {
// creates a table row
var row = document.createElement("tr");
row.className='pure-input-1';
for (var j = 0; j < y; j++) {
var cell = document.createElement("td");
/*cell.className='pure-u-1-24';*/
var input = document.createElement('input');
input.className = 'pure-input-1';
if (i == 0 && j > 0 && j < y-1) {
input.readOnly = true;
input.value='D'+ parseInt(j);
}
if (i == 0 && j == y-1) {
input.readOnly = true;
input.value='Demanda';
}
if (i > 0 && j == 0 && i < x-1) {
input.readOnly = true;
input.value='O'+ parseInt(i);
}
if (i == x-1 && j == 0) {
input.readOnly = true;
input.value='Oferta';
}
if ((i == x - 1 && j == y - 1) || (i == 0 && j == 0)) {
input.readOnly = true;
input.type = 'hidden';
}
cell.appendChild(input);
row.appendChild(cell);
}
tbl.appendChild(row);
}
body.appendChild(tbl);
var btnResolverNWC = document.createElement('input');
btnResolverNWC.value="NWCM";
btnResolverNWC.setAttribute('id','btnResolverNWC');
btnResolverNWC.setAttribute('onClick','saveTable(costos,"NWC")');
btnResolverNWC.className = 'pure-button pure-button-primary';
body.appendChild(btnResolverNWC);
var btnResolverLCM = document.createElement('input');
btnResolverLCM.value="LCM";
btnResolverLCM.setAttribute('id','btnResolverLCM');
btnResolverLCM.setAttribute('onClick','saveTable(costos,"LCM")');
btnResolverLCM.className = 'pure-button pure-button-primary';
body.appendChild(btnResolverLCM);
var btnResolverVAM = document.createElement('input');
btnResolverVAM.value="VAM";
btnResolverVAM.setAttribute('id','btnResolverVAM');
btnResolverVAM.setAttribute('onClick','saveTable(costos,"VAM")');
btnResolverVAM.className = 'pure-button pure-button-primary';
body.appendChild(btnResolverVAM);
}
else{
var mainForm = document.getElementById('mainForm');
var existingTable = document.getElementById('tablaGenerada');
var existingButtonVAM = document.getElementById('btnResolverVAM');
var existingButtonLCM = document.getElementById('btnResolverLCM');
var existingButtonNWC = document.getElementById('btnResolverNWC');
mainForm.removeChild(existingTable);
mainForm.removeChild(existingButtonVAM);
mainForm.removeChild(existingButtonLCM);
mainForm.removeChild(existingButtonNWC);
tableExistsAlready = false;
generarTabla();
}
}
function saveTable(myTableArray,method) {
costos.length=0;
demanda.length=0;
oferta.length=0;
var table = document.getElementById('tablaGenerada');
var Filas = document.getElementById('inputFilas');
var x = parseInt(Filas.value)+1;
var Columnas = document.getElementById('inputColumnas');
var y = parseInt(Columnas.value)+1;
for (var i=1;i<x ;i++) {
var arregloFilas = [];
for (var j=1; j< y ; j++) {
var td = table.rows[i].cells[j].getElementsByTagName('input')[0];
var data = parseInt(td.value);
arregloFilas.push(data);
}
myTableArray.push(arregloFilas);
}
for (var k=1; k<y ;k++) {
var td2 = table.rows[x].cells[k].getElementsByTagName('input')[0];
var dataDemanda = parseInt(td2.value);
demanda.push(dataDemanda);
}
for (var l=1; l<x ;l++) {
var td3 = table.rows[l].cells[y].getElementsByTagName('input')[0];
var dataOferta = parseInt(td3.value);
oferta.push(dataOferta);
}
if (method==='LCM') {
lcm();
}
if (method==='VAM') {
vam();
}
if (method==='NWC') {
nwc();
}
crearTablaProceso(procesoSolucion);
crearTablaSolucion(solucion);
}