I am facing issue while creating HTML table with JSON data, as I am new to this so not correctly able to write the logic.
I have a json data from which i have to create a dynamic html table. The design of table is little complex that's why I am not able to populate the HTML table with the correct data.
From my JSON I am trying to create:
but not able to.
I have done something like this
var data = [{
"billdate": "2018-08-01",
"outlet": "S0001",
"amount": 291589,
"cash": 288276,
"creditcard": 0,
"coupon": 0,
"paytm": 0,
"credit": 0,
"swiggy": 3313,
"kb": 0,
"bigbasket": 0
},
{
"billdate": "2018-08-01",
"outlet": "S0002",
"amount": 58337,
"cash": 56727,
"creditcard": 0,
"coupon": 0,
"paytm": 0,
"credit": 0,
"swiggy": 1610,
"kb": 0,
"bigbasket": 0
},
{
"billdate": "2018-08-01",
"outlet": "S0009",
"amount": 65970,
"cash": 65970,
"creditcard": 0,
"coupon": 0,
"paytm": 0,
"credit": 0,
"swiggy": 0,
"kb": 0,
"bigbasket": 0
},
{
"billdate": "2018-08-02",
"outlet": "S0001",
"amount": 296125,
"cash": 290480,
"creditcard": 0,
"coupon": 0,
"paytm": 0,
"credit": 0,
"swiggy": 5645,
"kb": 0,
"bigbasket": 0
},
{
"billdate": "2018-08-02",
"outlet": "S0002",
"amount": 56545,
"cash": 55034,
"creditcard": 0,
"coupon": 0,
"paytm": 0,
"credit": 0,
"swiggy": 1511,
"kb": 0,
"bigbasket": 0
},
{
"billdate": "2018-08-02",
"outlet": "S0009",
"amount": 72213,
"cash": 72213,
"creditcard": 0,
"coupon": 0,
"paytm": 0,
"credit": 0,
"swiggy": 0,
"kb": 0,
"bigbasket": 0
}
]
let formatData = function(data) {
let billdates = [];
let outlets = [];
data.forEach(element => {
if (billdates.indexOf(element.billdate) == -1) {
billdates.push(element.billdate);
}
if (outlets.indexOf(element.outlet) == -1) {
outlets.push(element.outlet);
}
});
return {
data: data,
billdates: billdates,
outlets: outlets,
};
};
let renderTable = function(data) {
billdates = data.billdates;
outlets = data.outlets;
data = data.data;
let tbl = document.getElementById("tblOlSalesSummary");
let table = document.createElement("table");
let thead = document.createElement("thead");
let headerRow = document.createElement("tr");
let th = document.createElement("th");
th.innerHTML = "BillDate";
th.classList.add("text-center");
headerRow.appendChild(th);
let grandTotal = 0;
let outletWiseTotal = {};
th = document.createElement("th");
th.innerHTML = "Sales Type";
th.classList.add("text-center");
headerRow.appendChild(th);
outlets.forEach(element => {
th = document.createElement("th");
th.innerHTML = element;
th.classList.add("text-center");
headerRow.appendChild(th);
outletWiseTotal[element] = 0;
data.forEach(el => {
if (el.outlet == element) {
outletWiseTotal[element] += parseInt(el.amount);
}
});
grandTotal += outletWiseTotal[element];
});
thead.appendChild(headerRow);
headerRow = document.createElement("tr");
th = document.createElement("th");
th.innerHTML = "Total";
th.classList.add("text-center");
headerRow.appendChild(th);
outlets.forEach(element => {
th = document.createElement("th");
th.innerHTML = outletWiseTotal[element].toLocaleString('en-in');
th.classList.add("text-right");
headerRow.appendChild(th);
});
th = document.createElement("th");
th.innerHTML = grandTotal.toLocaleString('en-in');
th.classList.add("text-right");
/* console.log(grandTotal); */
// headerRow.appendChild(th);
headerRow.insertBefore(th, headerRow.children[1]);
thead.appendChild(headerRow);
table.appendChild(thead);
let tbody = document.createElement("tbody");
billdates.forEach(element => {
let row = document.createElement("tr");
td = document.createElement("td");
td.innerHTML = element;
row.appendChild(td);
let total = 0;
outlets.forEach(outlet => {
let el = 0;
data.forEach(d => {
if (d.billdate == element && d.outlet == outlet) {
total += parseInt(d.cash);
el = d.cash;
}
});
td = document.createElement("td");
td.innerHTML = el.toLocaleString('en-in');
td.classList.add("text-right");
row.appendChild(td);
});
/* console.log("row is : " , row.children ) */
td = document.createElement("td");
td.innerHTML = total.toLocaleString('en-in');
td.classList.add("text-right");
// row.appendChild(td);
row.insertBefore(td, row.children[1]);
tbody.appendChild(row);
});
table.appendChild(tbody);
tbl.innerHTML = "";
tbl.appendChild(table);
table.classList.add("table");
table.classList.add("table-striped");
table.classList.add("table-bordered");
table.classList.add("table-hover");
}
let formatedData = formatData(data);
renderTable(formatedData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<div align="center">
<table id="tblOlSalesSummary">
</table>
</div>
As you can look at my image I have to loop data billdate wise, that's where I am stuck. The image I have uploaded is just for example, please don't match the values there, all the values of image and my JSON are different but Full Total Total should be calculated on the basis of that only.
I know how to create dynamic table but here I am stuck with some looping scenario.
My table is fully dynamic all the data is coming from db on the basis of user selection.
Edit
I have added amount in my JSON data which is billdate wise total so there is not need to calculate that by coding.
Amount is: the total date wise for each outlets
