ajax請求后臺得到json數(shù)據后動態(tài)生成樹形下拉框的方法
更新時間:2018年08月13日 15:36:02 作者:Amy_Queen
今天小編就為大家分享一篇ajax請求后臺得到json數(shù)據后動態(tài)生成樹形下拉框的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:
<select id="cc" class="easyui-combotree" style="width:580px;" 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, //選擇樹節(jié)點觸發(fā)事件 onSelect : function(node) { n = node; //返回樹對象 var tree = $(this).tree; //選中的節(jié)點是否為葉子節(jié)點,如果不是葉子節(jié)點,清除選中 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數(shù)據,動態(tài)生成下拉樹。
以上這篇ajax請求后臺得到json數(shù)據后動態(tài)生成樹形下拉框的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Ajax引擎 ajax請求步驟詳細代碼
- vue項目使用axios發(fā)送請求讓ajax請求頭部攜帶cookie的方法
- 為jquery的ajax請求添加超時timeout時間的操作方法
- 通過Ajax請求動態(tài)填充頁面數(shù)據的實例
- 通過jquery的ajax請求本地的json文件方法
- jQuery中ajax請求后臺返回json數(shù)據并渲染HTML的方法
- ajax請求后臺接口數(shù)據與返回值處理js的實例講解
- jquery 通過ajax請求獲取后臺數(shù)據顯示在表格上的方法
- Python基于分析Ajax請求實現(xiàn)抓取今日頭條街拍圖集功能示例
- 關于Ajax異步請求后臺數(shù)據進行動態(tài)分頁功能
- 爬取今日頭條Ajax請求
相關文章
Ajax獲取到數(shù)據放入echarts里不顯示的原因分析及解決辦法
在做一個需要用到echarts地圖的項目的時候,成功通過ajax獲取到了后臺提供的數(shù)據,并生成了想要的JSON串。但是,放到echarts option.series[0].data里,獲取不到數(shù)據。在生成的地圖上無法看到你從后臺獲取到的值,下面小編給大家分享我的解決辦法,需要的朋友參考下2016-01-01