时间:2021-05-28
整理文档,搜刮出一个Angular directive递归实现目录树结构代码实例代码,稍微整理精简一下做下分享。
效果图:
重点:
1. 整棵目录树的实现,通过嵌套完成,主要在于对treeItem.html的递归使用
<script type="text/ng-template" id="treeView.html"> <ul> <li ng-repeat="item in treeData.children" ng-include="'treeItem.html'"></li> </ul> </script> <script type="text/ng-template" id="treeItem.html"> <span class="color-indictor" ng-class="{'leaf-node': isLeaf(item), 'expand-node': !isLeaf(item) && item.isExpand, 'unexpand-node': !isLeaf(item) && !item.isExpand}" ng-click="toggleExpandStatus(item)"></span> <span>{{item.name}}</span> <ul ng-if="!isLeaf(item)" ng-show="item.isExpand"> <li ng-repeat="item in item.children" ng-include="'treeItem.html'"></li> </ul> </script>2. 点击展开/关闭目录树
通过ng-show对item.expand进行判断,点击item时切换其expand参数,完成目录树的打开与关闭
3. 源码
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.9/angular.js"></script><script>angular.module("treeApp", []).controller("treeController", function($scope){ $scope.jsonData = { name: 'menu', children: [{ name: 'A', children: [{ name: 'A.1', children: [{ name: 'A.1.1', children: [] }] },{ name: 'A.2', children: [{ name: 'A.2.1', children: [{ name: 'A.2.1.1', children: [] }] },{ name: 'A.2.2', children: [] }] }] },{ name: 'B', children: [{ name: 'B.1', children: [] },{ name: 'B.2', children: [] }] },{ name: 'C', children: [] }] };}).directive('treeView', function(){ return { restrict: 'E', templateUrl: 'treeView.html', scope: { treeData: '=' }, controller: function($scope){ $scope.isLeaf = function(item){ return !item.children || !item.children.length; }; $scope.toggleExpandStatus = function(item){ item.isExpand = !item.isExpand; }; } };});</script><style>ul{ list-style: none;}.color-indictor{ display: inline-block; width: 20px; height: 20px; cursor: pointer;}.color-indictor.leaf-node{ background: red;}.color-indictor.unexpand-node{ background: green;}.color-indictor.expand-node{ background-color: yellow;}</style><body ng-app="treeApp" ng-controller="treeController"> <div> <p>Introduce: Click green block expand the menu tree</p> <p>Red: Leaf node, can not click</p> <p>Green: Unexpand node, click to expand menu</p> <p>Yellow: Expanded node, click to unexpand menu</p> </div> <tree-view tree-data="jsonData"></tree-view></body><script type="text/ng-template" id="treeView.html"> <ul> <li ng-repeat="item in treeData.children" ng-include="'treeItem.html'"></li> </ul></script><script type="text/ng-template" id="treeItem.html"> <span class="color-indictor" ng-class="{'leaf-node': isLeaf(item), 'expand-node': !isLeaf(item) && item.isExpand, 'unexpand-node': !isLeaf(item) && !item.isExpand}" ng-click="toggleExpandStatus(item)"></span> <span>{{item.name}}</span> <ul ng-if="!isLeaf(item)" ng-show="item.isExpand"> <li ng-repeat="item in item.children" ng-include="'treeItem.html'"></li> </ul></script>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了sqlserver实现递归查询的方法示例。分享给大家供大家参考,具体如下:有时候面对树结构的数据时需要进行递归查询,网上找了一番,参考了各位大神的
用shell写的递归遍历目录的脚本,脚本实现递归遍历指定目录,打印目录下的文件名。实例1:复制代码代码如下:#!/bin/shfunctionscandir()
本文实例为大家分享了Java实现并查集的具体代码,供大家参考,具体内容如下自下而上的树结构接口/***@authorNino*/publicinterfaceU
本文实例讲述了php删除指定目录的方法。分享给大家供大家参考。具体分析如下:这段代码可实现递归删除子目录的功能read()){//Skippointersif(
数据结构二叉树的递归与非递归实例代码:#include#include#include#includeusingnamespacestd;templatestr