Flot在动态添加的元素中赢得了plot()

时间:2015-03-18 23:26:01

标签: jquery ajax flot handlebars.js

我正在使用统计信息控制台,我想用Ajax填充各个图表+表格面板。最初我只是静态地定义了空面板的HTML,但是自从我开始使用把手模板化时,Flot只是不想合作:

Error: Invalid dimensions for plot, width = null, height = null

代码:

<style type="text/css">}
    .dashboard-chart { min-height: 150px; }
</style>
<div class="row" id="dashboard-container"></div>
<script id="dashboard-template" type="text/x-handlebars-template">
    <div id="{{dashboard-id}}" class="dashboard-section col-md-6">
        <div class="panel panel-default">
            <div class="panel-heading"><h1 class="panel-title">{{dashboard-title}}</h3></div>
            <div class="panel-body row-fluid">
                <div id="{{dashboard-id}}-chart" class="dashboard-chart col-md-3"></div>
                <div class="col-md-9">
                    <table id="{{dashboard-id}}-table" class="dashboard-table table table-striped">
                        <thead>{{{dashboard-thead}}}</thead>
                        <tbody></tbody>
                    </table>
                    {{{dashboard-content}}}
                </div>
            </div>
        </div>
    </div>
</script>
<script type="text/javascript">
var baseurl = "/foo_app/"
$(function(){
    // get template
    var template = Handlebars.compile($("#dashboard-template").html());
    // define dashboards
    var dashboards = [
        {
            templateargs: {
                "dashboard-id": "servertypes",
                "dashboard-title": "Server Types",
                "dashboard-thead": "<tr><th>Type</th><th>Count</th></tr>",
                "dashboard-content": ""
            },
            ajaxurl: baseurl + "ajax/test.php?type=servertypes",
            callback: function(data) {
                console.log(chart);
                console.log(chart.width());
                console.log(chart.height());
                $.plot(chart, data, chart_options); // !! -- problem here -- !!
                for(i=0;i<data.length;i++) {
                    table.append("<tr><td>"+data[i]['label']+"</td><td>"+data[i]['data']+"</td></tr>");
                }
            }
        }
    ];
    // define chart options
    var chart_options = {
        series: { pie: { show: true, innerRadius: 0.5 } },
        legend: { show: false }
    }

    for( i=0, l=dashboards.length; i<l; i++ ){
        // append dash item
        $("#dashboard-container").append(template(dashboards[i]["templateargs"]));
        // get current dash id
        var cur_id = dashboards[i]["templateargs"]["dashboard-id"];
        // get chart/table references
        var chart = $(cur_id+"-chart");
        var table = $(cur_id+"-table tbody");
        // request data
        $.ajax({
            type: "GET",
            url: dashboards[i]["ajaxurl"],
            error: function() { alert('AJAX error'); },
            success: dashboards[i]["callback"]
        });
    }
});
</script>

在DOM源代码中呈现如下:

<div id="servertypes" class="dashboard-section col-md-6">
    <div class="panel panel-default">
        <div class="panel-heading"><h1 class="panel-title">Server Types</h1></div>
        <div class="panel-body row-fluid">
            <div id="servertypes-chart" class="dashboard-chart col-md-3"></div>
            <div class="col-md-9">
                <table id="servertypes-table" class="dashboard-table table table-striped">
                    <thead><tr><th>Type</th><th>Count</th></tr></thead>
                    <tbody></tbody>
                </table>

            </div>
        </div>
    </div>
</div>

chart对象转储到控制台:

Object { length: 0, prevObject: Object, context: HTMLDocument → openstack_inventory, selector: "servertypes-chart" }

但宽度和高度都以null的形式转储到控制台。我尝试通过模板中的style属性专门设置宽度和高度,但我仍然得到相同的错误。

1 个答案:

答案 0 :(得分:1)

根据您的控制台输出:selector: "servertypes-chart",这导致您的图表找不到html元素。 id-selector需要"#servertypes-chart"

更改

var chart = $(cur_id+"-chart");
var table = $(cur_id+"-table tbody");

var chart = $("#" + cur_id+"-chart");
var table = $("#" + cur_id+"-table tbody");