[英]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. 我想用json数据创建一个html表,而不使用我在stackoverflow上其他问题中看到的js脚本。
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. 这是我的js部分。 The html section, instead, is a simple div block:
html部分是一个简单的div块:
<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 . {% %}
用于模板指令,该指令必须是Tornado模板语法参考中给出的指令之一。 {{ }}
is for escaped output. {{ }}
用于转义输出。
In the line: 在该行中:
$("#chart_div").append("<td>{% d %}<td>");
{% d %}
should be either {{ d }}
(escaped) or {% raw d %}
(unescaped). {% d %}
应该是{{ d }}
(转义)或{% raw d %}
(未转义)。 You also need to end the for loop. 您还需要结束for循环。
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). 编辑:我只是再次看了一下,看起来您正在尝试访问Tornado模板代码中的JavaScript变量(如Chris Francis所指出的)。 Template code (in the curly braces) is executed server-side;
模板代码(花括号中)在服务器端执行; it has no access to or knowledge of JavaScript.
它无法访问或不了解JavaScript。
In this case, you can just use JQuery to iterate over the response: 在这种情况下,您可以使用JQuery遍历响应:
jQuery.each(data, function(index, value) {
$("#chart_div").append("<td>" + value + "<td>");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.