【发布时间】:2017-09-12 09:25:22
【问题描述】:
我正在尝试在动态填充表的每一行末尾添加一个sparkline chart。这是我的代码:
function populateOverallOverview(result){
var table = document.getElementById("firstTabOverall");
function addCell(tr, text) {
var td = tr.insertCell();
td.textContent = text;
return td;
}
result.forEach(function (item) {
// sparkline chart here
var dataSpan = document.createElement('span');
dataSpan.sparkline(amountData, {
type: 'bar',
barColor: '#191919'});
var row = table.insertRow();
addCell(row, item.category);
addCell(row, '$ ' + item.currentMonthAmt.toFixed(2));
addCell(row, item.topMonthStr + ' ($ ' + item.topAmount.toFixed(2) + ')');
});
}
amountData 的样本数据是 [5,6,7,2,0,-4,-2,4]。我的 HTML 表格:
<table id="firstTabOverall" class="table" style="font-size:13px">
<tr>
<th>Category <span id="sparkline1"></span></th>
<th>Current Month Revenue</th>
<th>Best Selling Month</th>
</tr>
</table>
我正在尝试动态创建迷你图,然后添加到行尾。但是,我收到此错误消息:
Uncaught (in promise) TypeError: dataSpan.sparkline is not a function
我不确定如何使用 ID 动态创建跨度,然后将该 ID 用于 .sparkline。有什么想法吗?
【问题讨论】:
-
您能否为
result和您要输出的html 添加相同的数据。 -
@BrahmaDev 嗨,我已经编辑了问题!
标签: javascript html html-table sparklines