采坑总结01

(1) javascript 的条件判断语句 else if中间有空格!

javascrip 中的if else判断语句 else if 写法和Python有区别:
js中的if...else语法是这样的:
if(...){
}else if(...){
}else{
}
else if中间有空格!

(2) 当前端 是通过ajex 向后端发送的请求,那么后端返回数据的时候,就必须注意数据的格式,ajex 支持接收json , xml, 原生字符串。
下方图片中的 第二种写法,返回给前端的时候,会报错。
如果需要返回,多组数据,则需要,自定义构造字典数据类型。
514d2029-59c7-4184-b07c-f7f77dd8ee0a

(3)Jquery 代码中 val()方法,默认是获取 标签中定义的value 值,

但是,如果value没有指定值,并且没有value关键字的话,jquery 的val() 方法 默认获取的是,该标签的文本内容。

function bindSubmitEditModal() {
$('#edit_ajax_submit').click(function () {
var tch_id = $('.edit-models input[name="teacher_id"]').val();
var tch_name = $('.edit-models input[name="name"]').val();
var cls_id_list = $('.edit-models select[name="sec2_d"]').val();
alert('教师ID:'+tch_id+'| 教师姓名:'+tch_name);
alert(cls_id_list);

(4)多对多关系 更新步骤:先更新主表,然后更新第三张关系表

models.Teacher.objects.filter(id=tch_id).update(name=tch_name)
obj = models.Teacher.objects.get(id=tch_id)
# obj.name = tch_name
# obj.save()
obj.cls.set(cls_id_list)

(5) javascript 中的for 循环

写法一:

for(var n=sec2.length;n>0;n--){
            sec1.appendChild(sec2.options[0]);
         }

写法二:

// 往右侧的方框中 添加已教授的班级  
// 这里的 循环参数x 每次的结果 只是一个循环次数的索引值 取值的话 需要用源对象+[x]
for (x in selected_cls_list)
   {var selected_cls_id = selected_cls_list[x][0];
    var selected_cls_names = selected_cls_list[x][1];
    var right_tag_option = document.createElement('option');
    right_tag_option.value = selected_cls_id;
    right_tag_option.innerText = selected_cls_names;
    $('#sec2_d').append(right_tag_option);
   }

(6) Jquery 中的for 循环

$(this).parent().prevAll().each(function () {
        var name = $(this).attr('alex');  
        var text = $(this).text();
        if (name == 'teacher_id') {
           var teacher_id = text;
           $('.edit-models input[name="' + name + '"]').val(teacher_id);}
        }

(7)压缩合并 列表中包含指定样式元组的方法 zip()

# 当前老师的信息
obj = models.Teacher.objects.get(id=nid)
# 获取当前老师已经管理的所有班级
obj_selected_cls_list = obj.cls.all().values_list('id', 'caption')
# print(obj_selected_cls_list)
# [ (1,"root1"),(2,"root2"),(3,"root3"),]

# 构造 已经管理的班级的ID列表 当作中间条件
# 这里zip() 方法的处理结果相当于,只取列表中每个元组对象的第[x]个值,然后提取合并到单独的一个列表中。
id_list = list(zip(*obj_selected_cls_list))[0] if obj_selected_cls_list else []
# print(id_list)
# # [1,2,3]

# 获取该教师ID 当前未教的班级
obj_Unselected_cls_list = models.Classes.objects.exclude(id__in=id_list).values_list('id', 'caption')
# print(obj_Unselected_cls_list)
# < QuerySet[(3, '一班1'), (300, '一班248'), (4003, '1班'), (4004, '2班')] >
posted @ 2017-02-08 18:56  hello-Jesson  阅读(291)  评论(0编辑  收藏  举报