1. 程式人生 > >zTree樹形中的搜尋定位

zTree樹形中的搜尋定位

<!DOCTYPE html>
<html lang="en">
	    <head>
			<meta charset="UTF-8">
			<title>Document</title>
		</head>
  <body>
		<div> 
			<input id="keyword" type="text" placeholder="搜尋關鍵字"> 
			<!--<button id="search-bt">搜尋</button>-->
</div> <div class="content" style="width:250px; height:362px;"> <ul id="treeA" class="ztree"></ul> </div> <script> $(function(){ init(); setCheck(); }) function setCheck() { var zTree = $.fn.zTree.getZTreeObj("treeA"); type =
{ "Y":p + s, "N":p + s}; zTree.setting.check.chkboxType = type; showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "' + type.N + '" };'); } function init(){ var zNodes=[ { id:123456, pId:0, name:"西瓜", open:true}, { id:1, pId:123456, name:"蘋果", open:true}, { id:
11, pId:1, name:"檸檬", open:true}, { id:111, pId:11, name:"菠蘿"}, { id:112, pId:11, name:"香蕉"}, { id:12, pId:1, name:"橘子", open:true}, { id:121, pId:12, name:"哈密瓜"}, { id:122, pId:12, name:"楊桃"}, { id:123, pId:12, name:"水蜜桃"}, { id:124, pId:12, name:"梨子"}, { id:125, pId:12, name:"柿子"}, { id:126, pId:12, name:"葡萄"}, { id:127, pId:12, name:"提子"}, { id:128, pId:12, name:"櫻桃"}, { id:2, pId:123456, name:"部門2", checked:true, open:true}, { id:21, pId:2, name:"部門2.1"}, { id:22, pId:2, name:"部門2.2", open:true}, { id:221, pId:22, name:"部門2.2.1", checked:true}, { id:222, pId:22, name:"部門2.2.2"}, { id:23, pId:2, name:"部門2.3"}, { id:24, pId:2, name:"部門2.4"}, { id:25, pId:2, name:"部門2.5"}, { id:26, pId:2, name:"部門2.6"}, { id:27, pId:2, name:"部門2.7"}, { id:28, pId:2, name:"部門2.8"}, { id:29, pId:2, name:"部門2.9"} ]; var setting = { check: { enable: true }, data: { simpleData: { enable: true } }, }; zTreeObj = $.fn.zTree.init($("#treeA"), setting, zNodes); $("#search-bt").click(searchNodes); }; //用按鈕查詢節點 function searchNodes(){ var treeObj = $.fn.zTree.getZTreeObj("treeA"); var keywords=$("#keyword").val(); //根據節點資料的屬性搜尋,獲取條件模糊匹配的節點資料 JSON 物件集合,(keywords)模糊匹配只能針對 String 型別的資料 var nodes = treeObj.getNodesByParamFuzzy("name", keywords, null); if (nodes.length>0) { treeObj.selectNode(nodes[0]); //會讓節點自動滾到到可視區域內 } } //input框變化時查詢節點 document.getElementById("keyword").addEventListener("input", test, false); function test(){ var treeObj = $.fn.zTree.getZTreeObj("treeA"); var keywords=$("#keyword").val(); var nodes = treeObj.getNodesByParamFuzzy("name", keywords, null); if (nodes.length>0) { treeObj.selectNode(nodes[0]); } } </script> //treeObj.getNodesByParamFuzzy();方法和treeObj.selectNode();不明之處可參考 zTree v3.5.30 API 文件 </body> </html>