本文实例为大家分享了jquery treeview树形结构的应用代码,供大家参考,具体内容如下
继bootstrap-treeview应用后,我又尝试了用jquery-treeview解决这个问题,记录我的解决方案,但是不一定是最优。
引入必备css
- jquery.treeview.css
引入必备js
- jquery-3.0.0.js
- jquery.treeview.js
编写页面treeview_jquery.html
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>treeviewbyjquery</title>
<link rel="stylesheet">
<script src="../static/js/jquery-3.0.0.js"></script>
<script src="../static/js/jquery.treeview.js"></script>
</head>
<script>
$(function () {
$.ajax({
type:"get",
url:"/tree/treeview.do", //后台接口路径
async:false, //非异步
datatype:"json", //数据格式为json
success:function (data) {
var html = buildtree(data); //调用buildtree()构建树形结构
$("#tree").append(html); //将树形结构追加到dom元素中
}
});
$("#tree").treeview({});//通过jquery.treeview将构建好的属性结构变成一个动态的树
});
/*
递归访问后台返回的数据,拼html代码构建树形结构
*/
var buildtree = function(data){
var html="";
$.each(data,function(i,n){ //遍历当前数据中的所有树节点
html = html+"<li><span class="folder">"+n.text+"</span>"; //当前节点为父节点
var children = buildtree(n.nodes); //递归遍历当前节点的所有子节点
html = html+"<ul>"+children+"</ul>"; //将父节点与子节点拼在一起
})
return html;//返回构建的树形结构
}
</script>
<body>
<ul id="tree" class="filetree"></ul>
</body>
</html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
石林22632073