树呢是一个最简单的树,并没有做一异步加载,也就是一个筛选,然后跳到第一个符合删选的数据下,并且所有符合的都会被展开和选中。其中ztreeAry是一个模拟的本地数组json。在test.json中,如果需要你可以自己创建一个。
<html>
<head>
<meta charset="utf-8">
<title>ztree 筛选</title>
<link rel="stylesheet" href="zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="test.json"></script>
<script type="text/javascript">
$(function() {
var setting = {
data: {
simpleData: {
enable: true,
idKey: "id",
pIdKey: "parentId",
rootPId: -1
},
key: {
url:"nourl",
name:"name"
}
}
};
var zTreeObj=$.fn.zTree.init($("#treeDemo"), setting,ztreeAry);
// zTreeObj.expandAll(true);
});
//搜索框
$(function() {
$('#sch').keydown(function(e) {
if(e.keyCode==13){
var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
var value = $('#sch').val();
var nodeList = treeObj.getNodesByParamFuzzy('name', value);
treeObj.cancelSelectedNode();
var nodes = treeObj.getNodes();
for(var j=0;j<nodes[0].children.length;j++){
treeObj.expandNode(nodes[0].children[j], false, true, false);
}
if(value){
if (nodeList.length>0) {
for(var i=0;i<nodeList.length;i++){
treeObj.expandNode(nodeList[i], true, false, false);
treeObj.selectNode(nodeList[i],true);
}
}
}
}
});
});
</script>
</head>
<body>
搜索:<input type="text" id="sch" placeholder="回车定位">
<br> 树状:
<div style="height:300px;width:200px;">
<ul id="treeDemo" class="ztree"></ul>
</div>
</body>
</html>