溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ajax請求后臺得到json數(shù)據(jù)后如何動態(tài)生成樹形下拉框

發(fā)布時間:2021-06-24 11:16:49 來源:億速云 閱讀:160 作者:小新 欄目:web開發(fā)

這篇文章主要介紹了ajax請求后臺得到json數(shù)據(jù)后如何動態(tài)生成樹形下拉框,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

如下所示:

<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,
//選擇樹節(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>

轉(zhuǎn)成json格式后,傳到jsp頁面,在前臺頁面中處理json數(shù)據(jù),動態(tài)生成下拉樹。

感謝你能夠認真閱讀完這篇文章,希望小編分享的“ajax請求后臺得到json數(shù)據(jù)后如何動態(tài)生成樹形下拉框”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI