Introduction

This article demonstrates working with MVC using Angular bootstrap nav tree.

Angular Bootstrap Nav Tree

The style is completely bootstrap because the tree is actually just a “nav” list, with a few changes.

  • Added
  • Expend / Collapse
  • Angular animations are used during expand / collapse

The abn-tree directives now work with Bootstrap 2 and Bootstrap 3. You can change the icons used, by specifying them in HTML attributes.

Follow the steps below to use Angular Bootstrap nav tree in Angular JS in MVC

  • Create MVC Project
  • Configure Angular bootstrap nav tree
  • Work in Angular bootstrap nav tree

Create MVC Project

Open Visual Studio 2017.

Angular

Go to New > New > Project. Now, it will open New Project window.

Angular

Select ASP.NET Web Application on Framework 4.5. Enter the name of the project in “Solution name” textbox and click OK button.

Angular

One more window should appear. Select MVC Template in this popup and click OK. Now, we can start the play.

Configure Angular bootstrap nav tree

You can download the plug-in from,

Open the _Layout.cshtml and must refer the .js files from downloaded folder to view this page.

  1. “~/Plugin/angular/angular.min.js”>
  2. “~/Plugin/angular-bootstrap-nav-tree/dist/abn_tree_directive.js”>
  3. “~/Plugin/angular-ui-router/release/angular-ui-router.min.js”>
  4. <link href=“~/Plugin/angular-bootstrap-nav-tree/dist/abn_tree.css” rel=“stylesheet” />

Link your Angular configurable file.

  1. “~/App/App.module.js”>
  2. “~/App/App.config.js”>
  3. “~/App/NTController.js”>

Angular Module

You need to include the module as a dependency on your application.

  1. var uiroute = angular
  2. .module(‘uiroute’, [‘ui.router’,‘angularBootstrapNavTree’]);

Work in Angular bootstrap nav tree

This tree control will work when you use the <abn-tree > directive as HTML attribute.

  1. <abn-tree tree-data=“my_data”
  2.                           tree-control=“my_tree”
  3.                           on-select=“my_tree_handler(branch)”
  4.                           expand-level=“2”
  5.                           initial-selection=“Granny Smith”>
  6.                 </abn-tree>

Html Code

  1. “NTreeController”>
  2. class=“alert alert-info”>{{ output }}
  3. class=“row”>
  4. class=“col-lg-3 col-sm-6 col-lg-push-3 col-sm-push-6”>
  5. class=“well wd-wide”>
  6.                 “my_data”
  7.                           tree-control=“my_tree”
  8.                           on-select=“my_tree_handler(branch)”
  9.                           expand-level=“2”
  10.                           initial-selection=“Granny Smith”>

Angular Controller

  1. var treedata_geography = [
  2.             {
  3.                 label: ‘North America’,
  4.                 children: [
  5.                     {
  6.                         label: ‘Canada’,
  7.                         children: [‘Toronto’‘Vancouver’]
  8.                     }, {
  9.                         label: ‘USA’,
  10.                         children: [‘New York’‘Los Angeles’]
  11.                     }, {
  12.                         label: ‘Mexico’,
  13.                         children: [‘Mexico City’‘Guadalajara’]
  14.                     }
  15.                 ]
  16.             }, {
  17.                 label: ‘South America’,
  18.                 children: [
  19.                     {
  20.                         label: ‘Venezuela’,
  21.                         children: [‘Caracas’‘Maracaibo’]
  22.                     }, {
  23.                         label: ‘Brazil’,
  24.                         children: [‘Sao Paulo’‘Rio de Janeiro’]
  25.                     }, {
  26.                         label: ‘Argentina’,
  27.                         children: [‘Buenos Aires’‘Cordoba’]
  28.                     }
  29.                 ]
  30.             }
  31.         ];

Click F5 button to Run the application. Now, it will open the browser where you can see the result. It will load geographic data and on-select change, shows the selected result on top of the page.

