Learn MVC Using Angular UI Select

Introduction

This article demonstrates how to create MVC applications using Angular UI-Select with bootstrap. This article is useful for implementing auto complete text in your application.

Angular UI Select

Angular UI-Select is used for customizing the select box or text, with the support for searching the user’s character or words in the bundle of data.

Features

  • Search, single select / multi select, tagging
  • Multiple themes: bootstrap, select 2
  • It will work with keyboard search
  • No jQuery required

Follow the steps to use Angular UI-Select in MVC

Create MVC Project

Open Visual Studio 2017.

MVC

Go to New menu and click New >> project. Now, it will open a "New Project" window.

MVC

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

MVC

One more window should appear. Select MVC Template in this popup & click OK.

Configure Angular UI Select

You can download the plug in from Angular UI Select or install it using NPM. 

$ npm install ui-select

Open the _Layout.cshtml and refer the .js file from downloaded folder to this View page.

  1. <script src="~/Plugin/angular/angular.min.js"></script>  
  2.         <script src="~/Plugin/angular-ui-router/release/angular-ui-router.min.js"></script>  
  3.         <script src="~/Plugin/angular-ui-select/dist/select.js"></script>  
  4.        <link href="~/Plugin/angular-ui-select/dist/select.css" rel="stylesheet" />  

Link your Angular configurable file here.

  1. <script src="~/App/App.module.js"></script>  
  2.     <script src="~/App/App.config.js"></script>  
  3.        <script src="~/App/uiSelectController.js"></script>  

Angular Module

Now, you need to include the module as a dependency on your application.

  1. var uiroute = angular  
  2.     .module('uiroute', ['ui.router','ui.select']);  

If you have any doubt in configuration, visit the following of my articles,

Working with Angular UI Select

With Angular UI-Select, you can use some different methods.

  • Array of single select
  • Array of string
  • Array of objects
  • Array of object with single property binding
  • Array of object with group by

Array of single select

It will allow you for single values and loaded as name, email & age. Here I will use bootstrap themes.

Html Code

  1. <ui-select ng-model="person.selected" theme="bootstrap" >  
  2.                         <ui-select-match placeholder="Select or search a name/age...">{{$select.selected.name}}</ui-select-match>  
  3.                         <ui-select-choices repeat="person in people | propsFilter: {name: $select.search, age: $select.search}">  
  4.                             <div ng-bind-html="person.name | highlight: $select.search"></div>  
  5.                             <small>  
  6.                                 email: {{person.email}} age:  
  7.                                 <span ng-bind-html="''+person.age | highlight: $select.search"></span>  
  8.                             </small>  
  9.                         </ui-select-choices>  
  10.                     </ui-select>  

Angular Controller

  1. $scope.person = {};  
  2.         $scope.people = [  
  3.             { name: 'Samantha', email: '[email protected]', age: 31 },  
  4.             { name: 'Estefanía', email: 'estefaní[email protected]', age: 16 },  
  5.             { name: 'Natasha', email: '[email protected]', age: 54 },  
  6.             { name: 'Nicole', email: '[email protected]', age: 43 },  
  7.             { name: 'Adrian', email: '[email protected]', age: 21 }  
  8.         ];  

Array of string

You can select multiple results in this list & binding as color list. Model will getting array as string.

Html Code

  1. <ui-select multiple="" ng-model="multipleDemo.colors" theme="bootstrap" >  
  2.                         <ui-select-match placeholder="Select colors...">{{$item}}</ui-select-match>  
  3.                         <ui-select-choices repeat="color in availableColors | filter:$select.search">{{color}}</ui-select-choices>  
  4.                     </ui-select>  
  5. <div class="text-sm text-muted mt-sm">Selected: {{multipleDemo.colors}}</div>  

Angular Controller

  1. $scope.availableColors = ['Red''Green''Blue''Yellow''Magenta''Maroon''Umbra''Turquoise'];  
  2.        $scope.multipleDemo = {};  
  3.        $scope.multipleDemo.colors = ['Blue''Red'];  

Array of objects.

Html Code

  1. <ui-select multiple="" ng-model="multipleDemo.selectedPeople" theme="bootstrap" >  
  2.                         <ui-select-match placeholder="Select person...">{{$item.name}} <{{$item.email}}></ui-select-match>  
  3.                         <ui-select-choices repeat="person in people | propsFilter: {name: $select.search, age: $select.search}">  
  4.                             <div ng-bind-html="person.name | highlight: $select.search"></div>  
  5.                             <small>  
  6.                                 email: {{person.email}} age:  
  7.                                 <span ng-bind-html="''+person.age | highlight: $select.search"></span>  
  8.                             </small>  
  9.                         </ui-select-choices>  
  10.                     </ui-select>  
  11. <div class="text-sm text-muted mt-sm">Selected: {{multipleDemo.selectedPeople}}</div>  

