现在index.html是
<html>
<head>
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/chosen/1.8.2/chosen.jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.css">
</head>
<body>
<select id="mainDD" data-placeholder="Choose" class="chzn-select" style="width:600px;">
{% for i in json_data.items.values %}
<option>{{ i }}</option>
{% endfor %}
</select>
<select name="type" id="type1">
{% for j in json_data.type1.values %}
<option>{{ j }}</option>
{% endfor %}
</select>
<select name="type" id="type2">
{% for k in json_data.type2.values %}
<option>{{ k }}</option>
{% endfor %}
</select>
<select name="type" id="type3">
{% for l in json_data.type3.values %}
<option>{{ l }}</option>
{% endfor %}
</select>
<select name="type" id="type4">
{% for m in json_data.type4.values %}
<option>{{ m }}</option>
{% endfor %}
</select>
<script type="text/javascript">
$(document).ready(function () {
$('#mainDD').on('change', function() {
console.log($(this).val());
console.log($('#mainDD :selected').text()) ;
var thisType = "type" + $(this).val();
for(i=1; i<5; i++) {
var thisId = "type" + i;
console.log(thisType + " " + thisId);
if(thisType !== thisId) {
$("#"+thisId).hide();
}
else {
$("#"+thisId).show();
}
}
}).trigger('change');
});
</script>
</body>
</html>
我想在标签上添加数字。所以我的理想输出是
<select name="type" id="type1">
<option value="1">a-1</option>
<option value="2">a-2</option>
<option value="3">a-3</option>
<option value="4">a-4</option>
</select>
<select name="type" id="type2">
<option value="5">b-1</option>
<option value="6">b-2</option>
<option value="7">b-3</option>
<option value="8">b-4</option>
<option value="9">b-5</option>
</select>
<select name="type" id="type3">
<option value="10">c-1</option>
<option value="11">c-2</option>
</select>
<select name="type" id="type4">
<option value="12">d-1</option>
<option value="13">d-2</option>
<option value="14">d-3</option>
</select>
但现在j&amp; k&amp; l&amp; m被json文件读取,这个jon文件可能会改变内容的数量。例如,现在j
已经
{'type1': [{'---': '---', ‘A’: ‘a’, ‘B’: ‘b’, ‘C: ‘c’, ‘D’: ‘d’}]}
但也许将来j有{'type1': [{'---': '---', ‘A’: ‘a’, ‘B’: ‘b’}]}
或{'type1': [{'---': '---', ‘A’: ‘a’, ‘B’: ‘b’, ‘C: ‘c’, ‘D’: ‘d’,'E':'e','F','f'}]}
所以我想通过使用值的数量来显示这些值,但我无法理解我是如何做到的。我该怎么写它?
views.py是
from collections import OrderedDict
from django.shortcuts import render
import json
def index(request):
with open('./data/data.json', 'r') as f:
json_data = json.loads(f.read(), object_pairs_hook=OrderedDict)
return render(request, 'index.html', {'json_data': json_data})
通过查看答案,我在index.html中写道
{% preprocessed = [] %}
{% counter = 0 %}
{% for key in ["type1", "type2", "type3", "type4"]: %}
{% values = [(i + counter, value) for i, value in enumerate(json_data[key].values())] %}
{% preprocessed.append((key, values)) %}
{% counter = len(data[key]) %}
{% for key, values in preprocessed %}
<select name="type" id="{{ key }}">
{% for counter, value in values %}
<option value="{{ counter }}">{{ value }}-{{counter}}</option>
{% endfor %}
</select>
{% endfor %}
答案 0 :(得分:0)
不确定为什么你需要顺序编号,因为你可以用jQuery做一些聪明的事情,但是如果你想要显示你的输出,你可以预处理你的json_data(在views.py中):
from collections import OrderedDict
from django.shortcuts import render
import json
def index(request):
with open('./data/data.json', 'r') as f:
json_data = json.loads(f.read(), object_pairs_hook=OrderedDict)
preprocessed = []
counter = 0
for key in ["type1", "type2", "type3", "type4"]:
values = [(i + counter, value) for i, value in enumerate(json_data[key].values())]
preprocessed.append((key, values))
counter = len(data[key])
return render(request, 'index.html', {'preprocessed': preprocessed})
然后在你的上下文中传递给index.html:
{% for key, values in preprocessed %}
<select name="type" id="{{ key }}">
{% for counter, value in values %}
<option value="{{ counter }}">{{ value }}-{{counter}}</option>
{% endfor %}
</select>
{% endfor %}