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:
- <div class="btn-group" dropdown is-open="status.isopen" style="width:{{DropDownParam.Width}};">
- <div style="float:left;width:100%;">
- <input type="text" class="form-control" style="width:90%;" ng-disabled="DropDownParam.Disabled"
- id="{{TextControlID}}" placeholder="{{DropDownParam.placeholder}}" value="{{SelectText}}"
- ng-model="SelectText" ng-change="toggleDropdown();" ng-model-options="{debounce:1000}">
- <button type="button" class="btn btn-primary dropdown-toggle" dropdown-toggle ng-disabled="DropDownParam.Disabled"
- id="{{ButtonControlID}}" ng-click="fnShowDropDown();">
- <span class="caret"></span>
- </button>
- </div>
- <ul class="dropdown-menu" role="menu" style="height:{{DropDownParam.height}};overflow:auto;overflow-x:hidden;width:95%;">
- <li ng-repeat="item in DropDownParam.source | filter: {Text : SelectText}" data-ng-click="selectItem(item)">
- <a><span tabindex="-1" style="cursor:pointer;" data-ng-click="selectItem(item)">{{item.Text}}</span></a>
- </li>
- </ul>
- </div>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Drop Down Demo</title>
- <script src="../RefScript/angular.min.js"></script>
- <script src="../RefScript/ui-bootstrap.min.js"></script>
- <script src="../PageScript/CustomCtrlApp.js"></script>
- <script src="../DirectiveScript/DropDown.js"></script>
- <script src="../PageScript/DropDownDemo.js"></script>
- <link href="../RefStyle/bootstrap.min.css" rel="stylesheet" />
- </head>
- <body ng-app="CustomCtrlApp">
- <div ng-controller="DropDownDemo">
- <h2>{{Heading1}}</h2>
- <div>
- <table style="width:100%;column-span:all;" cellpadding="5" cellspacing="10" >
- <tr>
- <td style="width:40%;">Basic Drop Down </td>
- <td style="width:60%;"><ng-drop-down dropdown-setting="ComboSetting" callbackfunction="fnChange"></ng-drop-down></td>
- </tr>
- <tr>
- <td colspan="2">
- <span>Select Item : {{SelectText}}</span>
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <input type="button" value="Get Text" ng-click="fnGetText();" />
- <input type="button" value="Get Text" ng-click="fnGetValue();" />
- <input type="button" value="Clear Selection" ng-click="fnClearItem();" />
- <input type="button" value="Clear Combo" ng-click="fnClear();" />
- <input type="button" value="Enable Combo" ng-click="fnEnable(true);" />
- <input type="button" value="Disable Combo" ng-click="fnEnable(false);" />
- </td>
- </tr>
- <tr>
- <td><input type="text" ng-model="SetValue" /></td>
- <td>
- <input type="button" value="Set Item by Value" ng-click="fnSetItem();" />
- <input type="button" value="Set Item by Index" ng-click="fnSetIndex();" />
- </td>
- </tr>
- </table>
- </div>
- </div>
- </body>
- </html>
First, we need to create an Angular App file named "CustomCtrlApp.Js" and add the following code:
- var CustomCtrlApp = angular.module('CustomCtrlApp', ['ui.bootstrap']);
- CustomCtrlApp.directive("ngDropDown", [function () {
- this;
- return {
- restrict: "EA",
- scope: {
- DropDownSetup: '=dropdownSetting',
- callbackfunction: '='
- },
- templateUrl: '../HTMLTemplate/DropDown.html',
- controller: function ($scope, $element, $attrs) {
- $scope.DropDownParam = $scope.DropDownSetup.attribute;
- $scope.TextControlID = "txt" + $scope.DropDownSetup.attribute.id;
- $scope.ButtonControlID = "btn" + $scope.DropDownSetup.attribute.id;
- $scope.showDropDown = false;
- $scope.SelectText = '';
- if ($scope.DropDownParam.Width != #ff0000) {
- var width = $scope.DropDownParam.Width.substr(0, $scope.DropDownParam.Width.length - 1);
- $scope.BoxWidth = Math.round(width * 0.95, 0) + '%';
- }
- else {
- $scope.BoxWidth = '95%';
- }
- if ($scope.DropDownParam.Enabled != undefined) {
- $scope.DropDownParam.Disabled = !$scope.DropDownParam.Enabled;
- }
- else {
- $scope.DropDownParam.Disabled = false;
- }
- if ($scope.DropDownSetup.events == undefined) {
- $scop.DropDownSetup.events = {};
- }
- $scope.fnShowDropDown = function () {
- if ($scope.DropDownParam.source.length > 0) {
- $scope.showDropDown = !$scope.showDropDown;
- }
- else {
- $scope.showDropDown = false;
- }
- }
- $scope.selectItem = function (item) {
- $scope.SelectText = item.Text;
- $scope.DropDownSetup.attribute.Text = item.Text;
- $scope.DropDownSetup.attribute.Value = item.Value;
- $scope.showDropDown = false;
- $scope.DropDownSetup.events.selectedIndexChange(item);
- }
- function assignDropDownMethod() {
- $scope.DropDownSetup.method = {
- clearSelection: function () {
- $scope.SelectText = '';
- },
- clear: function () {
- $scope.DropDownParam.source = '';
- $scope.SelectText = '';
- },
- selectItemByValue: function (value) {
- var result_obj = objectFindByKey($scope.DropDownParam.source, 'Value', value);
- if (result_obj != null) {
- $scope.selectItem(result_obj);
- }
- },
- selectItemByIndex: function (index) {
- var result_obj = $scope.DropDownParam.source[index];
- if (result_obj != null) {
- $scope.selectItem(result_obj);
- }
- },
- setEnable: function (param) {
- $scope.DropDownParam.Disabled = !param;
- }
- }
- }
- assignDropDownMethod();
- function objectFindByKey(array, key, value) {
- for (var i = 0; i < array.length; i++) {
- if (array[i][key] === value) {
- return array[i];
- }
- }
- return null;
- }
- $scope.fnHideDropDown = function () {
- $scope.showDropDown = false;
- }
- $scope.status = {
- isopen: false
- };
- $scope.toggleDropdown = function () {
- if ($scope.DropDownParam.source.length > 0) {
- $scope.showDropDown = !$scope.showDropDown;
- $scope.status.isopen = !$scope.status.isopen;
- }
- else {
- $scope.showDropDown = false;
- $scope.status.isopen = false;
- }
- };
- }
- }
- }]);
- CustomCtrlApp.controller("DropDownDemo", ['$scope', '$http',
- function ($scope, $http) {
- $scope.Heading1 = "Static Drop Down Control";
- $scope.ComboSetting = {};
- $scope.SetValue = "";
- var comboSource1 = [{ Text: "Item1", Value: "1" },
- { Text: "System", Value: "2" },
- { Text: "Sweet", Value: "3" },
- { Text: "Titan", Value: "4" },
- { Text: "Bimal", Value: "5" },
- { Text: "Swapan", Value: "6" },
- { Text: "Item4", Value: "7" }];
- $scope.ComboSetting.attribute = {
- Width: '50%',
- Enabled: true,
- SearchMode: true,
- id: 'cmb1',
- placeholder: 'Custom Combo',
- source: comboSource1,
- height: '200px'
- };
- $scope.fnChange = function (args) {
- $scope.SelectText = args.Text;
- }
- if ($scope.ComboSetting.events == undefined) {
- $scope.ComboSetting.events = {};
- }
- $scope.ComboSetting.events.selectedIndexChange = function (args) {
- $scope.SelectText = args.Text;
- }
- $scope.fnGetText = function () {
- alert($scope.ComboSetting.attribute.Text);
- }
- $scope.fnGetValue = function () {
- alert($scope.ComboSetting.attribute.Value);
- }
- $scope.fnClearItem = function () {
- $scope.ComboSetting.method.clearSelection();
- }
- $scope.fnClear = function () {
- $scope.ComboSetting.method.clear();
- }
- $scope.fnSetItem = function () {
- $scope.ComboSetting.method.selectItemByValue($scope.SetValue);
- }
- $scope.fnSetIndex = function () {
- $scope.ComboSetting.method.selectItemByIndex($scope.SetValue);
- }
- $scope.fnEnable = function (args) {
- $scope.ComboSetting.method.setEnable(args);
- }
- }]);


Sr KarthigaPosted Apr 19, 2016, 11:02 PM
Nice explanation
SharadPosted Jul 17, 2015, 3:52 AM
good one...
Jacob RobinsonPosted Jun 30, 2015, 5:13 AM
Great..
Debasis SahaPosted Jun 29, 2015, 1:00 AM
Thanks for the comment
Ankit BansalPosted Jun 28, 2015, 6:33 AM
nice article...
Rahul Kumar SaxenaPosted Jun 28, 2015, 5:15 AM
Good show
Gopi ChandPosted Jun 28, 2015, 1:56 AM
Great :)
Santhakumar MunuswamyPosted Jun 27, 2015, 10:55 AM
Thanks for nice article:)
Pankaj Kumar ChoudharyPosted Jun 27, 2015, 12:55 AM
Nice One Sir..........
Sibeesh VenuPosted Jun 27, 2015, 12:32 AM
Good one.
Debendra DashPosted Jun 26, 2015, 1:42 PM
nice.........
RakeshPosted Jun 26, 2015, 4:18 AM
Good one sir