<div id="grid"></div>
#grid {
width: 800px;
height: 500px;
background-color: #efefef;
}
let data = [
['张三', 90, 70, 88, 100, 92, 95, 87, 98, 99, 100, 55, 60 ],
['李四', 89, 92, 88, 100, 92, 95, 97, 98, 55, 92, 55, 60 ],
['王五', 100, 70, 82, 99, 92, 95, 97, 98, 69, 88, 55, 99],
['赵六', 77, 91, 81, 75, 91, 75, 96, 91, 77, 96, 55, 60 ]
]
new Handsontable(document.getElementById('grid'), {
data: data,
manualColumnResize: true,
colHeaders: true,
licenseKey: 'non-commercial-and-evaluation',
rowHeaders: true,
manualColumnResize: true,
width: '800',
height: '500',
readOnly: true,
outsideClickDeselects: false,
rowHeights: 28,
columnHeaderHeight: 32,
className: 'htMiddle htCenter',
nestedHeaders: [
['姓名',
{
label: "2021年",
colspan: 6
}, {
label: "2022年",
colspan: 6
}],
['', '语文', '数学', '外语', '政治', '历史', '地理', '语文', '数学', '外语', '政治', '历史', '地理']
],
afterGetColHeader: function(col, th) {
setTimeout(() => {
if (col === -1) {
const theads = th.parentNode.parentNode
const trs = theads.getElementsByTagName('tr')
const trCols1 = trs[0].getElementsByTagName('th')
const trCols2 = trs[1].getElementsByTagName('th')
if (trCols1.length === trCols2.length) {
trCols1[0].style.borderBottom = 'none'
for (let i = 1; i < trCols1.length; i++) {
if (!trCols1[i].getAttribute('colSpan') && trCols1[i].className !== 'hiddenHeader') {
trCols1[i].rowSpan = 2
trCols1[i].style.verticalAlign = 'middle'
trCols1[i].style.height = '64px'
trCols2[i].className = 'hiddenHeader'
trCols1[i].style.borderBottom = 'none'
}
}
}
}
}, 100)
}
});
