Custom DropDown Control Using Bootstrap and AngularJS

When we want to develop a web-based application using AngularJs, it often requires a dropdown control with some extra features like search text and so on. But like classic ASP.NET, always a ready-made control is not available in the AngularJs Framework. But actually, if we want to create a custom dropdown control, we can create it easily. Using this article, we will learn how to create a custom dropdown control.
 
For that, we will first open Visual Studio and create a blank ASP.NET web application. We then need to install some Nuget packages such as AngularJs, Bootstrap and Angular UI.
 
 
Now create a HTML file named DropDown.html. It is the template file for the dropdown directive. In this file, add the following code:
  1. <div class="btn-group" dropdown is-open="status.isopen" style="width:{{DropDownParam.Width}};">  
  2.     <div style="float:left;width:100%;">  
  3.         <input type="text" class="form-control" style="width:90%;" ng-disabled="DropDownParam.Disabled"  
  4.                id="{{TextControlID}}" placeholder="{{DropDownParam.placeholder}}" value="{{SelectText}}"   
  5.                ng-model="SelectText" ng-change="toggleDropdown();" ng-model-options="{debounce:1000}">  
  6.         <button type="button" class="btn btn-primary dropdown-toggle" dropdown-toggle ng-disabled="DropDownParam.Disabled"  
  7.                 id="{{ButtonControlID}}" ng-click="fnShowDropDown();">  
  8.             <span class="caret"></span>  
  9.         </button>  
  10.     </div>  
  11.     <ul class="dropdown-menu" role="menu" style="height:{{DropDownParam.height}};overflow:auto;overflow-x:hidden;width:95%;">  
  12.         <li ng-repeat="item in DropDownParam.source | filter: {Text : SelectText}" data-ng-click="selectItem(item)">  
  13.             <a><span tabindex="-1" style="cursor:pointer;" data-ng-click="selectItem(item)">{{item.Text}}</span></a>  
  14.         </li>  
  15.     </ul>  
  16. </div>  
Now add another file named "DemoDropDown.html" that shows the functionality of the dropdown control. In that file, we will use the reference of Angular.min.js file and bootstrap.js file. Also, we will use the reference of the bootstrap.css file. Provide the following code in this HTML file:
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title>Drop Down Demo</title>  
  5.     <script src="../RefScript/angular.min.js"></script>  
  6.     <script src="../RefScript/ui-bootstrap.min.js"></script>  
  7.     <script src="../PageScript/CustomCtrlApp.js"></script>  
  8.     <script src="../DirectiveScript/DropDown.js"></script>  
  9.     <script src="../PageScript/DropDownDemo.js"></script>  
  10.   
  11.     <link href="../RefStyle/bootstrap.min.css" rel="stylesheet" />  
  12. </head>  
  13. <body ng-app="CustomCtrlApp">  
  14.     <div ng-controller="DropDownDemo">  
  15.         <h2>{{Heading1}}</h2>  
  16.         <div>  
  17.             <table style="width:100%;column-span:all;" cellpadding="5" cellspacing="10" >  
  18.                 <tr>  
  19.                     <td style="width:40%;">Basic Drop Down </td>  
  20.                     <td style="width:60%;"><ng-drop-down dropdown-setting="ComboSetting" callbackfunction="fnChange"></ng-drop-down></td>  
  21.                 </tr>  
  22.                 <tr>  
  23.                     <td colspan="2">  
  24.                         <span>Select Item : {{SelectText}}</span>  
  25.                     </td>  
  26.                 </tr>  
  27.                 <tr>  
  28.                     <td colspan="2">  
  29.                         <input type="button" value="Get Text" ng-click="fnGetText();" />  
  30.                         <input type="button" value="Get Text" ng-click="fnGetValue();" />  
  31.                         <input type="button" value="Clear Selection" ng-click="fnClearItem();" />  
  32.                         <input type="button" value="Clear Combo" ng-click="fnClear();" />  
  33.                         <input type="button" value="Enable Combo" ng-click="fnEnable(true);" />  
  34.                         <input type="button" value="Disable Combo" ng-click="fnEnable(false);" />  
  35.                     </td>  
  36.                 </tr>  
  37.                 <tr>  
  38.                     <td><input type="text" ng-model="SetValue" /></td>  
  39.                     <td>  
  40.                         <input type="button" value="Set Item by Value" ng-click="fnSetItem();" />  
  41.                         <input type="button" value="Set Item by Index" ng-click="fnSetIndex();" />  
  42.                     </td>  
  43.                 </tr>  
  44.             </table>  
  45.         </div>         
  46.     </div>  
  47. </body>  
  48. </html>  
