级联菜单-初始化

来源:互联网 发布:蔬菜交易软件 编辑:程序博客网 时间:2024/05/01 04:29

级联菜单已经做好了,可以要是编辑的时候,需要把之前数据的值用select表示出来。
思路如下:
用逗号分隔的String字符串表示初始化数据。能够得到第一层的对象,然后赋初值。通过JS的操作得到第一层对象的下一个兄弟对象就是下一个Select,然后赋值。以些类推。
代码如下:

var json = [{'id':'0','name':'河南',parentId:'-1',children:[                    {'id':'01','name':'郑州','parentId':'0','children':                        [{'id':'011','name':'二七区',parentId:'-1',children:'-1'}]},                    {'id':'02','name':'新乡','parentId':'0','children':'-1'}]},                {'id':'1','name':'江苏',parentId:'-1',children:[                    {'id':'11','name':'江苏1','parentId':'1','children':'-1'},                    {'id':'12','name':'江苏2','parentId':'1','children':'-1'}]}];    var Settings = {            id:''    };         var DyCacadeSelect = {        init:function(id,json,path){            Settings.id = id;            var json = DyCacadeSelect.initJson(json);            DyCacadeSelect.recursion(json);            DyCacadeSelect.editInit(path);        },        initJson:function(json){            //json作为另一个json对象的属性值             //失败的操作有:            //1:字符串转成json            //将json对象作为字符串赋值  ----jsonRoot作为字符串合并json----转换JSON            //用eval("("+jsonRoot")")转换为json 成功              //用$.parseJson("{"+jsonRoot+"}") 转换失败               //原因是$.parseJson必须是标准的json格式。例如必须用""括起来属性与属性值            var jsonRoot = {'id':'-1','name':'',parentId:'-2','children':json};               return jsonRoot;        },        editInit:function(paths){             var path = paths.split(",");             $("#-2 option[value='"+path[0]+"']")                .attr("selected",true);             $("#-2").trigger("change");             for(var repeat=0;repeat<path.length-1;repeat++){                 $($("#-2").nextAll("select")[repeat])                    .find("option[value='"+path[parseInt(repeat)+1]+"']")                    .attr("selected",true);                 if(parseInt(repeat)+1<path.length-1){                     $($("#-2").nextAll("select")[repeat])                     .trigger("change");                 }             }        },        isChildren:function(note){            return note.children == '-1';        },        recursion:function(root){            if(DyCacadeSelect.isChildren(root)){                return ;            } else {                DyCacadeSelect.initSelect(root);                $.each(root.children,function(index,comment){                    DyCacadeSelect.initOption(root,comment);                    DyCacadeSelect.initSelectChange(root,comment);                });            }        },        initSelect:function(note){            $("#"+Settings.id).append("<select"+                    " class='select small newselect' name='keyword' id='"+note['parentId']+"'>"+                    "<option value=''>--请选择--</option></select>");        },        initOption:function(note,comment){            $("#"+note['parentId']).append("<option "+                    "value='"+comment['id']+"'>"+comment['name']+"</option>");        },        initSelectChange:function(note,comment){            $("#"+note['parentId']).bind("change",function(){                if($("#"+note['parentId']).val()==''){                    DyCacadeSelect.clearNextAllSelect(note);                }                if(comment['id'] == $("#"+note['parentId']).val()){                    DyCacadeSelect.existNextSelectOperatorClear(note);                    DyCacadeSelect.recursion(comment);                }             });        },        existNextSelectOperatorClear:function(note){            if(DyCacadeSelect.isExistNextSelect(note)){                DyCacadeSelect.clearNextAllSelect(note);                return true;            }            return false;        },        clearNextAllSelect:function(note){            $("#"+note['parentId']).nextAll('select').remove();        },        isExistNextSelect:function(note){            var selectLength = $("select[id="+note['parentId']+"]").length;            if(selectLength>0){return true;}            return false;        }    };    //调用如下    DyCacadeSelect.init("select",json,"0,01,011");
0 0
原创粉丝点击