ajax請求後臺得到json數據後動態生成樹形下拉框的方法

今天小編就爲大家分享一篇ajax請求後臺得到json數據後動態生成樹形下拉框的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

如下所示:

<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,
//選擇樹節點觸發事件 
 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數據後動態生成樹形下拉框的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章