0

Here is a simplified version of an object I get from a service.

[
  {
    "lookupID": "Annual Leave",
    "lookupCode": "Annual Leave",
    "reasonRecords": [
      {
        "reason": "Annual",
        "docRequired": "N"
      },
      {
        "reason": "Mandatory",
        "docRequired": "N"
      }
    ]
  },
  {
    "lookupID": "Accumulated Leave",
    "lookupCode": "Accumulated Leave",
    "reasonRecords": [
      {
        "reason": "Accumulative",
        "docRequired": "N"
      },
      {
        "reason": "Cancellation",
        "docRequired": "Y"
      }
    ]
  },
  {
    "lookupID": "Study Leave",
    "lookupCode": "Study Leave",
    "reasonRecords": [
      {
        "reason": "Examination Leave",
        "docRequired": "N"
      },
      {
        "reason": "Research Leave",
        "docRequired": "N"
      }
    ]
  }
]

I need to populate two dopdowns (which can be repeated x number of times)

End result should look something like this enter image description here

The first is already populated and working, it displays all the possible LookupID's

<select class="form-control" id="leavetype_{{$index}}" ng-model="leaveEntry.leaveTypeKey" value="leaveEntry.leaveTypeKey" ng-required>
    <option ng-repeat="leaveType in leaveTypes" value="{{leaveType.lookupCode}}">{{leaveType.lookupCode}}</option>
</select>

The second dropdown should display the possible reasons based on the lookupCode selected in the previous dropdown

It's currently just hardcoded to show the reasons of the first lookupCode

<select class="form-control" id="reason_{{$index}}" ng-model="leaveEntry.reason" value="leaveEntry.reason" ng-required>
    <option ng-repeat="reasonRecord in leaveTypes[0].reasonRecords" value="{{reasonRecord.reason}}" >{{reasonRecord.reason}}</option>
</select>

How do I dynamically populate the second dropdown ?

Bonus: a field then needs to display the appropriate docRequired value based on the selections

1

2 Answers 2

1

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>
<html>
<head>

<script>
var app = angular.module('myApp', []);
app.controller('ctrl', ['$scope', function($scope) {
 
  $scope.selectData = [
  {
    "lookupID": "Annual Leave",
    "lookupCode": "Annual Leave",
    "reasonRecords": [
      {
        "reason": "Annual",
        "docRequired": "N"
      },
      {
        "reason": "Mandatory",
        "docRequired": "N"
      }
    ]
  },
  {
    "lookupID": "Accumulated Leave",
    "lookupCode": "Accumulated Leave",
    "reasonRecords": [
      {
        "reason": "Accumulative",
        "docRequired": "N"
      },
      {
        "reason": "Cancellation",
        "docRequired": "Y"
      }
    ]
  },
  {
    "lookupID": "Study Leave",
    "lookupCode": "Study Leave",
    "reasonRecords": [
      {
        "reason": "Examination Leave",
        "docRequired": "N"
      },
      {
        "reason": "Research Leave",
        "docRequired": "N"
      }
    ]
  }
]
}]);
</script>
</head>
<body ng-app="myApp">
<div ng-controller="ctrl">
<select ng-options="data as data.lookupCode for data in selectData" ng-model="lookupCodeselected"> </select>
 <select ng-disabled="!lookupCodeselected" ng-options="data as data.reason for data in lookupCodeselected.reasonRecords" ng-model="selected2"></select>
</div>
</body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

1

What you have to do is select the whole object in your first select. Then populate the second select with the children of the object you selected.

<select ng-options="n as n.lookupCode for n in data" ng-model="selected">      
  </select>
  <select ng-options="n as n.reason for n in selected.reasonRecords" ng-model="selected2">      
  </select>

http://jsfiddle.net/hLhtbxb8/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.