ztree中獲取選中節點下的全部葉子節點

ztree中獲取選中節點下的全部葉子節點node

 

var setting = {
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
        onClick: treenodeClick
    }
};
spa

//點擊樹節點,獲取節點的全部葉子節點id
function treenodeClick(event, treeId, treeNode, clickFlag) {
    //此處treeNode 爲當前節點
     var str ='' ;
     if(treeNode.isParent){
         str = getAllChildrenNodes(treeNode,str);
     }else{
         str=treeNode.id;
     }
     alert(str); //全部葉子節點ID
}
遞歸

//使用了遞歸,獲得葉子節點的數據
function getAllChildrenNodes(treeNode,result){
    if (treeNode.isParent) {
      var childrenNodes = treeNode.children;
      if (childrenNodes) {
          for (var i = 0; i < childrenNodes.length; i++) {
           if(childrenNodes[i].isParent){
            result = getAllChildrenNodes(childrenNodes[i], result);
           }else{
            result += ',' + childrenNodes[i].id;
           }
          }
      }
  }
  return result;
}
get

相關文章
相關標籤/搜索