这篇文章主要介绍了ajax请求后台得到json数据后如何动态生成树形下拉框,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
如下所示:
<select id="cc" class="easyui-combotree" name="rempId" data-options="required:true"></select>
<script> $(function(){ $.ajax({ url:"departmentAction_getAllDep.action", type:"post", success:function(result){ //console.log(result); $("#cc").combotree('loadData',b1(result)); } }); $("#cc").combotree({ animate:true, //选择树节点触发事件 onSelect : function(node) { n = node; //返回树对象 var tree = $(this).tree; //选中的节点是否为叶子节点,如果不是叶子节点,清除选中 var isLeaf = tree('isLeaf', node.target); if (!isLeaf) { //清除选中 $("#cc").combotree('clear'); } } }); }); var tree = { id:'', text:'', state:'', checked:'', iconCls:'', attributes:'', children:'' } function b1(result){ var t = []; $.each(result,function(index,dept){ t[index] = b2(dept); }); return t; } function b2(dept){ var tree = new Object(); tree.id = dept.depId; tree.text = dept.depName; tree.state = 'closed'; tree.checked = 'false'; if(dept.employees.length != 0){ tree.children = b3(dept.employees); }else{ tree.children = []; } return tree; } function b3(employees){ var easyTree = []; $.each(employees,function(index,item){ easyTree[index] = b4(item); }); return easyTree; } function b4(item){ var tree = new Object(); tree.id = item.empId; tree.text = item.empName; if(item.empSex == "男"){ tree.iconCls = 'icon-nan'; }else{ tree.iconCls = 'icon-female'; } return tree; } </script>
department表中的dept_id作为employee表中有的外键,生成的Department.java类中有Set<employee>对象。从后台查询部门表,得到List<Department>集合,通过struts2配置:
<action name="departmentAction_*" class="com.chinasoft.action.DepartmentAction" method="{1}"> <result name="getAllDep" type="json"> <param name="root">list</param> </result> </action>
转成json格式后,传到jsp页面,在前台页面中处理json数据,动态生成下拉树。
感谢你能够认真阅读完这篇文章,希望小编分享的“ajax请求后台得到json数据后如何动态生成树形下拉框”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。