ajax使用json下拉框,ajax请求后台得到json数据后动态生成树形下拉框的方法
如下所示:
$(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;
}
department表中的dept_id作為employee表中有的外鍵,生成的Department.java類中有Set對象。從后臺查詢部門表,得到List集合,通過struts2配置:
list
轉(zhuǎn)成json格式后,傳到j(luò)sp頁面,在前臺頁面中處理json數(shù)據(jù),動態(tài)生成下拉樹。
以上這篇ajax請求后臺得到j(luò)son數(shù)據(jù)后動態(tài)生成樹形下拉框的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
總結(jié)
以上是生活随笔為你收集整理的ajax使用json下拉框,ajax请求后台得到json数据后动态生成树形下拉框的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 交易系统单服务器tps,交易者必看!一文
- 下一篇: 全球最大sap hana系统建立在以下哪