<!DOCTYPE html> <HTML> <HEAD> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="js/jquery-3.3.1.js"></script> <link rel="stylesheet" href="bower_components/ztree/css/zTreeStyle/demo.css" type="text/css"> <link rel="stylesheet" href="bower_components/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css"> <script type="text/javascript" src="bower_components/ztree/js/jquery-1.4.4.min.js"></script> <script type="text/javascript" src="bower_components/ztree/js/jquery.ztree.core-3.5.js"></script> <SCRIPT type="text/javascript"> var setting = { data: { key: { title: "t" }, simpleData: { enable: true } }, view: { fontCss: getFontCss } }; var nodes =[ { id:1, pId:0, name:"2", t:"id=1", open:true}, { id:11, pId:1, name:"22", t:"id=11"}, { id:12, pId:1, name:"24", t:"id=12"}, { id:13, pId:1, name:"26", t:"id=13"}, { id:14, pId:1, name:"28", t:"id=14"}, { id:2, pId:0, name:"节点搜索演示 2", t:"id=2", open:true}, { id:21, pId:2, name:"可以只搜索一个节点", t:"id=21"}, { id:22, pId:2, name:"可以搜索节点集合", t:"id=22"}, { id:23, pId:2, name:"搜我吧", t:"id=23"}, { id:3, pId:0, name:"节点搜索演示 3", t:"id=3", open:true }, { id:31, pId:3, name:"我的 id 是: 31", t:"id=31"}, { id:32, pId:31, name:"我的 id 是: 32", t:"id=32"}, { id:33, pId:32, name:"我的 id 是: 33", t:"id=33"} ]; function focusKey(e) { if (key.hasClass("empty")) { key.removeClass("empty"); } } function blurKey(e) { if (key.get(0).value === "") { key.addClass("empty"); } } var lastValue = "", nodeList = [], fontCss = {}; function clickRadio(e) { lastValue = ""; searchNode(e); } function searchNode(e) { var zTree = $.fn.zTree.getZTreeObj("sys"); if (!$("#getNodesByFilter").attr("checked")) { //如果被选中getNodesByFilter方法 var value = $.trim(key.get(0).value); //得到搜索框中的值,传给value var keyType = ""; //初始化查询条件 if ($("#name").attr("checked")) { keyType = "name"; } else if ($("#level").attr("checked")) { keyType = "level"; value = parseInt(value); } else if ($("#id").attr("checked")) { keyType = "id"; value = parseInt(value); //如果是根据level和id查的则强制转换为int类型 } if (key.hasClass("empty")) { //如果搜索框为空,则value为空 value = ""; } if (lastValue === value) return; lastValue = value; //如果查询值不变,则直接返回下次结果 if (value === "") return; updateNodes(false); if ($("#getNodeByParam").attr("checked")) { //如果被选中getNodesByParam方法 var node = zTree.getNodeByParam(keyType, value); //把指定的了结点(查到的第一个)给node if (node === null) { nodeList = []; } else { nodeList = [node]; } } else if ($("#getNodesByParam").attr("checked")) { nodeList = zTree.getNodesByParam(keyType, value); //把指定的结点给nodeList } else if ($("#getNodesByParamFuzzy").attr("checked")) { //模糊查询 nodeList = zTree.getNodesByParamFuzzy(keyType, value); } } else { updateNodes(false); nodeList = zTree.getNodesByFilter(filter); } updateNodes(true); } function updateNodes(highlight) { //如果结点数据变了,则动态更新所有ztree结点数据 var zTree = $.fn.zTree.getZTreeObj("sys"); for( var i=0, l=nodeList.length; i<l; i++) { nodeList[i].highlight = highlight; // 高亮显示查到的结果 zTree.updateNode(nodeList[i]); } } function getFontCss(treeId, treeNode) { return (!!treeNode.highlight) ? {color:"#A60000", "font-weight":"bold"} : {color:"#333", "font-weight":"normal"}; } function filter(node) { //过滤器 return !node.isParent && node.isFirstNode; } var key; $(document).ready(function(){ $.fn.zTree.init($("#sys"), setting, nodes); key = $("#key"); key .bind("focus", focusKey) .bind("blur", blurKey) .bind("propertychange", searchNode) .bind("input", searchNode); $("#name").bind("change", clickRadio); $("#level").bind("change", clickRadio); $("#id").bind("change", clickRadio); $("#getNodeByParam").bind("change", clickRadio); $("#getNodesByParam").bind("change", clickRadio); $("#getNodesByParamFuzzy").bind("change", clickRadio); $("#getNodesByFilter").bind("change", clickRadio); }); </SCRIPT> </HEAD> <BODY> <div class="content_wrap"> <div class="zTreeDemoBackground left"> <ul id="sys" class="ztree"></ul> </div> <div class="right"> <ul class="list"> <input type="text" id="key" value="" class="empty" /><br/> <!--搜索框--> <input type="radio" id="name" name="keyType" class="radio" checked /><span>name</span><br/> <!--搜索依据--> <input type="radio" id="level" name="keyType" class="radio" /><span>level</span><br/> <input type="radio" id="id" name="keyType" class="radio" /><span>id</span><br/> <input type="radio" id="getNodeByParam" name="funType" class="radio" /><span>getNodeByParam</span><br/> <input type="radio" id="getNodesByParam" name="funType" class="radio" /><span>getNodesByParam</span><br/> <input type="radio" id="getNodesByParamFuzzy" name="funType" class="radio" checked /><span>getNodesByParamFuzzy </span><br/> <input type="radio" id="getNodesByFilter" name="funType" class="radio" /><span>getNodesByFilter</span><br/> </ul> </div> </div> </BODY> </HTML>