Django + JQuery-遍历表行以json数据更新每一行 [英] Django + JQuery - Iterate over table rows updating each row with json data

查看:68
本文介绍了Django + JQuery-遍历表行以json数据更新每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表行看起来像这样

{% for item in items %}
    <tr id="labels" data-index="{{ forloop.counter }}"> </tr>  
{% endfor %}

我有这个json数据:

I have this json data:

"labels": [
    "A",
    "B",
    "C ",
    "D",
    "E",
    "F",
    "G",
    "H",
    "I",
    "J",
    "K",
    "L",
    "M",
    "N",
    "O",
    "P",
    "Q",
    "R",
    "S",
    "T",
    "U",
    "V",
    "W",
    "X",
    "Y",
    "Z"
],

我试图同时遍历表行和json数据,以便为每个表行分配标签.

Im trying to iterate over the table rows and the json data at the same time, so that I can assign each table row its label.

jQuery.each(labels, function() {
newlabel = this;
document.querySelectorAll('#labels').forEach(function (element, index) {
element.innerHTML = newlabel;
});
})},

但是,所有行都填充了字母Z,而不是字母顺序的A-Z.

But with this all the rows are populated with the letter Z, instead of being A-Z in alphabetical order.

谢谢您的帮助

推荐答案

对于任何有兴趣的人,以下是解决方法:

For anyone interested, here is the solution:

var iLabel = 0;
document.querySelectorAll('#labels').forEach(function (element, index) {
element.innerHTML = iLabel < labels.length ?
labels[iLabel] :
'undefined';
iLabel++;
});

这篇关于Django + JQuery-遍历表行以json数据更新每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