layuiExtend
layuiExtend copied to clipboard
在没有默认展开所有节点的情况下,搜索之后“苏”之后节点折合,点击几点等同没有搜索


因为数据量比较大,层级较多,不会在初始化是展开所有节点。我主要使用了搜索功能,但是在搜索后,节点完全折合, 如果在此时点击节点,会重新展开该节点下的所有子节点,包括除了我搜索以外的数据, 所以建议在搜索后自动展开所有节点。谢谢!
@daodao90 节点隐藏有两种情况,一种是已经渲染了节点,还有一种是没有渲染节点,你加的这个只适用已渲染的节点,但是对未渲染的节点不太好做,所以目前还是搜索之后如果原来是折合的,还是折合,但是点击之后只显示搜索到的节点,未搜索到的节点不显示了
搜索之后,几点都是折合的,而且子节点是没有被渲染的,我有尝试过手动渲染,但是没成功,最后发现,按照expandOnClick的逻辑,加上一个样式就能实现,子节点也被渲染了,可以直接显示。
以下代码也可以实现在搜索匹配后自动展开所有节点:

search: function(value) { var that = this; var options=this.config; if(!options.searchNodeMethod || typeof options.searchNodeMethod !== "function"){ return; } var data=options.data; //dai-s var vi = function(children) { for (var i=0; i<children.length; i++) { if (options.searchNodeMethod(value, children[i])) { return true; } var cc = children[i][options.request.children]; if (cc && cc.length>0) { if (vi(cc)) { return true; } } } return false; }; //dai-e // 数据递归 var traverse=function(data) { data.forEach(function(val,index) { // 所有查找到的节点增加属性 val.visible=options.searchNodeMethod(value,val); if(val[options.request.children] && val[options.request.children].length>0){ //dai-s if (vi(val[options.request.children])) { that.expandNode(val[options.request.key]); } //dai-e traverse(val[options.request.children]); } //如果当前节点属性为隐藏,判断其子节点是否有显示的,如果有,则当前节点改为显示 if(!val.visible){ var childSomeShow = false; if(val[options.request.children] && val[options.request.children].length>0){ childSomeShow=val[options.request.children].some(function(v,i) { return v.visible; }) } val.visible = childSomeShow; } // 通过节点的属性,显示隐藏各个节点,并添加删除搜索类 var el=options.elem.find("[data-"+options.customKey+"='"+val[options.request.key]+"']"); if(val.visible){ el.show().removeClass("eleTree-search-hide"); // 判断父节点是否展开,如果父节点没有展开,则子节点也不要显示 var parentEl=el.parent(".eleTree-node-group").parent(".eleTree-node"); var isParentOpen=parentEl.children(".eleTree-node-content").children(".eleTree-node-content-icon").children(".layui-icon.layui-icon-triangle-r").hasClass("icon-rotate") if((parentEl.length>0 && isParentOpen) || parentEl.length===0){ el.show(); } }else{ el.hide().addClass("eleTree-search-hide"); } // 删除子层属性 // if(val[options.request.children] && val[options.request.children].length>0){ // val[options.request.children].forEach(function(v,i) { // delete v.visible; // }) // } }) }