【问题标题】:Trying to add each element from a for loop into an array in javascript尝试将for循环中的每个元素添加到javascript中的数组中
【发布时间】:2017-12-25 10:04:41
【问题描述】:

我有一个显示 mySQL 表的 html 文件 index.html:

<body>

    <div class="container">
    <table align="center">
        <tr>
            <th bgcolor="#f0a00c">Col1</th>
            <th bgcolor="#f0a00c">Col2</th>
            <th bgcolor="#f0a00c">Col3</th>
            <th bgcolor="#f0a00c">Col4</th>
            <th bgcolor="#f0a00c">Col5</th>
       </tr>
       {% for b in obj %}
        <tr>
            <td>{{ b.col1 }}</td>
            <td>{{ b.col2 }}</td>
            <td>{{ b.col3 }}</td>
            <td>{{ b.col4 }}</td>
            <td>{{ b.col5 }}</td>
        </tr>
    {% endfor %}

    </table>
    </div> <!-- /container -->
    </body>

我想将b.col2b.col3 中的每个值放入单独的列表中。因此,我尝试将其添加如下:

{% for b in obj
var c1 = [];
c1.push(b.col1);
var c2 = [];
c2.push(b.col2);
%}

但它不起作用。正确的方法是什么?

更新:

这是我的views.py:

def display(request):
    find_duplicate()
    return render_to_response('index.html', {'obj': my_model.objects.order_by('id')})

def get_dict():
    d={}
    for e in my_model.objects.all():
        col2 = e.col2
        col3 = e.col3
        col2 = unicode(col2).encode('UTF8')
        col3 = unicode(col3).encode('UTF8')
        d.setdefault(col2, [])
        d[col2].append(col3)
    del d['']
    return d

def find_duplicate():
    #print(d)
    d = get_dict()
    for k,v in d.items():
        if len(v) > 1:
            name=[]
            id=[]
            #print(k)
            for i in v:
                #print(i)
                reg1 = i.split("(")[0]
                name.append(reg1)
                reg2 = re.search(r'[A-Z0-9]*', i.split("_")[1])
                id.append(reg2.group())
            #print(name)
            #print(id)

所以表格看起来像这样:

Number | NameAndId
1      | Name1(something_1234)
1      | Name2(something_3456)
2      | Name3(something_7890)
2      | Name4(something_0988)

所以字典d的输出是:

{'1': ['Name1(something_1234)', 'Name2(something_3456)'], '2': 'Name3(something_7890)', 'Name4(something_0988)']}

然后解析find_duplicate函数中的col2:

所以该函数中的print(name) 将为每个键(num) 提供名称和ID,即['Name1', 'Name2']['1234', '3456'] 用于键1。所以,我有点想在每个键的名称和 id 部分应用一些 CSS 样式。那么,如何将find_duplicate()函数的结果传递给html呢?

【问题讨论】:

  • 我相信它是 javascript。我从教程中得到了这种显示表格的方式
  • 这不是原生 javascript。你能链接到教程吗?编辑 - 看起来它可能是 django? docs.djangoproject.com/en/1.9/ref/templates/language
  • 我认为是 Django。
  • @sauntimo 像这样:netai-nayek.blogspot.com/2014/08/… 最后它创建了一个 user_page.html
  • @FredGandt 哦,是的,当然是,我正在学习它的教程!但我认为这是 html javascript 部分。

标签: html arrays django


【解决方案1】:

看起来您正在尝试创建包含这些元素的 javascript 列表,您可以使用此 sn-p 创建两个包含来自 b.col2b.col3 的值的 javascript 列表

<body>

    <div class="container">
        <table align="center">
            <tr>
                <th bgcolor="#f0a00c">Col1</th>
                <th bgcolor="#f0a00c">Col2</th>
                <th bgcolor="#f0a00c">Col3</th>
                <th bgcolor="#f0a00c">Col4</th>
                <th bgcolor="#f0a00c">Col5</th>
            </tr>
            {% for b in obj %}
            <tr>
                <td>{{ b.col1 }}</td>
                <td>{{ b.col2 }}</td>
                <td>{{ b.col3 }}</td>
                <td>{{ b.col4 }}</td>
                <td>{{ b.col5 }}</td>
            </tr>
            {% endfor %}

            </table>
    </div> <!-- /container -->

    <script>
        var c2 = [], c3 = [];
        {% for b in obj %}
        c2.push("{{ b.col2 }}");
        c3.push("{{ b.col3 }}");
        {% endfor %}
        console.log([c2, c3]);
        // ... Do what you need to do in javascript with these lists.
    </script>
</body>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-02
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    相关资源
    最近更新 更多