Michael Mahony
Michael Mahony

Reputation: 1350

Angular and linked drop down lists not holding data

I am attempting to make my view contain 3 dropdown lists and a submit button. I have figured out how to not show a dropdown until it contains data. I've figured out how to get it to load upon change of the previous dropdown. However, I have a couple of issues. Here is the code and then I will explain the issues:

The view:

<div ng-controller="UserCtrl">
    <div class="row" ng-show="$parent.loggedin">
        <div class="col-sm-3">
            <div class="form-group">
                <label for="lawType">Type of Law</label>
                <select class="form-control" id="lawType" ng-change="getCourthouse();" data-ng-model="typeoflaw">
                    <option value="0" selected>--Select a Type of Law--</option>
                    <option ng-repeat="type in typeoflaw" value="{{ type.LitigationCode}}">{{ type.LitigationType }}</option>
                </select>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="form-group" data-ng-show="courtHouse.length">
                <label for="courtHouse">Courthouse</label>
                <select class="form-control" id="courtHouse" data-ng-model="courtHouse" ng-change="getCourtroom();">
                    <option ng-repeat="bldg in courtHouse track by $index" value="{{ bldg.Loc_ID }}">{{ bldg.Loc_Name }}</option>
                </select>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="form-group" data-ng-show="courtRoom.length" data-ng-model="courtRoom">
                <label for="courtRoom">Department</label>
                <select class="form-control" id="courtRoom">
                    <option ng-repeat="room in courtRoom" value="{{ room.CourtRoom }}">{{ room.CourtRoom }}</option>
                </select>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="favorite-button">
                <button class="btn  btn-primary pull-left">Add Favorite</button>
            </div>
        </div>
    </div>
</div>

The controller:

JBenchApp.controller('UserCtrl', ['$scope', '$routeParams', '$http', '$filter', 'UserService', 
  function ($scope, $routeParams, $http, $filter, UserService) {
          // Get the preferences information
      /**UserService.loadType()
      .then(function (lawtypes) {
          $scope.typeoflaw = lawtypes;
      });**/



      $http.get('http://10.34.34.46/BenchViewServices/api/Litigation').success(function (response) {
          $scope.typeoflaw = response;
      });

      //$scope.courtHouse = [{ "Loc_ID": "0", "Loc_Name": "-- Select a Type of Law to Get Courthouse List --" }];
      //$scope.courtHouse = [];
      //$scope.courtRoom = [];

      $scope.getCourthouse = function () {
          var e = document.getElementById("lawType");
          $scope.typeoflawId = e.options[e.selectedIndex].value;
          console.log($scope.typeoflawId);
          $http.get('http://10.34.34.46/BenchViewServices/api/CourtDept/' + $scope.typeoflawId).success(function (response) {
              $scope.courtHouse = response;

          }).error(function (status, data) {
              console.log("error trapped");
          });
      }

      $scope.getCourtroom = function () {
          var e = document.getElementById("courtHouse");
          $scope.courtHouseId = e.options[e.selectedIndex].value;
          console.log($scope.courtHouseId);
          $http.get('http://10.34.34.46/BenchViewServices/api/CourtDept/' + $scope.typeoflawId + "/" + $scope.courtHouseId).success(function (response) {
              $scope.courtRoom = response;

          }).error(function (status, data) {
              console.log("error trapped");
          });
      }

      $scope.SavePreferences = function () {

      };

  }]);

The issues:

  1. When the first dropdown, Type of Law, loads, it does not have "--Select a type of law--" selected as I requested in the markup. In fact, there is just an empty entry that is above the "select a type of law" and it is selected by default. How can I make the initial entry in the list be selected at load?
    1. When the first dropdown is changed (a selection is made) it loads the second dropdown, but the first dropdown is then empty other than the blank row mentioned previously and the "select a type of law" entry. How do I make it hold the data that was there and show the selected item correctly?
    2. When the second dropdown is changed, it loads the third drop down, but has the same problem as the first one...it empties itself and it also doesn't show the selection the user makes. How do I make it not empty itself and still show the selection the user made?

thanks!

Upvotes: 1

Views: 55

Answers (2)

Michael Mahony
Michael Mahony

Reputation: 1350

Here is how I ultimately solved the issue.

In the view:

