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 an 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 the project in “Solution name” textbox and click OK.

MVC

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

Configure Angular UI Select

You can download the plugin 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.

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

Link your Angular configurable file here.

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

Angular Module

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

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

Working With Angular UI Select

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

  • An array of single select.
  • An array of strings.
  • An array of objects.
  • An array of objects with single property binding.
  • An array of objects with ‘group by.’

Array of Single Select

It will allow you to enter single values, such as names, email addresses, and age. Here I will use Bootstrap themes.

HTML Code

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

Angular Controller

$scope.person = {};
        $scope.people = [
            { name: 'Samantha', email: 'samantha@email.com', age: 31 },
            { name: 'Estefanía', email: 'estefanía@email.com', age: 16 },
            { name: 'Natasha', email: 'natasha@email.com', age: 54 },
            { name: 'Nicole', email: 'nicole@email.com', age: 43 },
            { name: 'Adrian', email: 'adrian@email.com', age: 21 }
        ];

Array of Strings

You can select multiple results in this list and bind them as a color list. The model will take an array as a string.

HTML Code

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

Angular Controller

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

Array of Objects

HTML Code

<ui-select multiple="" ng-model="multipleDemo.selectedPeople" theme="bootstrap" >
                        <ui-select-match placeholder="Select person...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match>
                        <ui-select-choices repeat="person in people | propsFilter: {name: $select.search, age: $select.search}">
                            <div ng-bind-html="person.name | highlight: $select.search"></div>
                            <small>
                                email: {{person.email}} age:
                                <span ng-bind-html="''+person.age | highlight: $select.search"></span>
                            </small>
                        </ui-select-choices>
                    </ui-select>
<div class="text-sm text-muted mt-sm">Selected: {{multipleDemo.selectedPeople}}</div>

Angular Controller

$scope.people = [
            { name: 'Samantha', email: 'samantha@email.com', age: 31 },
            { name: 'Estefanía', email: 'estefanía@email.com', age: 16 },
            { name: 'Natasha', email: 'natasha@email.com', age: 54 },
            { name: 'Nicole', email: 'nicole@email.com', age: 43 },
            { name: 'Adrian', email: 'adrian@email.com', age: 21 }
        ];
 
$scope.multipleDemo = {};
        $scope.multipleDemo.selectedPeople = [$scope.people[5], $scope.people[4]];

Array of Object With Single Property Binding

HTML Code

<ui-select multiple="" ng-model="multipleDemo.selectedPeopleSimple" theme="bootstrap" >
                        <ui-select-match placeholder="Select person...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match>
                        <ui-select-choices repeat="person.email as person in people | propsFilter: {name: $select.search, age: $select.search}">
                            <div ng-bind-html="person.name | highlight: $select.search"></div>
                            <small>
                                email: {{person.email}} age:
                                <span ng-bind-html="''+person.age | highlight: $select.search"></span>
                            </small>
                        </ui-select-choices>
                    </ui-select>
<div class="text-sm text-muted mt-sm">Selected: {{multipleDemo.selectedPeopleSimple}}</div>

Angular Controller

$scope.people = [
            { name: 'Samantha', email: 'samantha@email.com', age: 31 },
            { name: 'Estefanía', email: 'estefanía@email.com', age: 16 },
            { name: 'Natasha', email: 'natasha@email.com', age: 54 },
            { name: 'Nicole', email: 'nicole@email.com', age: 43 },
            { name: 'Adrian', email: 'adrian@email.com', age: 21 }
        
  $scope.multipleDemo.selectedPeopleSimple = ['samantha@email.com', 'wladimir@email.com'];

Array of Object With ‘Group By’

Here, you must use the directive group-by="someGroupFn".

HTML Code

<ui-select multiple="" ng-model="multipleDemo.selectedPeopleWithGroupBy" theme="bootstrap" >
                        <ui-select-match placeholder="Select person...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match>
                        <ui-select-choices group-by="someGroupFn" repeat="person in people | propsFilter: {name: $select.search, age: $select.search}">
                            <div ng-bind-html="person.name | highlight: $select.search"></div>
                            <small>
                                email: {{person.email}} age:
                                <span ng-bind-html="''+person.age | highlight: $select.search"></span>
                            </small>
                        </ui-select-choices>
                    </ui-select>
<div class="text-sm text-muted mt-sm">Selected: {{multipleDemo.selectedPeopleWithGroupBy}}</div>

Angular Controller

$scope.people = [
            { name: 'Samantha', email: 'samantha@email.com', age: 31 },
            { name: 'Estefanía', email: 'estefanía@email.com', age: 16 },
            { name: 'Natasha', email: 'natasha@email.com', age: 54 },
            { name: 'Nicole', email: 'nicole@email.com', age: 43 },
            { name: 'Adrian', email: 'adrian@email.com', age: 21 }
        ];
$scope.someGroupFn = function (item) {

            if (item.name[0] >= 'A' && item.name[0] <= 'M')
                return 'From A - M';

            if (item.name[0] >= 'N' && item.name[0] <= 'Z')
                return 'From N - Z';

        };
$scope.multipleDemo.selectedPeopleWithGroupBy = [$scope.people[8], $scope.people[6]];
$scope.disabled = undefined;

        $scope.enable = function () {
            $scope.disabled = false;
        };

        $scope.disable = function () {
           
            $scope.disabled = true;
        };

        $scope.clear = function () {
            $scope.person.selected = undefined;
            $scope.address.selected = undefined;
            $scope.country.selected = undefined;
        };

Click the play button on VS 2017, and run the application. Now it will appear in the browser and you can see the results.

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 sectionbecause your comments are very valuable.

Happy Coding!