AngularJS - 填充嵌套数组中的两个下拉列表

时间:2017-04-03 13:12:36

标签: angularjs

以下是我从服务中获得的对象的简化版本。

[
  {
    "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"
      }
    ]
  }
]

我需要填充两个掺杂物(可以重复x次)

最终结果应该是这样的 enter image description here

第一个已经填充并正常工作,它会显示所有可能的LookupID

<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>

第二个下拉列表应根据前一个下拉列表中选择的lookupCode显示可能的原因

它目前只是硬编码以显示第一个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>

如何动态填充第二个下拉列表?

奖励:然后一个字段需要根据选择显示相应的docRequired值

2 个答案:

答案 0 :(得分:1)

您需要做的是在第一个选择中选择整个对象。然后使用所选对象的子项填充第二个选择。

<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/

答案 1 :(得分: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>