gpt4 book ai didi

javascript - 如何从对象生成具有多级标题的数据透视表?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:55:19 25 4
gpt4 key购买 nike

我想从给定的 2 个对象生成 HTML 表格,但它有太多嵌套的标题,我似乎无法管理。

最终结果应该是这样的:

enter image description here

这是我创建表格的数据:

var columnData = [  {Col001: "1",Col002: "John Barnes",Col003: "Martha Hall",Col004: "Small",Col005: "Has a Drive-Thru",Col006: "Dustin Kensrue", ScorePct: "84.7"},
{Col001: "2",Col002: "John Barnes",Col003: "Martha Hall",Col004: "Small",Col005: "Has a Drive-Thru",Col006: "Dustin Kensrue", ScorePct: "93.8"},
{Col001: "2",Col002: "John Barnes",Col003: "Martha Hall",Col004: "Small",Col005: "Has a Drive-Thru",Col006: "Ninja Koto", ScorePct: "87.5"},
{Col001: "3",Col002: "Russell Montgomery",Col003: "Judith Rodriguez",Col004: "Small",Col005: "Has a Drive-Thru",Col006: "Dustin Kensrue", ScorePct: "90.3"},
{Col001: "4",Col002: "Russell Montgomery",Col003: "Judith Rodriguez",Col004: "Small",Col005: "Has a Drive-Thru",Col006: "Ninja Koto", ScorePct: "83.3"},
{Col001: "5",Col002: "Russell Montgomery",Col003: "Alice Butler",Col004: "Medium",Col005: "Has a Drive-Thru",Col006: "Ed Waves", ScorePct: "90.3"},
{Col001: "6",Col002: "Russell Montgomery",Col003: "Alice Butler",Col004: "Medium",Col005: "Has a Drive-Thru",Col006: "Ed Waves", ScorePct: "87.5"},
{Col001: "7",Col002: "Russell Montgomery",Col003: "Stephen Peterson",Col004: "Medium",Col005: "No Drive-Thru",Col006: "Dustin Kensrue", ScorePct: "90.6"},
{Col001: "7",Col002: "Russell Montgomery",Col003: "Stephen Peterson",Col004: "Medium",Col005: "No Drive-Thru",Col006: "Ed Waves", ScorePct: "68.8"},
{Col001: "7",Col002: "Russell Montgomery",Col003: "Stephen Peterson",Col004: "Medium",Col005: "No Drive-Thru",Col006: "Ninja Koto", ScorePct: "83.3"},
{Col001: "8",Col002: "John Barnes",Col003: "Stephen Peterson",Col004: "Large",Col005: "No Drive-Thru",Col006: "Ninja Koto", ScorePct: "80.0"},
{Col001: "8",Col002: "John Barnes",Col003: "Stephen Peterson",Col004: "Large",Col005: "No Drive-Thru",Col006: "Veela Faint", ScorePct: "96.9"},
{Col001: "9",Col002: "John Barnes",Col003: "Russell Taylor",Col004: "Large",Col005: "No Drive-Thru",Col006: "Ninja Koto", ScorePct: "88.9"},
{Col001: "10",Col002: "Russell Montgomery",Col003: "Carl Perry",Col004: "Large",Col005: "No Drive-Thru",Col006: "Ed Waves", ScorePct: "87.5"},
{Col001: "10",Col002: "Russell Montgomery",Col003: "Carl Perry",Col004: "Large",Col005: "No Drive-Thru",Col006: "Veela Faint", ScorePct: "93.8"}];

var rowData = [ {ColID:"0",ColValue:"101",RowID:"1"},
{ColID:"1",ColValue:"Varna",RowID:"1"},
{ColID:"0",ColValue:"102",RowID:"2"},
{ColID:"1",ColValue:"Dobrich",RowID:"2"},
{ColID:"0",ColValue:"103",RowID:"3"},
{ColID:"1",ColValue:"Plovdiv",RowID:"3"},
{ColID:"0",ColValue:"104",RowID:"4"},
{ColID:"1",ColValue:"Montana",RowID:"4"},
{ColID:"0",ColValue:"105",RowID:"5"},
{ColID:"1",ColValue:"Sofia",RowID:"5"},
{ColID:"0",ColValue:"106",RowID:"6"},
{ColID:"1",ColValue:"Sliven",RowID:"6"},
{ColID:"0",ColValue:"107",RowID:"7"},
{ColID:"1",ColValue:"Pomorie",RowID:"7"},
{ColID:"0",ColValue:"108",RowID:"8"},
{ColID:"1",ColValue:"Albena",RowID:"8"},
{ColID:"0",ColValue:"109",RowID:"9"},
{ColID:"1",ColValue:"Bourgas",RowID:"9"},
{ColID:"0",ColValue:"110",RowID:"10"},
{ColID:"1",ColValue:"Bansko",RowID:"10"}];

关于 columnData 对象:

columnData.Col001代表表格的每一行号。

columnData.Col002到columnData.Col005为表头,其中001为最高层,005为最低层。

columnData.ScorePct每一行对应的值

关于 rowData 对象:

rowData.RowID - 行号

rowData.ColID - 列数

rowData.ColValue——对应的值

这是我到目前为止提出的解决方案:

https://jsfiddle.net/u5c21mg3/1/

我设法从这两个构建了另一个对象(您将在 jsfiddle 的控制台中看到它)但我无法正确打印它。或者也许有更好的方法来构建第三个对象?任何帮助将不胜感激!

最佳答案

https://fiddle.jshell.net/jddz3jgz/2/

这是我的部分解决方案 - 按点分组。如果我能提供帮助,我会很高兴,但不幸的是,我无法为标题提出解决方案

更新:

完整解决方案:

https://fiddle.jshell.net/jddz3jgz/3/

关于javascript - 如何从对象生成具有多级标题的数据透视表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46326525/

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