Output 1

Angular

Angular Controller

  1. var treedata_avm = [
  2.           {
  3.               label: ‘Animal’,
  4.               children: [
  5.                   {
  6.                       label: ‘Dog’,
  7.                       data: {
  8.                           description: ‘man\’s best friend’
  9.                       }
  10.                   }, {
  11.                       label: ‘Cat’,
  12.                       data: {
  13.                           description: ‘Felis catus’
  14.                       }
  15.                   }, {
  16.                       label: ‘Hippopotamus’,
  17.                       data: {
  18.                           description: ‘hungry, hungry’
  19.                       }
  20.                   }, {
  21.                       label: ‘Chicken’,
  22.                       children: [‘White Leghorn’‘Rhode Island Red’‘Jersey Giant’]
  23.                   }
  24.               ]
  25.           }, {
  26.               label: ‘Vegetable’,
  27.               data: {
  28.                   definition: ‘A plant or part of a plant used as food, typically as accompaniment to meat or fish, such as a cabbage, potato, carrot, or bean.’,
  29.                   data_can_contain_anything: true
  30.               },
  31.               onSelect: function (branch) {
  32.                   $scope.output = ‘Vegetable: ‘ + branch.data.definition;
  33.                   return $scope.output;
  34.               },
  35.               children: [
  36.                   {
  37.                       label: ‘Oranges’
  38.                   }, {
  39.                       label: ‘Apples’,
  40.                       children: [
  41.                           {
  42.                               label: ‘Granny Smith’,
  43.                               onSelect: apple_selected
  44.                           }, {
  45.                               label: ‘Red Delicous’,
  46.                               onSelect: apple_selected
  47.                           }, {
  48.                               label: ‘Fuji’,
  49.                               onSelect: apple_selected
  50.                           }
  51.                       ]
  52.                   }
  53.               ]
  54.           }, {
  55.               label: ‘Mineral’,
  56.               children: [
  57.                   {
  58.                       label: ‘Rock’,
  59.                       children: [‘Igneous’‘Sedimentary’‘Metamorphic’]
  60.                   }, {
  61.                       label: ‘Metal’,
  62.                       children: [‘Aluminum’‘Steel’‘Copper’]
  63.                   }, {
  64.                       label: ‘Plastic’,
  65.                       children: [
  66.                           {
  67.                               label: ‘Thermoplastic’,
  68.                               children: [‘polyethylene’‘polypropylene’‘polystyrene’‘ polyvinyl chloride’]
  69.                           }, {
  70.                               label: ‘Thermosetting Polymer’,
  71.                               children: [‘polyester’‘polyurethane’‘vulcanized rubber’‘bakelite’‘urea-formaldehyde’]
  72.                           }
  73.                       ]
  74.                   }
  75.               ]
  76.           }
  77.       ];
  78. cope.try_changing_the_tree_data = function () {
  79.           if ($scope.my_data === treedata_avm) {
  80.               $scope.my_data = treedata_geography;
  81.           } else {
  82.               $scope.my_data = treedata_avm;
  83.           }
  84.           return $scope.my_data;
  85.       };

You can load the data dynamically in nev tree. When you click on toggle tree data, it shows the description of label.

Output 2

Angular
Angular

Angular Controller

  1. $scope.try_adding_a_branch = function () {
  2.             var b;
  3.             b = tree.get_selected_branch();
  4.             return tree.add_branch(b, {
  5.                 label: ‘New Branch’,
  6.                 data: {
  7.                     something: 42,
  8.                     ‘else’: 43
  9.                 }
  10.             });
  11.         };

You can add the new branch on that tree in any of the nodes.

Output 3

Angular

Note

Other events are already predefined.

Conclusion

In this article, we have learned creating MVC application using Angular Bootstrap nav tree. If you have any queries, please tell me via comments section. Your comments are very valuable.

Happy Coding!

Advertisements