`

Easyui tree扩展tree方法获取一级子节点

阅读更多

Easyui Tree插件获取子节点的时候用的getChildren方法,但是这个方法获取到的节点却是级联获取了子节点下的子节点,但是在有些时候,我们并不希望他返回这么多。我们只想获取目标节点的一级子节点,那么就我们就需要扩展我们自己的方法了。

这里我扩展了这个方法(getLeafChildren):

$.extend($.fn.tree.methods,{
	getLeafChildren:function(jq, params){
		var nodes = [];
		$(params).next().children().children("div.tree-node").each(function(){
			nodes.push($(jq[0]).tree('getNode',this));
		});
		return nodes;
	}
});

 

 具体的用法和getChildren方法是一样的,只是这个只返回目标节点的第一级子节点。

例把省市县下拉列表展开到市级:

 function expandNode(){
  var combotree_org=$('#admin_elecmg_activity_plan_orgNo').combotree('tree');
  combotree_org.tree('collapseAll');//折叠所有节点
  /**方法一:根据省id展开**/
//   var root=combotree_org.tree('getRoot');//获取根节点
//   combotree_org.tree('expand', root.target);//展开根节点
//   var node = combotree_org.tree('find', 41101);//找到获取国网河南省电力公司
//   combotree_org.tree('expand', node.target);//展开国网河南省电力公司
  
  /**方法二:由根开始遍历一级一级的展开,展开到市级**/
  var roots=combotree_org.tree('getRoots'),children=null,i,j;
  for(i=0;i<roots.length;i++){
   combotree_org.tree('expand', roots[i].target);
   children=combotree_org.tree('getLeafChildren',roots[i].target);
   for(j=0;j<children.length;j++){
    combotree_org.tree('expand', children[j].target);
   }
   alert(children.length);
  }
  /**展开到县级**/
//   var roots=combotree_org.tree('getRoots'),children=null,children2=null,i,j,k;
//   for(i=0;i<roots.length;i++){
//    combotree_org.tree('expand', roots[i].target);
//    children=combotree_org.tree('getLeafChildren',roots[i].target);
//    for(j=0;j<children.length;j++){
//     combotree_org.tree('expand', children[j].target);
//     children2=combotree_org.tree('getLeafChildren',children[j].target);
//     for(k=0;k<children2.length;k++){
//      combotree_org.tree('expand', children2[k].target);
//     }
//    }
//   }
 }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics