【问题标题】:Adding sparkline chart to dynamically populated HTML table将迷你图添加到动态填充的 HTML 表中
【发布时间】: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


【解决方案1】:

$(document).ready(function() {
    var amountData = [5, 6, 7, 2, 0, -4, -2, 4];

    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) {
            var row = table.insertRow();
            addCell(row, item.category);
            addCell(row, '$ ' + item.currentMonthAmt.toFixed(2));
            addCell(row, item.topMonthStr + ' ($ ' + item.topAmount.toFixed(2) + ')');

            var lastCell = addCell(row, ""); //Added blank cell for sparkline

            var dataSpan = $("<span>");
            dataSpan.appendTo(lastCell);
            dataSpan.sparkline(amountData, {
                type: 'bar',
                barColor: '#191919'
            });
        });

    }

    populateOverallOverview([{
        category: "Transportation",
        currentMonthAmt: 1,
        topMonthStr: 1,
        topAmount: 1
    }, {
        category: "Healthcare",
        currentMonthAmt: 1,
        topMonthStr: 1,
        topAmount: 1
    }]);
});
<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-sparklines/2.1.2/jquery.sparkline.min.js"></script>
</head>

<body>
    <table id="firstTabOverall" class="table" style="font-size:13px">
        <tr>
            <th>Category</th>
            <th>Current Month Revenue</th>
            <th colspan=2>Best Selling Month</th>
        </tr>
    </table>
</body>

</html>

【讨论】:

  • 这是一个很好的实现@Brahma Dev ,也许我一直在努力传达+1 :)
【解决方案2】:

错误信息不提示是否创建了span元素,它说代码找不到sparkline函数,为了解决这个问题 请确保:

  1. 您已将jquery.sparkline.js 包含在您的页面中,即 存在迷你图的 JavaScript 文件。
  2. 还要注意你加载jquery的顺序和 jquery.sparkline.js ,首先加载 jQuery 然后 sparkline

还要注意jquery selector总是返回一个类似数组的jquery对象,这与JavaScript createElement()返回的元素不同

我想说的是:

var dataSpan = document.createElement('span');
dataSpan.sparkline(...)

不同于

$("#sparkline").sparkline(...)

建议您通过jQuery检索动态添加的元素,然后创建sparkline图表。

或者,在创建元素集ID属性后如下

dataSpan.setAttribute("id", "yourRandomDynamicIdHere");
$("#yourRandomDynamicIdHere").sparkline(...)

希望这会有所帮助!

【讨论】:

  • 嗨,我设法通过硬编码创建了一个虚拟迷你图:$("#sparkline").sparkline([5,6,7,2,0,-4,-2,4], {类型:'bar',barColor:'#191919'});但是,当我尝试按照上面的代码动态创建它时,它向我抛出了错误消息
  • 你能分享一把小提琴吗?
  • 您知道如何实际检索动态创建元素的 ID 吗?
  • 非常感谢您的帮助!
猜你喜欢
  • 2021-03-06
  • 2017-12-24
  • 1970-01-01
  • 2013-08-02
  • 2011-06-18
  • 2018-01-11
  • 2021-08-19
  • 2011-06-08
  • 1970-01-01
相关资源
最近更新 更多