本文参考http://blog.csdn.net/m0_37355951/article/details/78004994?locationNum=4&fps=1
用jstreel是因为公司要用一下无限极的树结构;而后台给的数据又不是同一种代码风格
代码如下``` <!doctype html>
<html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width"> <title>jstree 插件</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script> </head> <body> <div id="jstree"></div> <script type="text/javascript"> var isclick=true; $(function () { $('#jstree').jstree({ // 'plugins': ["checkbox", "state"], 'core': { 'data': [{ "id": "root", "text": "sxsf", "state": { //默认状态展开 "opened": true }, "children": [] }], 'check_callback': true } }); });
function gettree(parentsid){
$.ajax({
type:"get",
url:parentsid=="root"?"getFacultyListByUserIdAndCollegeIdLast":"getNextFacultyListById",
async:true,
data:{
tokenId:tokenId,
userId:userId,
collegeId:collegeId,
id:parentsid
},
success:function(res){
if(res.code==200){
var data=res;
$.each((parentsid=="root"?data.body:data.body.list),function(index,obj){
obj.text=obj.name;
createNode("#"+parentsid, obj.id, obj.name,"last");
})
isclick=true;
}
}
});
}
// API createNode(parent, id, text, position).
// parent:在该节点下创建,id: 新节点id, text:新节点文本, position:插入位置
// 封装一个函数动态创建节点
function createNode(parent_node, new_node_id, new_node_text, position) {
debugger;
if($("#"+new_node_id).length>0){//此处是为了防止重复创建
}else{
$('#jstree').jstree('create_node', $(parent_node), {
"text":new_node_text,
"id":new_node_id,
"children": [],
"state": { //默认状态展开
"opened": true
}
}, position, false, false);
}
}
</script>
</body> </html> ```