分享一个zTree api文档 这是链接:zTree api文档
下面贴一些自己敲的代码,在文档中都介绍的很清楚,每一项都能找到,我就不备注了。
$(document).ready(function(){
ansyTree();
$("#submitForm").click(function(){
var nodes = treeObj.getCheckedNodes(true);
if(nodes.length==0){
$.messager.alert('提示','请选择要分配的资源....' );
return ;
}
var mdId="";
for(var i=0;i<nodes.length;i++){
if(nodes[i].id!='0')
mdId+=","+nodes[i].id;
}
$.ajax({
type : 'post',
url:parent.basePath+'server/role/insertRoleModule.do',
dataType:"json",
data:{
ROLE_ID:roleId,
MD_ID:mdId.substr(1)
},
success: function (retVal) {//回调函数
$.messager.alert('提示',retVal.message);
},
beforeSend:function(){
$.messager.progress({title : '提示', text : '数据处理中,请稍后....'});
},
complete :function(){
$.messager.progress('close');
}
});
});
});
var treeObj;
function ansyTree(){
var rootNode = [
{id:0,"name":"系统菜单(根目录)",isParent:true, open:true}
]
treeObj= $.fn.zTree.init($("#treeDemo"), {
treeId:'moduleTree',
async: {
enable: true,
url : parent.basePath+'server/role/selectRoleModuleTree.do',
type:"post",
autoParam: ["id=PM_ID"],
otherParam: {"ROLE_ID":roleId},
dataFilter: null
},
callback: {
onAsyncSuccess:function(event, treeId, treeNode, msg){
expandNodes(treeNode.children);
}
},
view: {
dblClickExpand: false
},
check: {
enable: true
}
},rootNode);
expandNodes(treeObj.getNodes());
}
function expandNodes(nodes){
if (!nodes) return;
for (var i=0, l=nodes.length; i<l; i++) {
treeObj.expandNode(nodes[i], true, false, false);
}
}