gpt4 book ai didi

javascript - 如何使用 JSON 数据创建动态表?

转载 作者:行者123 更新时间:2023-11-30 14:08:28 25 4
gpt4 key购买 nike

我有一个在 JSON 数据的帮助下制作的 HTML 表格,我仅在 javascript 的帮助下创建我的表格并且我的表格是动态的

我有一个 json 数组,其中有几个具有数据的对象。

我试图从这个 JSON 制作一个 HTML 表格

  • 我已成功创建页眉,但在创建正文部分时遇到问题
  • 我正在尝试创建这种表格:

    table

  • 平均值就像所有已取消账单的总和除以网点数量,这就是我计算网点长度并将其存储在全局变量中的原因

  • 由于我的代码有点长,所以我在所有行中都注释了我在做什么以及在什么地方遇到了什么问题
  • 在正文部分,我不知道如何填充关于导出的已取消账单数据以及重复账单的数据

这是我到目前为止所做的:

var outletCount = 0; //global variable to get the no of outlets
var data = [{
"outlet": "JAYANAGAR",
"cancelled": 126544,
"duplicate": 1
},
{
"outlet": "MALLESHWARAM",
"cancelled": 31826,
"duplicate": 31
},
{
"outlet": "KOLAR",
"cancelled": 10374,
"duplicate": 10
}
];

let formatData = function(data) { //outlets is unique thats why formating it to loop forward in my code
let outlets = [];
data.forEach(element => {
if (outlets.indexOf(element.outlet) == -1) {
outlets.push(element.outlet);
}
});
outletCount = outlets.length //calculating outlet count

return {
data: data,
outlets: outlets,

};
};

let renderTable = function(data) {
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 = "Bill Type"; //header
th.classList.add("text-center");
headerRow.appendChild(th);
th = document.createElement("th");
th.innerHTML = "Average"; //header
th.classList.add("text-center");
headerRow.appendChild(th);
outlets.forEach(element => {
th = document.createElement("th");
th.innerHTML = element; //this one is populating outlet as header
th.classList.add("text-center");

headerRow.appendChild(th);

});

thead.appendChild(headerRow);
table.appendChild(thead);

let tbody = document.createElement("tbody"); // from here onwards i don't know what to do

let row = document.createElement("tr");

let total = 0;
outlets.forEach(outlet => { //i am trying to loop through outlets but getting somthing else
let el = 0;
data.forEach(d => {
if (d.outlet == outlet) {
total += parseInt(d.cancelled);
el = d.cancelled;
}
});
td = document.createElement("td");
td.innerHTML = el.toLocaleString('en-in');
td.classList.add("text-right");
row.appendChild(td);
});
/* console.log("row is : " , row.children ) */

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" class="table table-responsive">
<table id="tblOlSalesSummary"></table>
</div>

我不知道要循环我的已取消账单和关于网点的重复账单,然后计算平均值。

最佳答案

您的表中有两个静态字段列 billtypeaverage 需要在循环 json 数据之前追加

 var outletCount = 0; //global variable to get the no of outlets
var data = [{
"outlet": "JAYANAGAR",
"cancelled": 126544,
"duplicate": 1
},
{
"outlet": "MALLESHWARAM",
"cancelled": 31826,
"duplicate": 31
},
{
"outlet": "KOLAR",
"cancelled": 10374,
"duplicate": 10
},
{
"outlet": "New Test",
"cancelled": 154,
"duplicate": 20
}
];

let formatData = function(data) { //outlets is unique thats why formating it to loop forward in my code
let outlets = [];
data.forEach(element => {
if (outlets.indexOf(element.outlet) == -1) {
outlets.push(element.outlet);
}
});
outletCount = outlets.length //calculating outlet count

return {
data: data,
outlets: outlets,

};
};

let renderTable = function(data) {
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 = "Bill Type"; //header
th.classList.add("text-center");
headerRow.appendChild(th);
th = document.createElement("th");
th.innerHTML = "Average"; //header
th.classList.add("text-center");
headerRow.appendChild(th);
outlets.forEach(element => {
th = document.createElement("th");
th.innerHTML = element; //this one is populating outlet as header
th.classList.add("text-center");

headerRow.appendChild(th);

});

thead.appendChild(headerRow);
table.appendChild(thead);

let tbody = document.createElement("tbody"); // from here onwards i don't know what to do

let row = document.createElement("tr");

let total = 0;

// static field insertion for Cancelled bill
let el = 'Cancelled bill';
td = document.createElement("td");
td.innerHTML = el.toLocaleString('en-in');
td.classList.add("text-right");
row.appendChild(td);
// Logic start to find the average cancelled amount
var total_cancel =0;
total_can_count = 0;
outlets.forEach(outlet => {
data.forEach(d => {
if (d.outlet == outlet) {
total_cancel += parseInt(d.cancelled);
total_can_count++;

}
});
});

let el_avg = ( total_cancel / (total_can_count) );
td = document.createElement("td");
td.innerHTML = el_avg.toLocaleString('en-in');
td.classList.add("text-right");
row.appendChild(td);
// Logic End to find the average cancelled amount

outlets.forEach(outlet => {
let el = 0;
data.forEach(d => {
if (d.outlet == outlet) {
total += parseInt(d.cancelled);
el = d.cancelled;
}
});
td = document.createElement("td");
td.innerHTML = el.toLocaleString('en-in');
td.classList.add("text-right");
row.appendChild(td);
});


/* console.log("row is : " , row.children ) */

tbody.appendChild(row);

let row_duplicate = document.createElement("tr");

let total_dup = 0;
// static field insertion for duplicate bill
let el_2 = 'Duplicate bill';
td = document.createElement("td");
td.innerHTML = el_2.toLocaleString('en-in');
td.classList.add("text-right");
row_duplicate.appendChild(td);

// Logic start to find the Duplicate average
total_dup_count = 0;
outlets.forEach(outlet => {
data.forEach(d => {
if (d.outlet == outlet) {
total_dup += parseInt(d.duplicate);
total_dup_count++;
}
});
});

let el_avg_2 = ( total_dup / (total_dup_count) );
td = document.createElement("td");
td.innerHTML = el_avg_2.toLocaleString('en-in');
td.classList.add("text-right");
row_duplicate.appendChild(td);

// Logic End to find the Duplicate average

outlets.forEach(outlet => { //i am trying to loop through outlets but getting somthing else
let el = 0;
data.forEach(d => {
if (d.outlet == outlet) {
total += parseInt(d.duplicate);
el = d.duplicate;
}
});
td = document.createElement("td");
td.innerHTML = el.toLocaleString('en-in');
td.classList.add("text-right");
row_duplicate.appendChild(td);
});


/* console.log("row is : " , row.children ) */

tbody.appendChild(row);
tbody.appendChild(row_duplicate);

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" class="table table-responsive">
<table id="tblOlSalesSummary"></table>
</div>

关于javascript - 如何使用 JSON 数据创建动态表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54899044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com