Now it's time to add our own JavaScript file.
 
First, we need to create an Angular App file named "CustomCtrlApp.Js" and add the following code:
  1. var CustomCtrlApp = angular.module('CustomCtrlApp', ['ui.bootstrap']);  
Now we will add another JavaScript file named "DropDown.js" that is the controller file of the dropdown directive to define the attribute, events and methods of the dropdown control.
  1. CustomCtrlApp.directive("ngDropDown", [function () {  
  2.     this;  
  3.     return {  
  4.         restrict: "EA",  
  5.         scope: {  
  6.             DropDownSetup: '=dropdownSetting',  
  7.             callbackfunction: '='  
  8.         },  
  9.         templateUrl: '../HTMLTemplate/DropDown.html',  
  10.         controller: function ($scope, $element, $attrs) {  
  11.             $scope.DropDownParam = $scope.DropDownSetup.attribute;  
  12.             $scope.TextControlID = "txt" + $scope.DropDownSetup.attribute.id;  
  13.             $scope.ButtonControlID = "btn" + $scope.DropDownSetup.attribute.id;  
  14.             $scope.showDropDown = false;  
  15.             $scope.SelectText = '';  
  16.   
  17.             if ($scope.DropDownParam.Width != #ff0000) {  
  18.                 var width = $scope.DropDownParam.Width.substr(0, $scope.DropDownParam.Width.length - 1);  
  19.                 $scope.BoxWidth = Math.round(width * 0.95, 0) + '%';  
  20.             }  
  21.             else {  
  22.                 $scope.BoxWidth = '95%';  
  23.             }  
  24.   
  25.             if ($scope.DropDownParam.Enabled != undefined) {  
  26.                 $scope.DropDownParam.Disabled = !$scope.DropDownParam.Enabled;  
  27.             }  
  28.             else {  
  29.                 $scope.DropDownParam.Disabled = false;  
  30.             }  
  31.   
  32.             if ($scope.DropDownSetup.events == undefined) {  
  33.                 $scop.DropDownSetup.events = {};  
  34.             }  
  35.   
  36.             $scope.fnShowDropDown = function () {  
  37.                 if ($scope.DropDownParam.source.length > 0) {  
  38.                     $scope.showDropDown = !$scope.showDropDown;  
  39.                 }  
  40.                 else {  
  41.                     $scope.showDropDown = false;  
  42.                 }  
  43.             }  
  44.   
  45.             $scope.selectItem = function (item) {  
  46.                 $scope.SelectText = item.Text;  
  47.                 $scope.DropDownSetup.attribute.Text = item.Text;  
  48.                 $scope.DropDownSetup.attribute.Value = item.Value;  
  49.                 $scope.showDropDown = false;                  
  50.                 $scope.DropDownSetup.events.selectedIndexChange(item);  
  51.             }  
  52.   
  53.             function assignDropDownMethod() {  
  54.                 $scope.DropDownSetup.method = {  
  55.                     clearSelection: function () {  
  56.                         $scope.SelectText = '';  
  57.                     },  
  58.                     clear: function () {  
  59.                         $scope.DropDownParam.source = '';  
  60.                         $scope.SelectText = '';  
  61.                     },  
  62.                     selectItemByValue: function (value) {  
  63.                         var result_obj = objectFindByKey($scope.DropDownParam.source, 'Value', value);  
  64.                         if (result_obj != null) {  
  65.                             $scope.selectItem(result_obj);  
  66.                         }  
  67.                     },  
  68.                     selectItemByIndex: function (index) {  
  69.                         var result_obj = $scope.DropDownParam.source[index];  
  70.                         if (result_obj != null) {  
  71.                             $scope.selectItem(result_obj);  
  72.                         }  
  73.                     },  
  74.                     setEnable: function (param) {  
  75.                         $scope.DropDownParam.Disabled = !param;  
  76.                     }  
  77.                 }  
  78.             }  
  79.   
  80.             assignDropDownMethod();  
  81.   
  82.             function objectFindByKey(array, key, value) {  
  83.                 for (var i = 0; i < array.length; i++) {  
  84.                     if (array[i][key] === value) {  
  85.                         return array[i];  
  86.                     }  
  87.                 }  
  88.                 return null;  
  89.             }  
  90.   
  91.             $scope.fnHideDropDown = function () {  
  92.                 $scope.showDropDown = false;  
  93.             }  
  94.   
  95.             $scope.status = {  
  96.                 isopen: false  
  97.             };  
  98.   
  99.             $scope.toggleDropdown = function () {  
  100.                 if ($scope.DropDownParam.source.length > 0) {  
  101.                     $scope.showDropDown = !$scope.showDropDown;  
  102.                     $scope.status.isopen = !$scope.status.isopen;  
  103.                 }  
  104.                 else {  
  105.                     $scope.showDropDown = false;  
  106.                     $scope.status.isopen = false;  
  107.                 }  
  108.   
  109.             };  
  110.         }  
  111.     }  
  112. }]);  
