Good day I have the following code that creates a dynamic table from an array.
var data = [
{id:1, land: 'FINCA1', product: "ROSA", week:25, quantity: 2000},
{id:1, land: 'FINCA2', product: "ROSA", week:25, quantity: 1900},
{id:1, land: 'FINCA3', product: "ROSA", week:25, quantity: 350},
{id:1, land: 'FINCA3', product: "ROSA1",week:25, quantity: 410},
{id:1, land: 'FINCA1', product: "ROSA", week:26, quantity: 1254},
{id:1, land: 'FINCA2' , product: "ROSA",week:26, quantity: 123},
{id:1, land: 'FINCA3' , product: "ROSA",week:26, quantity: 200}
];
//Obtengo los keys del objeto para crear el Thead de la tabla y los guardo en una variable
var tabla = '';
var tableHead = '<tr>';
Object.keys(data[0]).forEach(function(prop){
if(prop != 'week' && prop != 'quantity')
tableHead += '<th>'+prop+'</th>';
});
var semanas = [];
for(var i in data){
semanas.push(
data[i].week
);
}
semanas = array_unique(semanas);
var index = semanas.indexOf(undefined);
if (index > -1) {
semanas.splice(index, 1);
}
for(var a in semanas){
tableHead += '<th style="text-align:right;">'+'Sem '+semanas[a]+'</th>';
}
tableHead += '<th>Total</th>';
var arrayOrganizado = data.reduce(function(arr, item){
item.week = {
numero : item.week,
cantidad : item.quantity
}
var ele = arr.find(it=>it.land === item.land && it.product === item.product);
if(ele){
ele.week.push(item.week);
ele.total = ele.week.reduce((a, b)=> a + b.cantidad, 0);
} else {
item.week = [item.week];
item.total = item.quantity;
arr.push(item);
}
return arr;
}, []);
arrayOrganizado.forEach(function(fila){
table += '<tr>';
var f = Object.keys(fila).reduce( function(a, b){
if(b != 'week' && b != 'quantity' && b != 'total')
return a + '<td>' + fila[b] + '</td>';
return a;
}, '');
table += f + semanas.reduce( (a, _, i)=>a + '<td>' + (fila.week[i] ? fila.week[i].cantidad : 0) + '</td>', '') + '<td>' + fila.total + '</td>';
});
It generates the following result (Note: as seen in the table in the HTML):
-----------------------------------------------------
Land | Product | 25 | 26 | Total |
-----------------------------------------------------
FINCA1 | ROSA | 2000 | 1254 | 3254 |
-----------------------------------------------------
FINCA2 | ROSA | 1900 | 123 | 2023 |
-----------------------------------------------------
FINCA3 | ROSA | 350 | 200 | 550 |
-----------------------------------------------------
FINCA3 | ROSA1 | 410 | 0 | 410 |
-----------------------------------------------------
Up to here everything is very good since it works excellent, what I want is to be able to totalize the columns and be as follows:
-----------------------------------------------------
Land | Product | 25 | 26 | Total |
-----------------------------------------------------
FINCA1 | ROSA | 2000 | 1254 | 3254 |
-----------------------------------------------------
FINCA2 | ROSA | 1900 | 123 | 2023 |
-----------------------------------------------------
FINCA3 | ROSA | 350 | 200 | 550 |
-----------------------------------------------------
FINCA3 | ROSA1 | 410 | 0 | 410 |
-----------------------------------------------------
TOTAL | | 4660 | 1577 | 6237 |
------------------------------------------------------
I have tried it in the following way:
var tableFoot = '';
var totalsem = 0;
var totalsum = 0;
arrayOrganizado.forEach(function(fila){
tableFoot = semanas.reduce( (a, _, i)=>a + '<td>' + (fila.week[i] ? (totalsem += fila.week[i].cantidad) : 0) + '</td>', '') + '<td>' + (totalsum += fila.total) + '</td>';
});
This way I do the sum and it works very well in the Total field of the table but in each of the columns of the week does not work well, Thanks for your help. Greetings!