简体   繁体   English

更新 <h1> div内的数据

[英]Updating the <h1> data inside a div

I have a div which has some dynamic <h1> elements embedded inside it. 我有一个div,其中嵌入了一些动态<h1>元素。 I want to update these <h1> elements. 我想更新这些<h1>元素。 But I am not able to figure out how. 但是我不知道怎么做。 I am using handlebars to feed data to the <h1> 's. 我正在使用把手将数据馈送到<h1>

This is the code for the div. 这是div的代码。

<div class="homepage-availability-div">
    {{#each availData as |index|}}
    <div class="homepage-availability-inner-div">
        <h1 class="homepage-availability-text"> {{@key}}: <span class="dashboard-success">{{this}}% </span> </h1>
    </div>
    {{/each}}
</div>

This is how i am trying to do it. 这就是我试图做到的方式。

$.ajax({
    url: url,
    headers: { 'x-cyclops-ajax': 'yes' },
    method: 'GET',
    dataType: 'json',
    success: function(data) {
        var chart = $('#container').highcharts();
        var keys = Object.keys(data["histData"]);
        $( ".main-div" ).empty();
        for( var i=0; i< keys.length; i++) {
            chart.series[i].setData(data["histData"][keys[i]]["histFailure"], true);
            $( ".main-div" ).append( "<div class=homepage-availability-inner-div><h1 class=homepage-availability-text> " + keys[i] + ": <span class=dashboard-success>" + data["availData"][keys[i]] + " </span> </h1></div>" );
        }
        chart.xAxis[0].setCategories(data["histKeys"]);
        console.log("Data:" + JSON.stringify(data["availData"]));
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log("Did not hit the AJAX call");
    }
});

Any help would be appreciated. 任何帮助,将不胜感激。 :) :)

I see no connection between your handlebars template and your JavaScript. 我看不到您的车把模板和JavaScript之间有任何联系。 You make an ajax request and then take that data and attempt to manually update the content of your <div class="main-div"> tag: 您发出ajax请求,然后获取该数据并尝试手动更新<div class="main-div">标记的内容:

var keys = Object.keys(data["histData"]);
    $( ".main-div" ).empty();
    for( var i=0; i< keys.length; i++) {
        chart.series[i].setData(data["histData"][keys[i]]["histFailure"], true);
        $( ".main-div" ).append( "<div class=homepage-availability-inner-div><h1 class=homepage-availability-text> " + keys[i] + ": <span class=dashboard-success>" + data["availData"][keys[i]] + " </span> </h1></div>" );
    }

This data is coming from your ajax call not from your template (unless of course you are applying this template server side?!). 此数据来自您的Ajax调用,而不是来自模板(当然,除非您正在应用此模板服务器端?!)。

Now, the logic here is incorrect because your div's are improperly formed. 现在,这里的逻辑不正确,因为您的div格式不正确。 The class, for example needs to be quoted: 例如,该类需要用引号引起来:

... '<div class="homepage-availability-inner-div"><h1 class="homepage-availability-text"> ...'

and later on: 后来:

... <span class="dashboard-success"> ...

I use single quotes around the outer strings so I can use double quotes within it. 我在外部字符串周围使用单引号,因此可以在其中使用双引号。 Otherwise you have to escape them which is a pain. 否则,您必须摆脱它们,这很痛苦。

I would suggest to get further assistance if this doesn't resolve your issue, that you explain how (and where) you are invoking your handlebars template. 如果这不能解决您的问题,我建议您寻求进一步的帮助,说明您如何(以及在​​何处)调用车把模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM