2017-04-03 104 views
0

下面是從服務中獲得的對象的簡化版本。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" 
     } 
    ] 
    } 
] 

我需要填充兩個dopdowns(可重複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> 

第二個下拉菜單應顯示基於o N的前面下拉

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

如何動態填充第二個下拉的原因是什麼?

獎勵:一個字段,則需要顯示根據所做的選擇

+0

的[嵌套JSON數據angularjs NG-選項(可能的複製http://stackoverflow.com/questions/41935602/angularjs-ng-options -nested-JSON數據) – Mistalis

回答

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>

1

你所要做的就是選擇你的第一選擇整個對象適當docRequired值。然後用您選擇的對象的孩子填充第二個選擇。

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