我有这样的代码.. JS
$(".id_sort").bind('keypress',function(e){
if (e.keyCode == '13'){
var all_name = [];
var all_pass = [];
var index_fix = [];
var index_ori = [];
for (i=0;i < $("tbody tr").length; i++){
index_ori.push(i);
}
$(".id_sort").each(function(){
index_fix.push(Number($(this).val())-1);
});
if (JSON.stringify(index_fix) !== JSON.stringify(index_ori)){
data = { key : 'sort', index : JSON.stringify(index_fix)};
$.ajax({
url : "/ajax/",
data : data,
dataType : "json",
type : "POST",
cache : false,
success : function (resp){
$(".data").html(resp);
}
});
}
else {
alert("data sama coy");
}
}
});
和django查看
if request.POST.get('key') == 'sort':
index = json.loads(request.POST.get('index'))
urut_fix = [int(i) for i in index]
files = open('log.txt','r')
read = files.readlines()
data = []
try :
for i in read:
data.append(i)
except ValueError:
pass
urut_prev = range(len(data))
for i in urut_prev :
if i != (urut_fix[i]):
get_data2 = data[i]
data.pop(i)
data.insert(urut_fix[i],get_data2)
files.close()
reads = open('log.txt', 'w')
for y in data:
reads.write(y)
foo = []
for i in data:
foo.append(json.loads(i))
return render_to_response("sort.html",locals())
code sort.html
{% for i in foo%}
<tr>
{% if i.name and i.pwd%}
<td class="name">{{ i.name}}</td>
<td class="pass">{{i.pwd}}</td>
<td>
<input class="id_sort" type="text" name="sort" value="{{forloop.counter}}"/>
</td>
<td class="up" value="{{forloop.counter}}"onclick="up(this);">up</td>
<td class="down" value="{{forloop.counter}}" onclick="down(this);">down</td>
<td class="del" value="{{forloop.counter}}" onclick="del(this);">del</td>
{% endif%}
</tr>
{% endfor%}
和我这样的模板
<div class="hasil">
<table>
<thead>
<tr>
<th>Name</th>
<th>Password</th>
<th>Sort</th>
<th>Up</th>
<th>Down</th>
<th>Delete</th>
</tr>
</thead>
<tbody class="data">
{% for i in foo%}
<tr>
{% if i.name and i.pwd%}
<td class="name">{{ i.name}}</td>
<td class="pass">{{i.pwd}}</td>
<td>
<input class="id_sort" type="text" name="sort" value="{{forloop.counter}}"/>
</td>
<td class="up" value="{{forloop.counter}}"onclick="up(this);">up</td>
<td class="down" value="{{forloop.counter}}" onclick="down(this);">down</td>
<td class="del" value="{{forloop.counter}}" onclick="del(this);">del</td>
{% endif%}
</tr>
{% endfor%}
</tbody>
</table>
我希望添加对类数据的响应,.i获取响应但不添加到类,我的代码中有什么错误?我非常困惑我在django的新手..谢谢提前:D