preface

Previously in the following article, we implemented the method to expand to any level recursively :juejin.cn/post/695679… Later, I encountered the possibility of not expanding some nodes, so I modified part of the previous article to support expanding hierarchical nodes without expanding some nodes.

implementation

Add a regular expression match to the original recursion

Code implementation

/** expand the ztree node b-(true indicates that the obj argument is passed to the ztree object; False: a tree node object is passed in.) childnodes- childnodes, l- which level to expand to, Should be greater than or equal to 0, obj-Ztree or node object (supports ztree incoming or node incoming) Excludes some nodes that do not need to expand
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); }}}}Copy the code

How to use

ZtreeObj is the ztree object (ztreeObj =$.fn.ztree.init (dom, config);)

showztreemenuNum(true,zTreeObj,1.null./ / southeast of northeast + + | | northwest);
Copy the code

Isn’t that easy