jquery ztree implements the search function of the tree

  • 2021-01-06 00:28:57
  • OfStack

This article examples to share jquery ztree tree search function, for your reference, the specific content is as follows


var userZTree; 
var userSetting={ 
 check: { 
  enable: true, 
  chkStyle: "radio", 
  chkboxType : {"Y" : "" , "N" : ""}, 
  radioType: "all" 
 }, 
 data: { 
  simpleData: { 
  enable: true, 
  idKey : "id", 
  pIdKey : "pid" 
  } 
 }, 
 callback:{ 
  onClick : clickCheck 
 }, 
 view :{ 
  showIcon: false, 
  fontCss: getFontCss 
 } 
}; 

view: {fontCss: getFontCss}
Here is a method getFontCss writes for itself:


function getFontCss(treeId, treeNode) { 
 return (!!treeNode.highlight) ? {color:"#A60000", "font-weight":"bold"} : {color:"#333", "font-weight":"normal"}; 
} 

So you can achieve color change;
Next, add a search input box to the top of your display tree:


<div id="userDiv" class="showParentDiv showDiv" style="z-index:105;display: none;"> 
 <div class="grayBg"> 
  <div class="toolbar"> 
  <input type="button" value=" <s:text name='button.submit'/> " onclick="submitUser();"/> 
  <input type="button" value=" <s:text name='button.cancel'/> " onclick="closeUserDiv();"/> 
  <input type="button" value="  new  " onclick="toAddDiv();"/> 
 </div> 
 </div> 
 <div style="text-align:left;margin:5px;height: 15px;"> Filtering by name: <input type="text" id="dicKey" onkeyup="changeColor('userTree','name',this.value)"/></div> 
 <ul id="userTree" class="ztree" style="height:350px; overflow-y:scroll;"></ul> 
</div>

Here you can see that the changeColor method is called:


// Using search data   Highlight function required 2 step  
//1. in tree the setting  the view  I'm going to put in Settings  fontCss: getFontCss  Set up the  
//2. in ztree Above the container, add 1 A text box, and add onkeyup Event, which invokes a fixed method  changeColor(id,key,value )  
// id Refers to the ztree Of the container id . 1 As for the ul . key Refers to the ztree Which attribute of the node's data is the condition to filter ,value Refers to the filter condition, the filter is fuzzy filter  
function changeColor(id,key,value){ 
 treeId = id; 
 updateNodes(false); 
 if(value != ""){ 
 var treeObj = $.fn.zTree.getZTreeObj(treeId); 
 nodeList = treeObj.getNodesByParamFuzzy(key, value); 
 if(nodeList && nodeList.length>0){ 
  updateNodes(true); 
 } 
 } 
} 
function updateNodes(highlight) { 
 var treeObj = $.fn.zTree.getZTreeObj(treeId); 
 for( var i=0; i<nodeList.length; i++) { 
 nodeList[i].highlight = highlight; 
 treeObj.updateNode(nodeList[i]); 
 } 
} 

treeObj.getNodesByParamFuzzy(key, value); 

Is the retrieved ztree function;
In this way, ok can realize the search function.

For more information on ztree controls, see the special topic jQuery plug-in ztree usage summary.

The above is for everyone to analyze the ztree tree search function of the relevant information, I hope to be able to learn for everyone.


Related articles: