angular-ui-tree使用

今天为大家介绍下angular-ui-tree的使用方法,它是一个不依赖于jQuery 的angualrJS UI,友好的外观,方便的操作,是它最大的优势。

angular-ui-tree有如下特点:

1.使用本地AngularJS范围数据绑定

2.整个树都可以进行排序和移动项目

3.防止上级节点创建子节点

废话不多说,下面上效果图

拖拽效果:

节点的添加

官方DEMO:http://angular-ui-tree.github.io/angular-ui-tree/

angular-ui-tree 下载地址: https://github.com/angular-ui-tree/angular-ui-tree


前端使用代码如下:

index.js 

1、  载入angular-ui-tree.css 和 angular-ui-tree.js



2、module中加入依赖


使用页面中的代码

html

[html]  view plain  copy
  1. <script type="text/ng-template" id="angularTreeTemplate">  
  2.     <div ui-tree-handle class="tree-node tree-node-content angular-ui-tree-handle">  
  3.                 <a class="btn btn-success btn-xs" ng-if="item.children && item.children.length > 0" data-nodrag ng-click="toggle(this)">  
  4.                  <span class="glyphicon" ng-class="{'glyphicon-chevron-right': collapsed, 'glyphicon-chevron-down': !collapsed }">span>  
  5.                 a>  
  6.                  <span ng-show="item.isEdit">  
  7.                     <input style="width:40%" type="text" data-nodrag ng-model="item.name" />  
  8.                  span>  
  9.                  <span ng-show="!item.isEdit">  
  10.                      {{ item.name | limitTo: 5 }}{{item.name.length > 5 ? '...' : ''}}  
  11.                  span>  
  12.                <a ng-hide="item.children.length>0||item.level==0" class="pull-right btn btn-danger btn-xs" data-nodrag ng-click="removeItem(item)||remove(this)">  
  13.                 <span class="glyphicon glyphicon-remove">span>  
  14.                a>   
  15.                <a ng-show="item.level class="pull-right btn btn-primary btn-xs" data-nodrag ng-click="newSubItem(item)">  
  16.                 <span class="glyphicon glyphicon-plus">span>  
  17.                a>  
  18.                <a ng-show="item.level>0" class="pull-right btn btn-warning btn-xs" data-nodrag ng-click="editItem(item)">  
  19.                         <span class="glyphicon glyphicon-edit">span>  
  20.                a>  
  21.                <a ng-show="dept==1||item.level>1" class="pull-right btn btn-info btn-xs" data-nodrag ng-click="selectTreeItem(item)">  
  22.                             <span class="glyphicon glyphicon-search">span>  
  23.                a>  
  24.     div>  
  25.               <ol ui-tree-nodes="" style="padding-left:10px;" ng-model="item.children" ng-class="{hidden: collapsed}">  
  26.                 <li ng-repeat="item in item.children" ui-tree-node ng-include="'angularTreeTemplate'">  
  27.                       
  28.                 li>  
  29.               ol>     
  30. script>  
  31.   
  32. <div ui-tree="treeOptions" id="tree-root">  
  33.           <ol ui-tree-nodes ng-model="angularTreeList">  
  34.             <li ng-repeat="item in angularTreeList" ui-tree-node ng-include="readonly?'angularTreeReadOnlyTemplate':'angularTreeTemplate'">  
  35.               
  36.             li>  
  37.           ol>  
  38.         div>   
上段代码中  dept  参数决定了树的深度,决定了树到第几级不在继续创建子节点,我们可以自行配 。

在标签上加入data-nodrag表示此功能不受拖动的影响 ,保证了次功能能够正确使用,不被拖动效果所覆盖,如input的聚焦效果 。

ui-tree-handle为节点拖动的指令,自由的使用或删除它我们可以限制某些特定节点禁止拖动

前端用ng-include 这种形式 模拟后端生成树

js

[html]  view plain  copy
  1.     $scope.init=function(){  
  2.             $scope.getTreeList();  
  3.         }  
  4.       
  5.         $scope.getTreeList=function(){   
  6.             OrganizationService.getOrgTreeList().then(function(result) {  
  7.                 $scope.angularTreeList=[];  
  8.                 //查询树  
  9.                 $scope.orgTypeList=result;    
  10.                 //创建根节点  
  11.                 var root={};  
  12.                 root.name="法制机构";  
  13.                 root.level=0;  
  14.                 root.sequence=1;  
  15.                 root.orgTypeId=-1;  
  16.                 root.children=$scope.orgTypeList;  
  17.                 $scope.angularTreeList.push(root);  
  18.                 $scope.treeOptions.data=$scope.angularTreeList;  
  19.             });  
  20.         }  
  21.           
  22.         //新建节点  
  23.         $scope.newSubItem = function (item) {  
  24.             var name=window.prompt("请输入节点的名称");  
  25.             if(name==""||name==undefined){  
  26.                 return;  
  27.             }  
  28.             if(item.orgTypeId==undefined){  
  29.                 item.orgTypeId=-1;  
  30.             }  
  31.             var json={"level":parseInt(item.level)+1,"name":name,"parentTypeId":item.orgTypeId,"children":[]};  
  32.             if(item.children==null||item.children.length==0){  
  33.                 item.children=[];  
  34.                 json.sequence=1;  
  35.             }else{  
  36.                 var maxSequence=parseInt(item.children[item.children.length-1].sequence);  
  37.                 json.sequence=maxSequence+1;  
  38.             }  
  39.             OrganizationService.saveOrgType(json).then(function(result) {  
  40. //                item.children.push(json);  
  41.                 $scope.init();  
  42.             });             
  43.         };  
  44.           
  45.         //编辑节点  
  46.         $scope.editItem=function(item){  
  47. //            var target=$("#div_"+item.orgTypeId);  
  48.             if(item.isEdit==undefined||item.isEdit==false){  
  49.                 item.isEdit=true;    
  50. //                $(target).removeAttr("ui-tree-handle");       
  51.             }else if(item.isEdit==true){  
  52.                 item.isEdit=false;    
  53. //                $(target).attr("ui-tree-handle",'');  
  54.                 var json={"orgTypeId":item.orgTypeId,"name":item.name}  
  55.                 OrganizationService.updateOrgType(json).then(function(result) {  
  56. //                  $scope.init();  
  57.                 });  
  58.             }  
  59.         }  
  60.           
  61.         //删除节点  
  62.         $scope.removeItem=function(item){  
  63.           var json={};  
  64.           json.orgTypeId=item.orgTypeId;  
  65.           json.status=0;  
  66.           OrganizationService.updateOrgType(json).then(function(result) {  
  67. //            $scope.init();  
  68.           });  
  69.         }  
  70.           
  71.         $scope.treeOptions = {  
  72.             //拖拽操作 拖拽后会返回callback beforeDrop函数,我们可以重写这个函数,拖拽本质上是顺序的改变和层级的改变,所以是一个update操作  
  73.             beforeDrop : function (e) {  
  74.                var source = e.source.nodeScope.item;  
  75.                var destRoot = e.dest.nodesScope.item ;  
  76.                if(destRoot==undefined){  
  77.                    return $q.reject();  
  78.                }  
  79.                var destIndex=e.dest.index;  
  80.                var dest=e.dest.nodesScope.item.children[destIndex];  
  81.                if(dest==undefined){  
  82.                    return $q.reject();  
  83.                }  
  84.                if (source.parentTypeId!=dest.parentTypeId) {         
  85.                    return $q.reject();  
  86.                }  
  87.                var sourceSeq=source.sequence;  
  88.                var destSeq=dest.sequence;  
  89.                source.sequence=destSeq;  
  90.                dest.sequence=sourceSeq;  
  91.                OrganizationService.updateOrgType(source).then(function(result) {  
  92.   
  93.                });  
  94.                OrganizationService.updateOrgType(dest).then(function(result) {  
  95.           
  96.                });  
  97.                return;  
  98.             }  
  99.        };  