Angular Controller

  1. $scope.people = [  
  2.             { name: 'Samantha', email: '[email protected]', age: 31 },  
  3.             { name: 'Estefanía', email: 'estefaní[email protected]', age: 16 },  
  4.             { name: 'Natasha', email: '[email protected]', age: 54 },  
  5.             { name: 'Nicole', email: '[email protected]', age: 43 },  
  6.             { name: 'Adrian', email: '[email protected]', age: 21 }  
  7.         ];  
  8.    
  9. $scope.multipleDemo = {};  
  10.         $scope.multipleDemo.selectedPeople = [$scope.people[5], $scope.people[4]];  

Array of object with single property binding

Html Code

  1.   <ui-select multiple="" ng-model="multipleDemo.selectedPeopleSimple" theme="bootstrap" >  
  2.                         <ui-select-match placeholder="Select person...">{{$item.name}} <{{$item.email}}></ui-select-match>  
  3.                         <ui-select-choices repeat="person.email as person in people | propsFilter: {name: $select.search, age: $select.search}">  
  4.                             <div ng-bind-html="person.name | highlight: $select.search"></div>  
  5.                             <small>  
  6.                                 email: {{person.email}} age:  
  7.                                 <span ng-bind-html="''+person.age | highlight: $select.search"></span>  
  8.                             </small>  
  9.                         </ui-select-choices>  
  10.                     </ui-select>  
  11. <div class="text-sm text-muted mt-sm">Selected: {{multipleDemo.selectedPeopleSimple}}</div>  

. Angular Controller

  1. $scope.people = [  
  2.             { name: 'Samantha', email: '[email protected]', age: 31 },  
  3.             { name: 'Estefanía', email: 'estefaní[email protected]', age: 16 },  
  4.             { name: 'Natasha', email: '[email protected]', age: 54 },  
  5.             { name: 'Nicole', email: '[email protected]', age: 43 },  
  6.             { name: 'Adrian', email: '[email protected]', age: 21 }  
  7.           
  8.   $scope.multipleDemo.selectedPeopleSimple = ['[email protected]''[email protected]'];  

Array of object with group by

In there you must use group-by="someGroupFn" directives

Html Code

  1.    <ui-select multiple="" ng-model="multipleDemo.selectedPeopleWithGroupBy" theme="bootstrap" >  
  2.                         <ui-select-match placeholder="Select person...">{{$item.name}} <{{$item.email}}></ui-select-match>  
  3.                         <ui-select-choices group-by="someGroupFn" repeat="person in people | propsFilter: {name: $select.search, age: $select.search}">  
  4.                             <div ng-bind-html="person.name | highlight: $select.search"></div>  
  5.                             <small>  
  6.                                 email: {{person.email}} age:  
  7.                                 <span ng-bind-html="''+person.age | highlight: $select.search"></span>  
  8.                             </small>  
  9.                         </ui-select-choices>  
  10.                     </ui-select>  
  11. <div class="text-sm text-muted mt-sm">Selected: {{multipleDemo.selectedPeopleWithGroupBy}}</div>  

.Angular Controller

  1. $scope.people = [  
  2.             { name: 'Samantha', email: '[email protected]', age: 31 },  
  3.             { name: 'Estefanía', email: 'estefaní[email protected]', age: 16 },  
  4.             { name: 'Natasha', email: '[email protected]', age: 54 },  
  5.             { name: 'Nicole', email: '[email protected]', age: 43 },  
  6.             { name: 'Adrian', email: '[email protected]', age: 21 }  
  7.         ];  
  8. $scope.someGroupFn = function (item) {  
  9.   
  10.             if (item.name[0] >= 'A' && item.name[0] <= 'M')  
  11.                 return 'From A - M';  
  12.   
  13.             if (item.name[0] >= 'N' && item.name[0] <= 'Z')  
  14.                 return 'From N - Z';  
  15.   
  16.         };  
  17. $scope.multipleDemo.selectedPeopleWithGroupBy = [$scope.people[8], $scope.people[6]];  
  18. $scope.disabled = undefined;  
  19.   
  20.         $scope.enable = function () {  
  21.             $scope.disabled = false;  
  22.         };  
  23.   
  24.         $scope.disable = function () {  
  25.              
  26.             $scope.disabled = true;  
  27.         };  
  28.   
  29.         $scope.clear = function () {  
  30.             $scope.person.selected = undefined;  
  31.             $scope.address.selected = undefined;  
  32.             $scope.country.selected = undefined;  
  33.         };  

Click play button on VS 2017,Run the Application. Now it will appear in the  browser & you can see the result

Output 1

MVC

Output 2

MVC

Conclusion

In this article, we have learned about MVC using angular UI select. If you have any queries, please tell me through the comments section, because your comments are very valuable.

Happy Coding!..


Similar Articles