Ztree expands to the specified level, and supports regular expression matching mode to not expand certain specified nodes

Ztree expands to the specified level, and supports regular expression matching mode to not expand certain specified nodes

Preface

Previously, in the following article, the method of expanding to any level was realized through recursion.

The realization method of ztree tree menu control expansion to arbitrary nodes

Later, I encountered whether it was necessary not to expand certain nodes, so I made some modifications to the previous article to support expanding the level nodes without expanding certain nodes.

Some digressions: ①I didn’t expect this handwritten ztree note to be reprinted by many people and changed on this basis when it was reprinted, but I still hope that everyone can indicate the original author and article address when reprinting, thanks for supporting eguid original technology sharing And practice.

Method to realize

Add a regular expression to match the original recursion

Code

/**
展开全部ztree树节点
b-(true表示obj参数传入的是ztree对象;false表示传入的是树节点对象)
childnodes-子节点,
l-要展开到哪个层级,大于等于0即可
obj-ztree对象或者节点对象(支持ztree对象传入和节点对象传入)
excludes-排除某些不需要展开的节点的正则表达式
*/
function showztreemenuNum(b,childnodes,l,obj,excludes) {
    
    
	if(b){
    
    
		var rootnodes = childnodes.getNodes();
		showztreemenuNum(false,rootnodes,l,childnodes,excludes);
	}else{
    
    
		var len=-1;
		if(!isNull(childnodes)&&!isNull((len=childnodes.length))&&len>0){
    
    
			if(l<=childnodes[0].level){
    
    
				return;
			}
			
			for (var i = 0; i < len; i++) {
    
    
				if(excludes!=null){
    
    
					if(excludes.test(childnodes[i].name)){
    
    
						continue;
					}
				}
				obj.expandNode(childnodes[i], true, false, false, true);
				var child=childnodes[i].children;
				showztreemenuNum(false,child,l,obj,excludes);
			}
		}
	}
}

how to use

ztreeObj is a ztree object (zTreeObj=$.fn.zTree.init(dom, config);)

showztreemenuNum(true,zTreeObj,1,null,/东北+|西北+|东南/);

Guess you like

Origin blog.csdn.net/eguid/article/details/106144432