注:treeOptions中还有removed,dropped等回调方法,这里没有讲述,具体请看angular-ui-tree.js 源码

       本例中的拖拽操作默认为只能同级节点之间拖拽才能生效,不是同级自动取消,当然,不是同级完全自由的也可以实现,在使用的过程中,请根据自身业务需求及数据结构自行规定拖动的规则,angular-ui-tree默认的规则是全部可以自由拖动的

最后是后端实体的结构

[java]  view plain  copy
  1. public class OrganizationType extends BaseEntity {  
  2.     //组织类型id  
  3.     private String orgTypeId;  
  4.     //code  
  5.     private String code;  
  6.     //排序  
  7.     private String sequence;  
  8.     //层级  
  9.     private String level;  
  10.     //类型名称  
  11.     private String name;  
  12.     //父类型id  
  13.     private String parentTypeId;  
  14.       
  15.     private List children;  
  16.       
  17.       
  18.     public List getChildren() {  
  19.         return children;  
  20.     }  
  21.     public void setChildren(List children) {  
  22.         this.children = children;  
  23.     }  
  24.     public String getOrgTypeId() {  
  25.         return orgTypeId;  
  26.     }  
  27.     public void setOrgTypeId(String orgTypeId) {  
  28.         this.orgTypeId = orgTypeId;  
  29.     }  
  30.     public String getCode() {  
  31.         return code;  
  32.     }  
  33.     public void setCode(String code) {  
  34.         this.code = code;  
  35.     }  
  36.     public String getName() {  
  37.         return name;  
  38.     }  
  39.     public void setName(String name) {  
  40.         this.name = name;  
  41.     }  
  42.       
  43.     public String getParentTypeId() {  
  44.         return parentTypeId;  
  45.     }  
  46.     public void setParentTypeId(String parentTypeId) {  
  47.         this.parentTypeId = parentTypeId;  
  48.     }  
  49.   
  50.     public String getSequence() {  
  51.         return sequence;  
  52.     }  
  53.     public void setSequence(String sequence) {  
  54.         this.sequence = sequence;  
  55.     }  
  56.     public String getLevel() {  
  57.         return level;  
  58.     }  
  59.     public void setLevel(String level) {  
  60.         this.level = level;  
  61.     }  
  62.       
  63.     public String toString() {  
  64.         return "OrganizationType [orgTypeId=" + orgTypeId + ", code=" + code + ", sequence=" + sequence + ", level="  
  65.                + level + ", name=" + name + ", parentTypeId=" + parentTypeId + ", children=" + children + "]";  
  66.     }  
  67.          
  68. }  

查询树

[java]  view plain  copy
  1. @RequestMapping("/getOrgTreeList.do")  
  2.     @ResponseBody  
  3.     public List getOrgTreeList(){  
  4.         List orgTypeList= new ArrayList();  
  5.         OrganizationType orgType=new OrganizationType();  
  6.         orgType.setParentTypeId("-1");   
  7.         orgTypeList=orgTypeService.findOrgTypeList(orgType);  //把根查出来  
  8.         fillTree(orgTypeList);  
  9.         return orgTypeList;  
  10.     }  
  11.       
  12.     public void fillTree(List orgTypeList){  
  13.         if(orgTypeList!=null){  
  14.             for(OrganizationType orgType : orgTypeList){  
  15.                 OrganizationType type=new OrganizationType();  
  16.                 type.setParentTypeId(orgType.getOrgTypeId());  
  17.                 List orgChildList=orgTypeService.findOrgTypeList(type);  
  18.                 orgType.setChildren(orgChildList);  
  19.                 fillTree(orgChildList);  
  20.             }  
  21.         }  
  22.     }  
原文地址:http://blog.csdn.net/pkpk20044/article/details/51982232

你可能感兴趣的:(angular-ui-tree使用)