Now we need to add another JavaScript file named "DropDownDemo.js" file as the controller file of the demo HTML file. In that file, we will provide all the required attributes including the data source of the dropdown control and call the various methods of the dropdown control including enable or disable and so on. 
  1. CustomCtrlApp.controller("DropDownDemo", ['$scope''$http',  
  2.     function ($scope, $http) {  
  3.         $scope.Heading1 = "Static Drop Down Control";  
  4.         $scope.ComboSetting = {};  
  5.         $scope.SetValue = "";          
  6.         var comboSource1 = [{ Text: "Item1", Value: "1" },  
  7.                             { Text: "System", Value: "2" },  
  8.                             { Text: "Sweet", Value: "3" },  
  9.                             { Text: "Titan", Value: "4" },  
  10.                             { Text: "Bimal", Value: "5" },  
  11.                             { Text: "Swapan", Value: "6" },  
  12.                             { Text: "Item4", Value: "7" }];  
  13.   
  14.         $scope.ComboSetting.attribute = {  
  15.             Width: '50%',  
  16.             Enabled: true,  
  17.             SearchMode: true,  
  18.             id: 'cmb1',  
  19.             placeholder: 'Custom Combo',  
  20.             source: comboSource1,  
  21.             height: '200px'  
  22.         };  
  23.   
  24.         $scope.fnChange = function (args) {  
  25.             $scope.SelectText = args.Text;  
  26.         }  
  27.   
  28.         if ($scope.ComboSetting.events == undefined) {  
  29.             $scope.ComboSetting.events = {};  
  30.         }  
  31.   
  32.         $scope.ComboSetting.events.selectedIndexChange = function (args) {  
  33.             $scope.SelectText = args.Text;  
  34.         }  
  35.   
  36.         $scope.fnGetText = function () {  
  37.             alert($scope.ComboSetting.attribute.Text);  
  38.         }  
  39.   
  40.         $scope.fnGetValue = function () {  
  41.             alert($scope.ComboSetting.attribute.Value);  
  42.         }  
  43.   
  44.         $scope.fnClearItem = function () {  
  45.             $scope.ComboSetting.method.clearSelection();  
  46.         }  
  47.   
  48.         $scope.fnClear = function () {  
  49.             $scope.ComboSetting.method.clear();  
  50.         }  
  51.   
  52.         $scope.fnSetItem = function () {  
  53.             $scope.ComboSetting.method.selectItemByValue($scope.SetValue);  
  54.         }  
  55.   
  56.         $scope.fnSetIndex = function () {  
  57.             $scope.ComboSetting.method.selectItemByIndex($scope.SetValue);  
  58.         }  
  59.   
  60.         $scope.fnEnable = function (args) {  
  61.             $scope.ComboSetting.method.setEnable(args);  
  62.         }  
  63.     }]);  
Now our code is complete. Run the project and check the output here.