级联菜单-初始化
来源:互联网 发布:蔬菜交易软件 编辑:程序博客网 时间: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
- 级联菜单-初始化
- 编辑时,级联菜单的初始化问题
- 级联菜单
- 级联菜单
- 级联菜单
- 级联菜单
- 级联菜单
- 菜单级联
- 菜单级联
- 级联菜单
- 级联菜单
- 级联菜单
- 级联菜单
- 级联菜单
- 级联菜单
- 级联菜单,省市级联菜单
- 级联菜单,两级菜单
- 多级级联菜单
- WAV文件格式分析
- 黑马程序员--protocol代理模式
- android图像图像处理——自定义组件的属性
- Servlet-文件上传@MultipartConfig,Part
- iOS9 HTTP 不能正常使用的解决办法
- 级联菜单-初始化
- 算法与数据结构笔记
- ios9 http请求报错
- 微信主打的语聊功能真能替代打字吗?
- MQTT Paho之Android service源码分析
- Linux 搭建 SVN 服务器端【完全可以执行】
- Servlet-异步执行AsyncContext
- setsockopt :SO_LINGER 选项设置
- 基础数学总结