<div ng-controller="UserCtrl">
    <div class="row" ng-show="$parent.loggedin">
        <div class="col-sm-3">
            <div class="form-group">
                <label for="lawType">Select a Type of Law</label>
                <select class="form-control" id="lawType" name="lawType" ng-change="getCourthouse();" ng-model="typeoflaw.LitigationType" ng-options="typeoflaw.LitigationType for typeoflaw in typeoflaw track by typeoflaw.LitigationCode" required>
                    <option value="">--Select a Type of Law--</option>
                   <!-- <option value="0" selected>--Select a Type of Law--</option>
                    <option ng-repeat="type in typeoflaw" value="{{ type.LitigationCode}}">{{ type.LitigationType }}</option>-->
                </select>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="form-group" ng-show="courtHouse.length">
                <label for="courtHouse">Select a Courthouse</label>
                <select class="form-control" id="courtHouse" name="courtHouse" ng-model="courtHouse.Loc_ID" ng-change="getCourtroom();" ng-options="courtHouse.Loc_Name for courtHouse in courtHouse track by courtHouse.Loc_Name" required>
                    <option value="">--Select a Courthouse--</option>
                    <!--<option ng-repeat="bldg in courtHouse track by $index" value="{{ bldg.Loc_ID }}">{{ bldg.Loc_Name }}</option>-->
                </select>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="form-group" ng-show="courtRoom.length">
                <label for="courtRoom">Select a Department</label>
                <select class="form-control" id="courtRoom" name="courtRoom" ng-model="courtRoom.CourtRoom" ng-options="courtRoom.CourtRoom for courtRoom in courtRoom track by courtRoom.CourtRoom" required>
                    <option value="">--Select a Department--</option>
                    <!-- <option ng-repeat="room in courtRoom" value="{{ room.CourtRoom }}">{{ room.CourtRoom }}</option>-->
                </select>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="favorite-button" ng-show="courtRoom.length">
                <button class="btn  btn-primary pull-left" ng-click="SavePreferences();">Add Favorite</button>
            </div>

        </div>
    </div>
    <div class="row" ng-show="userPreferences.length">
        <div class="col-sm-12 favorite-header">
            <h2>Your Saved Favorites</h2>
        </div>
        <div class="col-sm-12 favorite-filter">
            <input type="text" ng-model="query" placeholder="Filter your favorites list" />
        </div>
    </div>
    <div class="row" ng-show="userPreferences.length">
        <div class="col-sm-3 favorite-column-title">
            Courthouse
        </div>
        <div class="col-sm-3 favorite-column-title">
            Department
        </div>
        <div class="col-sm-3 favorite-column-title">
            Type of Law
        </div>
        <div class="col-sm-3 favorite-column-title">
            Default
        </div>
    </div>
    <div class="row" ng-show="userPreferences.length" ng-model="userPreferences" ng-repeat-start="userPreference in userPreferences | filter:query | orderBy: ['LocName', 'CourtRoom']">
        <div class="col-sm-3">
            <span class="get-path" ng-click="setPathValues(userPreference.LitigationCode, userPreference.LocID, userPreference.CourtRoom);">{{ userPreference.LocName }} ({{ userPreference.CourtRoom}})</span>
</div>
        <div class="col-sm-3">
            {{ userPreference.CourtRoom}}
        </div>
        <div class="col-sm-3">
            {{ userPreference.LitigationType }}
        </div>
        <div class="col-sm-3">
            <span ng-if="showDefaultIcon(userPreference.IsDefault);" class="glyphicon glyphicon-ok green-check" data-toggle="tooltip" data-placement="top" title="This is your default favorite and it cannot be deleted. Please mark another favorite as your default before attempting to delete this one." tooltip></span>
            <span ng-if="!showDefaultIcon(userPreference.IsDefault);" class="glyphicon glyphicon-star-empty red-star" ng-click="setAsDefault(userPreference.PreferenceID, userPreference.LitigationCode, userPreference.LocID, userPreference.CourtRoom);" data-toggle="tooltip" data-placement="top" title="Mark this favorite as your default." tooltip></span>
            <span ng-if="!showDefaultIcon(userPreference.IsDefault);" class="glyphicon glyphicon-remove delete-icon" ng-click="deletePreference(userPreference.PreferenceID);" data-toggle="tooltip" data-placement="top" title="Delete this favorite." tooltip></span>
        </div>
    </div>
    <div ng-repeat-end></div>
</div>

In the controller:

