ajax请求后台得到json数据后动态生成树形下拉框的方法(ajax获取json数据返回给表格)硬核推荐

随心笔谈12个月前发布 admin
93 0


<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>

© 版权声明

相关文章