简体   繁体   中英

Html table from json data without javascript

I want to create an html table with json data, without using the js scripts I have seen in other questions here on stackoverflow.

I have this structure:

function visualize(json_response) {
  var err = json_response['error'];
  if (err) {
    $("#chart_div").html("<div class='alert'>" + err + "</div>");
    return;
  }
  var data = json_response['data'];
  if (data.length == 0) {

    {% elif net.ntype == "ztc" and sens.type == 2 %}
    $("#chart_div").html("<div class='info'>No data received from this contact sensor!</div>");

  }


  $("#chart_div").html('<img src="{{ static_url('img/loading.gif') }}">');

  {% if net.ntype == "ztc" and sens.type == 2 %}

  {% for d in data %} 
     $("#chart_div").append("<td>{% d %}<td>");

}

This is my js section. The html section, instead, is a simple div block:

<div id="chart_div" style="width: 100%; height: 400px"></div>

I want, in other words, put my data into a table. But this solution I've wrote is not working. I don't know I can do... any ideas?

Thank you very much!

{% %} is for template directives, which must be one of the directives given in the Tornado template syntax reference . {{ }} is for escaped output.

In the line:

     $("#chart_div").append("<td>{% d %}<td>");

{% d %} should be either {{ d }} (escaped) or {% raw d %} (unescaped). You also need to end the for loop.

Edit: I just looked at this again and it looks like you're trying to access a JavaScript variable in your Tornado template code (as Chris Francis pointed out). Template code (in the curly braces) is executed server-side; it has no access to or knowledge of JavaScript.

In this case, you can just use JQuery to iterate over the response:

jQuery.each(data, function(index, value) {
   $("#chart_div").append("<td>" + value + "<td>"); 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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