Home > Web Front-end > JS Tutorial > body text

AngularJS recursive instruction to implement Tree View effect example

高洛峰
Release: 2016-12-07 15:18:24
Original
1288 people have browsed it

The example in this article describes how to use AngularJS recursive instructions to achieve the Tree View effect. Share it with everyone for your reference, the details are as follows:

In the display of hierarchical data structures, trees are an extremely common display method. For example, the directory structure, corporate organizational structure, and e-commerce product classification in the system are all common tree-structured data.

Here we use Angular to implement this type of common tree view structure.

First we define the data structure and use the children attribute to connect child nodes to display the tree hierarchy. The example is as follows:

[
  {
   "id":"1",
   "pid":"0",
   "name":"家用电器",
   "children":[
     {
      "id":"4",
      "pid":"1",
      "name":"大家电"
     }
   ]
  },
  {
   ...
  }
  ...
]
Copy after login

Then our tree view for ng way can be implemented as:

JavaScript:

angular.module('ng.demo', [])
.directive('treeView',[function(){
   return {
     restrict: 'E',
     templateUrl: '/treeView.html',
     scope: {
       treeData: '=',
       canChecked: '=',
       textField: '@',
       itemClicked: '&',
       itemCheckedChanged: '&',
       itemTemplateUrl: '@'
     },
     controller:['$scope', function($scope){
       $scope.itemExpended = function(item, $event){
         item.$$isExpend = ! item.$$isExpend;
         $event.stopPropagation();
       };
       $scope.getItemIcon = function(item){
         var isLeaf = $scope.isLeaf(item);
         if(isLeaf){
           return 'fa fa-leaf';
         }
         return item.$$isExpend ? 'fa fa-minus': 'fa fa-plus';
       };
       $scope.isLeaf = function(item){
        return !item.children || !item.children.length;
       };
       $scope.warpCallback = function(callback, item, $event){
         ($scope[callback] || angular.noop)({
           $item:item,
           $event:$event
         });
       };
     }]
   };
 }]);
Copy after login

HTML:

Tree content theme HTML: /treeView.html

Copy after login

HTML of each item node:/treeItem.html




Copy after login

The trick here is to use ng-include is used to load child nodes and data, and a warpCallback method is used to transfer the external callback function of the function. The empty object mode of angular.noop is used to avoid unregistered callback scenarios. Data isolation for View interaction is directly encapsulated in metadata objects, but they all start with $$, such as $$isChecked and $$isExpend. Objects starting with $$ in the Angular program will be considered internal private variables. They will not be serialized during angular.toJson, so when using $http to send back to the server for updates, they will not be serialized. Affects the data sent by the server. At the same time, on the client side, we can also use these $$ properties of the object to control the state of the View, such as item.$$isChecked to select a node by default.

We can use this tree-view as follows:

Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!