javascript - How can I get cascading dropdown at level 4? -
here attempt: if user hits territory need display "sales team a" , "sales team b" options.`enter code here: http://plnkr.co/edit/n2a83thk4r0g1zwddb7v?p=preview.
<!doctype html> <html > <head> <meta charset="utf-8"> <link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css'> <link rel="stylesheet" href="css/style.css"> </head> <body> <div data-ng-app="myapp" class="container"> <form class="form-horizontal" data-ng-controller="dropdownctrl"> <div class="form-group"> <label for="country" class="col-sm-2 control-label">* </label> <div class="col-sm-7"> <select data-ng-model="customer.country" data-ng-options="obj.id obj.country obj in countries" data-ng-change="getcountrystates()" class="form-control" data-ng-required="true" id="country"> <option value="">-- choose org --</option> </select> </div> </div> <div class="form-group"> <label for="state" class="col-sm-2 control-label">** </label> <div class="col-sm-7"> <select data-ng-model="customer.state" data-ng-options="x.id x.state x in sates" data-ng-change = "getstatecities()" class="form-control" data-ng-required="true" id="state"> <option value="">-- select --</option> </select> </div> </div> <div class="form-group"> <label for="city" class="col-sm-2 control-label">*** </label> <div class="col-sm-7"> <select data-ng-model="customer.city" data-ng-options="x.id x.city x in cities" data-ng-change = "getstatessales()" class="form-control" data-ng-required="true" id="city"> <option value="">-- select --</option> </select> </div> </div> <div class="form-group"> <label for="sales" class="col-sm-2 control-label">**** </label> <div class="col-sm-7"> <select data-ng-model="customer.sales" data-ng-options="x.id x.sales x in mysales" class="form-control" data-ng-required="true" id="sales"> <option value="">-- select --</option> </select> </div> </div> </form> </div> <script src='http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.min.js'></script> <script src="js/index.js"></script> </body> </html>
here index.js file:
var myapp = angular.module('myapp',[]); myapp.controller('dropdownctrl', ['$scope','customerservice', function($scope, customerservice) { $scope.countries = customerservice.getcountry(); $scope.getcountrystates = function(){ $scope.sates = customerservice.getcountrystate($scope.customer.country); $scope.cities =[]; $scope.mysales = []; } $scope.getstatecities = function(){ // debugger; $scope.cities = customerservice.getstatecity($scope.customer.state); console.log("hi"); // console.log($scope.cities); } $scope.getstatessales = function(){ // debugger; $scope.sales = customerservice.getsaleslist($scope.customer.sales); // console.log($scope.sales); } }]); myapp.factory("customerservice", ['$filter', function($filter){ var service = {}; var countrylist = [ { "id": 1, "country": "marketing" }, { "id": 2, "country": "sales" }, { "id": 3, "country": "customer service" } ]; var statelist = [ {"id":1, "state":"marketing team a", "countryid": 1}, {"id":3, "state":"marketing team b", "countryid": 1}, {"id":4, "state":"territories", "countryid": 2} ]; var citylist = [ {"id":5, "city":"territory a", "stateid": 4}, {"id":6, "city":"territory b", "stateid": 4} ]; var salesteamlist = [ {"id":1, "sales":"sales team a", "salesid": 5}, {"id":2, "sales":"sales team b", "salesid": 5}, {"id":3, "sales":"sales team a", "salesid": 6}, {"id":4, "sales":"sales team b", "salesid": 6} ]; service.getcountry = function(){ return countrylist; }; service.getcountrystate = function(countryid){ var states = ($filter('filter')(statelist, {countryid: countryid})); return states; }; service.getstatecity = function(stateid){ var items = ($filter('filter')(citylist, {stateid: stateid})); return items; }; service.getsaleslist = function(salesid){ var items = ($filter('filter')(salesteamlist, {salesid: salesid})); return items; console.log(items); }; return service; }]);
you missed 2 points:
i changed here parameter $scope.customer.city
$scope.getstatessales = function(){ $scope.sales = customerservice.getsaleslist($scope.customer.city); }
and in last drop down passed wrong list mysales changed sales in ng-repeat
data-ng-options="x.id x.sales x in sales"
Comments
Post a Comment