首页 > 代码库 > angularJS-Ztree

angularJS-Ztree

官网:http://www.treejs.cn/v3/main.php#_zTreeInfo

自定义指令

/**
 * name: public.ztree
 * Version: 1.0.0 beta
 */
define([‘publicZtree‘], function () {
angular.module(‘public.ztree‘, []).directive(‘tree‘,[function(){
     return { 
        require: ‘ngModel‘, 
        restrict: ‘A‘, 
        link: function ($scope, element, attrs, ngModel) { 
          var setting = { 
            data: { 
              key: { 
                title: "branchName"
              }, 
              simpleData: { 
                enable: true
              } 
            }, 
            callback: { 
              onClick: function (event, treeId, treeNode, clickFlag) { 
                  BRANCHID = treeNode.branchId;
                  BranchID = treeNode.branchId;
                $scope.$apply(function () { 
                  ngModel.$setViewValue(treeNode); 
                }); 
              } 
            },
          }; 
          
          //向控制器发送消息,进行菜单数据的获取 
          $scope.$emit("treeCtrl",attrs["1"]);
          $scope.$on("menuData",function(event,data){ //监听或接收数据。。用于接收event与data
              $.fn.zTree.init($("#tree"), setting, data);//进行初始化树形菜单 
              //$.fn.zTree.init(element, setting, data);//进行初始化树形菜单 element
              var zTree = $.fn.zTree.getZTreeObj("tree");//zTree 的 DOM 容器的 id
              var selectName = $("#selectName").val();
              if(typeof selectName == "undefined" || selectName == ""){ 
                zTree.selectNode(zTree.getNodeByParam("tree","1"));//默认第一个选中 
                $("#selectName").val(zTree.getSelectedNodes()[0]);//赋值 
              }else{ 
                for(var i =0; i<data.length;i++){ 
                  if(data[i]["branchId"] == selectName ){ 
                    zTree.selectNode(zTree.getNodeByParam("branchId", data[i]["branchId"])); 
                  } 
                }
              } 
          });
          
          
          $scope.$on("menuData1",function(event,data){ //监听或接收数据。。用于接收event与data
              $.fn.zTree.init($("#tree1"), setting, data);//进行初始化树形菜单 
              //$.fn.zTree.init(element, setting, data);//进行初始化树形菜单 element
              var zTree = $.fn.zTree.getZTreeObj("tree");//zTree 的 DOM 容器的 id
              var selectName = $("#selectName").val();
              if(typeof selectName == "undefined" || selectName == ""){ 
                //zTree.selectNode(zTree.getNodeByParam("tree","1"));//默认第一个选中 
                //$("#selectName").val(zTree.getSelectedNodes()[0]);//赋值    //因为修改时selectNode是空的 赋值失败报错,所以注释
              }else{ 
                for(var i =0; i<data.length;i++){ 
                  if(data[i]["branchId"] == selectName ){ 
                    zTree.selectNode(zTree.getNodeByParam("branchId", data[i]["branchId"])); 
                  } 
                } 
              } 
          });
          
          
          $scope.$on("menuData2",function(event,data){ //监听或接收数据。。用于接收event与data
              $.fn.zTree.init($("#tree2"), setting, data);//进行初始化树形菜单 
              //$.fn.zTree.init(element, setting, data);//进行初始化树形菜单 element
              var zTree = $.fn.zTree.getZTreeObj("tree");//zTree 的 DOM 容器的 id
              var selectName = $("#selectName").val();
              if(typeof selectName == "undefined" || selectName == ""){ 
                //zTree.selectNode(zTree.getNodeByParam("tree","1"));//默认第一个选中 
                //$("#selectName").val(zTree.getSelectedNodes()[0]);//赋值    //因为修改时selectNode是空的 赋值失败报错,所以注释
              }else{ 
                for(var i =0; i<data.length;i++){ 
                  if(data[i]["branchId"] == selectName ){ 
                    zTree.selectNode(zTree.getNodeByParam("branchId", data[i]["branchId"])); 
                  } 
                } 
              } 
          });
          
        } 
      }; 
}]);
});
控制器
//获取树节点
         $scope.$on("treeCtrl",function(event,params){ 
                var CoreUrl = new coreUrl({"transCode":"B000300"});
                    CoreUrl.$save(function(data){
                        //发送消息给子控制器 
                        $scope.mechanismList =data.row;
                        $scope.$broadcast("menuData",$scope.mechanismList);
                    },function(error){
                        alert(JSON.stringify(error)); 
                    });
             $scope.modifyMechanismList = function (){
                 $(‘#addUser‘).modal();
                 //$(‘body‘).removeClass("open_modal");
                 
                $scope.BRANCHID = BRANCHID;
                var CoreUrl = new coreUrl({"transCode":"B000300","branchId":$scope.BRANCHID});
                CoreUrl.$save(function(objc){
                     $scope.organization_query2 = objc;
                     $scope.parentBranchId_choose = objc.row[0].branchName;
                   },function(error){
                           alert(JSON.stringify(error)); 
                   });
            };
         });
<div class="modal-body">
               <ul tree id="tree" color:#dcdcdc;" class="ztree" ng-model="selectNode" value="http://www.mamicode.com/1" > 
            </div>


本文出自 “紫枫5966” 博客,请务必保留此出处http://zifeng5966.blog.51cto.com/8763113/1947635

angularJS-Ztree