I have 4+ experience and working as software Engineer with Hytech professionals India PVT ltd and also working as Consultant,Trainer,Blogger and Author on Microsoft technologies,Also I have Trained more than 500+ IT professionals.I have knowledge about Asp.net,C#,SQL,Project management,replication,MVC,WebApi,SSRS,PostMan,TFS,SVN,Swagger,WMI,Agile,SignalR,Angularjs,IIS configuration.

Part 18::How to implement cascading dropdownlist using angularjs?

Today we are implementing exceptionally straightforward case of Cascading Dropdowns in AngularJS.
Some of the time we have to show DropDownLists in our site page such that qualities in one DropDownList are subject to the worth chose in another DropDownList. The most widely recognized case of such a usefulness is nations and states DropDownLists where in light of a chose nation you have to populate the states DropDownList. Additionally chose State you have to populate the City DropDownList.

Basic Example of Cascading DropdownList in AngularJs:-

Step 1:-Create a HTML Page
Step 2:-Include AngularJs library  to support Angularjs in your application inside head tag with script tag.
or /use CDN URL:-<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<HTML>
<head>
<script src="angular.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-app="mainApp" ng-controller="CountryStateCityCtnl">
        <div> Country:
            <select id="country" ng-model="statessource" ng-options="country for (country, states) in countries"
                ng-change="GetSelectedCountry()">
                <option value=''>Select</option>
            </select>
        </div>
        <div style='height: 15px;'>
            &nbsp;</div>
        <div>
            States:<select id="state" ng-disabled="!statessource" ng-model="citiessource" ng-options="state for (state,city) in statessource"
                ng-change="GetSelectedState()"><option value=''>Select</option>
            </select>
        </div>
        <div style='height: 15px;'>
            &nbsp;</div>
        <div>
            City:<select id="city" ng-disabled="!citiessource || !statessource" ng-model="city"><option value=''>
                Select</option>
                <option ng-repeat="city in citiessource" value='{{city}}'>{{city}}</option>
            </select>
        </div>
        <div style='height: 15px;'>
            &nbsp;</div>
        <div>
            <span>Selected Country: </span>
            <label>
                {{strCountry}}</label>
        </div>
        <div style='height: 15px;'>
            &nbsp;</div>
        <div>
            <span>Selected State: </span>
            <label>
                {{strState}}</label>
        </div>
        <div style='height: 15px;'>
            &nbsp;</div>
        <div>
            <span>Selected City: </span>
            <label>
                {{city}}</label>
        </div>
    </div>
</body>
</HTML>
Step 3:-Create Controller:-script.js file

         var mainApp = angular.module("mainApp", []); mainApp.controller('CountryStateCityCtnl', function($scope) {
            $scope.countries = {
                'India': {
                    'Maharashtra': ['Pune', 'Mumbai', 'Nagpur', 'Akola'],
                    'Madhya Pradesh': ['Indore', 'Bhopal', 'Jabalpur'],
                    'Rajasthan': ['Jaipur', 'Ajmer', 'Jodhpur']
                },
                'USA': {
                    'Alabama': ['Montgomery', 'Birmingham'],
                    'California': ['Sacramento', 'Fremont'],
                    'Illinois': ['Springfield', 'Chicago']
                },
                'Australia': {
                    'New South Wales': ['Sydney'],
                    'Victoria': ['Melbourne']
                }
            };
            $scope.GetSelectedCountry = function () {
                $scope.strCountry = document.getElementById("country").value;
            };
            $scope.GetSelectedState = function () {
                $scope.strState = document.getElementById("state").value;
            };
        });
Step 4:- Run project and check final output:-

                        Cascading Dropdowns in AngularJs Output
Description:-
    The ngOptions ascribe can be utilized to powerfully create a rundown of <option> components for the <select> component utilizing the cluster or protest got by assessing the ngOptions understanding expression. 
At the point when a thing in the <select> menu is chosen, the exhibit component or item property spoke to by the chose alternative will be bound to the model recognized by the ngModel mandate.
In this given example first we are select the country and pass the statesource, similar also select the state from the statesource and pass citysource in the ng-model see below image…

Cascading Dropdowns in AngularJs_Source_Pass

Also we are using the ng-change for get the value of selected country and state. See image below…
                                       Cascading Dropdowns in AngularJs_Selectedvalue

You have just read an article that categorized by title AngularJs by title Part 18::How to implement cascading dropdownlist using angularjs?. You can bookmark this page with a URL https://bikeshsrivastava.blogspot.com/2016/06/part-18how-to-implement-cascading.html. Thank You!
Author: Bikesh Srivastava - Wednesday, 8 June 2016

There are currently no comments for "Part 18::How to implement cascading dropdownlist using angularjs?"

Post a Comment

Life Is Complicated, But Now programmer Can Keep It Simple.