JBenchApp.controller('UserCtrl', ['$scope', '$routeParams', '$http', '$filter', '$window', 'HoldState', 
  function ($scope, $routeParams, $http, $filter, $window, HoldState) {


      $scope.isDefault = false;


      $http.get('http://10.34.34.46/BenchViewServices/api/Litigation').success(function (response) {
          $scope.typeoflaw = response;
      });

      $scope.getCourthouse = function () {
          $scope.typeoflawId = $scope.typeoflaw.LitigationType;
          console.log($scope.typeoflawId);
          $scope.courtRoom = [];
          $http.get('http://10.34.34.46/BenchViewServices/api/CourtDept/' + $scope.typeoflawId.LitigationCode).success(function (response) {
              $scope.courtHouse = response;

          }).error(function (status, data) {
              console.log("error trapped");
          });
      }

      $scope.getCourtroom = function () {
          var e = document.getElementById("courtHouse");
          $scope.courtHouseId = $scope.courtHouse.Loc_ID;
          console.log($scope.courtHouseId);
          $http.get('http://10.34.34.46/BenchViewServices/api/CourtDept/' + $scope.typeoflawId.LitigationCode + "/" + $scope.courtHouseId.Loc_ID).success(function (response) {
              $scope.courtRoom = response;

          }).error(function (status, data) {
              console.log("error trapped");
          });
      }

      $scope.LoadPreferences = function () {

          $http.get('http://10.34.34.46/BenchViewServices/api/UserPreference/dpeng').success(function (response) {
              $scope.userPreferences = response;
          }).error(function (status, data) {
              console.log("error trapped");
          });
      }

      $scope.SavePreferences = function () {
          $scope.userid = 'dpeng';
          $scope.departmentNumber = $scope.courtRoom.CourtRoom;
          $scope.newPreference = {
              "PreferenceID": "0",
              "UserID": $scope.userid,
              "LocID": $scope.courtHouseId.Loc_ID,
              "CourtRoom": $scope.departmentNumber.CourtRoom,
              "IsDefault": $scope.isDefault,
              "LitigationCode": $scope.typeoflawId.LitigationCode
          };
          $http({
              method: 'POST',
              url: 'http://10.34.34.46/BenchViewServices/api/UserPreference/Post',
              data: $scope.newPreference,
              headers: {
                  'Content-Type': 'application/json'
              }
          }).then(function(response) {
              LoadPreferences();
              console.log(response);
          }, function(response) {
              console.log(response.status + " -- " + response.data + " -- " + response.statusText);
          });
      }

      $scope.showDefaultIcon = function (theStatus){
          return theStatus;
      }

      $scope.showSetAsDefaultIcon = function (theStatus) {
          if (theStatus) {
              return false;
          } else {
              return true;
          }
      }

      $scope.setAsDefault = function (prefId, LitCode, LocID, Dept) {
          alert("setAsDefault function is called")
          var data = {
              "LitigationCode": LitCode,
              "LocID": LocID,
              "CourtRoom": Dept,
              "IsDefault": true
          };

          $http.put(' http://10.34.34.46/BenchViewServices/api/UserPreference/Put/' + prefId, data)
          .then(function (response) {
              LoadPreferences();
              console.log(response);
          }, function (response) {
              console.log(response.status + " -- " + response.data + " -- " + response.statusText);
          });
      }

      $scope.deletePreference = function (prefId) {
          $http.delete('http://10.34.34.46/BenchViewServices/api/UserPreference/Delete/' + prefId);
          LoadPreferences();
      }

      $scope.setPathValues = function (LawType, Bldg, Dept) {
          HoldState.setTypeOfLaw(LawType);
          HoldState.setCourthouse(Bldg);
          HoldState.setDepartment(Dept);
          var a = HoldState.getTypeOfLaw();
          var b = HoldState.getCourthouse();
          var c = HoldState.getDepartment();
          //alert('Law: ' + a + ' Courthouse: ' + b + ' Department: ' + c);
          $window.location.href = 'calendar';
      }

      $scope.LoadPreferences();

  }]);

The key is ng-options:

ng-model="courtHouse.Loc_ID" " ng-options="courtHouse.Loc_Name for courtHouse in courtHouse track by courtHouse.Loc_Name"

When I set things up like this the 3 dropdowns sync up perfectly.

Upvotes: 0

add-Naan
add-Naan

Reputation: 158

the way i solved the problem of passing data between one or more controllers was by using services. so this was an example of a service

var NameService = function () {
    var service = this;
    service.property= value;
    service.getProperty = function () {
        return property;
    }
};
ngapp.service('NameService', NameService);

the cool thing about it is you can inject your service in your controller

appcontroller.$inject = ['$scope', '$http', 'NameService'];
$.ajaxSetup({
    async: false
});
var NameCtrl = function ($scope, $http, NameService) {
    var Property = NameService.Property;
    $scope.update = function () {
        NameService.Property= $scope.Properties.selectedProperty.PropertyId;
    }
};
appcontroller.controller('NameCtrl', NameCtrl);

Upvotes: 0

Related Questions