Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

To generate a table from a nested list in Django template

I have a nested list as such:

olddataList = [['Route To Path/file1.txt', 'Route To Path/file2.txt', 'Route To Path/file3.txt'], [['Routing', 'Error'], ['Routing', 'Error'], ['Routing', 'Error']], [[['file1.txt', 'Mapping error']], [['file2.txt', 'Mapping error']],[['file3.txt', 'Mapping error']]], [['Summary 1 CODE018', 1], ['Summary 1 CODE018', 2], ['Summary 1 CODE018', 3]]]


dataList = dataList = [['Route To Path/file1.txt', 'Route To Path/file2.txt', 'Route To Path/file3.txt'], [['Routing', 'Error'], ['Routing', 'Error'], ['Routing', 'Error']], [[['file1a.txt', 'Mapping error'],['file1b.txt', 'Mapping error']], [['file2a.txt', 'Mapping error'],['file2b.txt', 'Mapping error']],[['file3a.txt', 'Mapping error'],['file3b.txt', 'Mapping error']]], [['Summary 1 CODE018', 1], ['Summary 1 CODE018', 2], ['Summary 1 CODE018', 3]]]

My intention is to generate a table like below:

Route To Path/file1.txt
Routing     Error
file1a.txt   Mapping error
file1b.txt   Mapping error
Summary 1 CODE018

Route To Path/file2.txt
Routing     Error
file2a.txt   Mapping error
file2b.txt   Mapping error
Summary 1 CODE018

Route To Path/file3.txt
Routing     Error
file3a.txt   Mapping error
file3b.txt   Mapping error
Summary 1 CODE018

However with my code below, the table I get is as below. The final summary line should take only the first field of the list.

Route To Path/file1.txt
Route To Path/file2.txt
Route To Path/file3.txt
Routing     Error
Routing     Error
Routing     Error
file1.txt   Mapping error
file2.txt   Mapping error
file3.txt   Mapping error

I updated my code follows on suggestion by you Mr NavaneethaKrishnan, but I'm facing issue cause my Django environment do not recognize the table in bracket. However the way I implementing it wrong though cause it also not recognize argument such as 'dataList.1.outer_counter' as to replace your code 'dataList[1][i]'.

{% with dataList|length as ctr %}
{{ ctr }} <br />
<h4>{{ dataList.0.ctr }}</h4><br /> <!-- not recognize dataList.0.ctr -->
{% endwith %}
<table id="myTable">
{% load summary %}
{% for c in dataList %} <!-- unable to apply range like  0|range:ctr here -->
{% with forloop.counter0 as outer_counter %}
    <tr>
        {% for r in dataList.1.outer_counter %} <!-- not recognize dataList.1.outer_counter -->
        <td>{{r}}</td>
        {% endfor %}
    </tr>
    <tr>
        {% for r in dataList.2.outer_counter.0 %}
        <td>{{r}}</td>
        {% endfor %}
    </tr>
    <tr>
        {% for r in dataList.3.outer_counter %}
        <td>{{r}}</td>
        {% endfor %}
    </tr>
</table>
{% endwith %}
{% endfor %}

Thank you in advance. Your helps is greatly appreciated.

[UPDATE] After do more research, finally I managed to get the solution. I post it here for anyone else who may need this for reference in future, as a token of appreciation for Mr NavaneethaKrishnan who had helped me.

{% for i in dataList.0 %}
{% with forloop.counter0 as outer_counter %}
{% if forloop.counter0 == outer_counter %}
<br />
<br />
<h4>{{ i }}</h4>
<table id="unitCheckDetailedTable">
    <tr>
        {% for i in dataList.1 %}
            {% if forloop.counter0 == outer_counter %}
                {% for j in i %}
                    <td>{{j}}</td>
                {% endfor %}
            {% endif %}
        {% endfor %}

    </tr>
        {% for i in dataList.2 %}
            {% if forloop.counter0 == outer_counter %}
                {% for j in i%}
    <tr>
                    {% for k in j%}
                        <td>{{k}}</td>
                    {% endfor %}
    </tr>
                {% endfor %}
            {% endif %}
        {% endfor %}
</table>
{% endif %}
{% endwith %}
{% endfor %}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your list is not in direct order. Each sublist we have to add in the same table. We can do something like this.

{% for i in range(3) %}
<h4>{{dataList[0][i]}}</h4>
<table border>
    <tr>
        {% for r in dataList[1][i]%}
        <td>{{r}}</td>
        {% endfor %}
    </tr>
    <tr>
        {% for r in dataList[2][i][0]%}
        <td>{{r}}</td>
        {% endfor %}
    </tr>
    <tr>
        {% for r in dataList[3][i]%}
        <td>{{r}}</td>
        {% endfor %}
    </tr>
</table>
{% endfor %}

enter image description here

Here I passed 3 inside range function. This will generate 3 tables. But if you want this as dynamic, you can change something like this range(len(dataList[1])


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...