def selectedSort(myList):
#获取list的长度
length = len(myList)
#一共进行多少轮比较
for i in range(0,length-1):
#默认设置最小值得index为当前值
smallest = i
#用当先最小index的值分别与后面的值进行比较,以便获取最小index
for j in range(i+1,length):
#如果找到比当前值小的index,则进行两值交换
if myList[j]['num']>myList[smallest]['num']:
myList[j],myList[smallest] = myList[smallest],myList[j]
3.生成斐波那契序列并排序
def fib(request):
if request.method == 'POST':
mes = {}
num = request.POST.get('num')
if num:
models.Fib.objects.all().delete()
f = Fibonacci(int(num))
for i in f:
models.Fib.objects.create(number = i)
nums = models.Fib.objects.all()
alist = []
for i in nums:
dict = {}
dict['id'] = i.id
dict['num'] = i.number
alist.append(dict)
selectedSort(alist)
def func(key):
return key['id']
sorted(alist,key=func,reverse=True)
mes['message'] = alist
mes['code'] = 200
return HttpResponse(json.dumps(mes))
4.ajax异步加载并拼接多选框删除
<script type="text/javascript">
function fib() {
var num = $('.fib').val()
$.ajax({
url:"/fib/",
type: "post",
dataType:"json",
data:{'num':num},
success:function (res) {
if (res.code == '200'){
var fibs = res.message
console.log(fibs)
var content = '<table border="1"><tr>'+
'<td>id</td>'+
'<td>斐波那契数列</td>'+
'<td>选择</td>'+
'</tr>'
for (var i=0;i<fibs.length;i++){
var fib = fibs[i]
content += '<tr>' +
'<td>'+ fib.id +'</td>'+
'<td>'+ fib.num +'</td>'+
'<td><input type="checkbox" name="del" value="'+ fib.id +'"></td>'+
'</tr>'
}
content += '</table><br> <button type="submit">删除</button>'
$('#table').html(content)
}
else {
alert(res.message)
}
}
})
}
</script>
4.多选删除
def del1(request):
if request.method == 'POST':
num_list = request.POST.getlist('del')
for i in num_list:
models.Fib.objects.filter(id = i).delete()
return redirect('/')