Я новичок в Angularjs. У меня есть 8 выпадающих списков на моей странице, и каждый выпадающий результат зависит от соответствующего раскрывающегося списка проблем: например, Outcome_1 зависит от Issue_1, Outcome_2 зависит от Issue_2 и т.д. Я заполнил все выпадающие списки из 4 проблем, но у меня возникли проблемы с заполнением их соответствующего результата выпадающие.
Если в первом выпадающем списке выбрать "OCF 18", тогда он должен заполнить первые варианты выпадающего результата, которые ("Разумные и необходимые", "Частично разумные и необходимые", "Не разумные и необходимые", "Не применимо", "Невозможно для определения "), если в первом выпадающем списке выбрать" Применимость МИГ ", тогда он должен заполнить первые варианты выпадающего результата, которые (" Соответствует Руководящим принципам MIG "," Не соответствует рекомендациям MIG "," Не применимо ", Не удалось определить ").
$scope.SelectedLine = [
{
"Event_ID": "13970897",
"IssueOutcomes": {
"List": [
{
"Issue": {
"Name": "OCF 18",
"Outcomes": [
"Reasonable and Necessary",
"Partially reasonable and necessary",
"Not reasonable and necessary",
"Not applicable",
"Unable to determine"
]
}
},
{
"Issue": {
"Name": "Applicability of the MIG",
"Outcomes": [
"Meets MIG Guidelines",
"Does not meet MIG Guidelines",
"Not applicable",
"Unable to determine"
]
}
}
]
},
"Issue_1": "Applicability of the MIG",
"Outcome_1": "Meets MIG Guidelines",
"Issue_2": "Applicability of the MIG",
"Outcome_2": "Does not meet MIG Guidelines",
"Issue_3": "Income Replacement",
"Outcome_3": "Not applicable",
"Issue_4": "Housekeeping and Home Maintenance",
"Outcome_4": "Unable to determine"
}
];
<select ng-model="SelectedLine.Issue_1" class="form-control">
<option ng-repeat="Temp in SelectedLine.IssueOutcomes.List" value="{{Temp.Issue.Name}}">{{Temp.Issue.Name}}</option>
</select>
<select ng-model="SelectedLine.Outcome_1" class="form-control"></select>
<select ng-model="SelectedLine.Issue_2" class="form-control">
<option ng-repeat="Temp in SelectedLine.IssueOutcomes.List" value="{{Temp.Issue.Name}}">{{Temp.Issue.Name}}</option>
</select>
<select ng-model="SelectedLine.Outcome_2" class="form-control"></select>
<select ng-model="SelectedLine.Issue_3" class="form-control">
<option ng-repeat="Temp in SelectedLine.IssueOutcomes.List" value="{{Temp.Issue.Name}}">{{Temp.Issue.Name}}</option>
</select>
<select ng-model="SelectedLine.Outcome_3" class="form-control"></select>
<select ng-model="SelectedLine.Issue_4" class="form-control">
<option ng-repeat="Temp in SelectedLine.IssueOutcomes.List" value="{{Temp.Issue.Name}}">{{Temp.Issue.Name}}</option>
</select>
<select ng-model="SelectedLine.Outcome_4" class="form-control"></select>
@Карим Али,
Спасибо за интересный вопрос; это была настоящая тренировка мозга для меня. Я бы изменил структуру SelectedLine
(я называю ее data
в моем коде ниже), чтобы разделить ее на три части: issues
, outcomes
и selections
. Поскольку для каждой выпущенной проблемы требуется один выпуск и один выпуск результатов, часть данных для selections
должна иметь такое же количество элементов, что и issues
. Это моя модель данных:
data = {
"Issues":[
{
"name": "OCF 18",
"issue_id":0
},
{
"name": "Applicability of the MIG",
"issue_id":1
}
],
"Outcomes": [
{
"name": "Reasonable and Necessary",
"issue_id":0
},
{
"name": "Partially reasonable and necessary",
"issue_id":0
},
{
"name": "Not reasonable and necessary",
"issue_id":0
},
{
"name": "Not applicable",
"issue_id":0
},
{
"name": "Meets MIG Guidelines",
"issue_id":1
},
{
"name": "Does not meet MIG Guidelines",
"issue_id":1
},
{
"name": "Not applicable",
"issue_id":1
},
{
"name": "Unable to determine",
"issue_id":1
}
],
"Selections": [
{
"issue_id":null,
"outcome":null
},
{
"issue_id":null,
"outcome":null
}
]
}
Обратите внимание, что каждый результат имеет issue_id
проблемы, к которой он принадлежит. В HTML
идея состоит в том, чтобы показать все проблемы в раскрывающихся списках, но показывать только результаты, относящиеся к выбранной проблеме, используя канал filter
(в противном случае будут показаны все результаты всех проблем).
<!--Cycle through all issues because we want as many dropdown groups
as we have issues -->
<div ng-repeat="Issue in data.Issues">
<!-- $index is the iteration count: 0 on first iteration
, 1 on second iteration... Link the selected issue to
the matching index in data.Selections. Store the issue_id,
not the name, as the value (we need it to filter for
the right outcomes -->
<select ng-model="data.Selections[$index].issue_id"
class="form-control">
<!-- show all the issues in every dropdown -->
<option ng-repeat="Temp in data.Issues"
value="{{Temp.issue_id}}">
{{Temp.name}}
</option>
</select>
<!-- Link the selected outcome to the same data.Selections
element as above. This will mean that every
Selections[i] will hold the issue_id selected and the
outcome name selected from that issue -->
<select ng-model="data.Selections[$index].outcome"
class="form-control">
<!-- Since all outcomes for all issues are in data.Outcomes,
we have to apply a filter to show only those outcomes matching
the selected issue. The filter below says to show only
those outcomes whose 'issue_id' property matches the issue_id
of Selections[$index], the currently selected issue id -->
<option ng-repeat="outcome in data.Outcomes
| filter: {issue_id: data.Selections[$index].issue_id}"
value="{{outcome.name}}">
{{outcome.name}}
</option>
</select>
<hr/>
</div>
Когда проблема выбрана, ее issue_id
сохраняется в issue_id
Selections
(1-й раскрывающийся issue_id
привязан к первому элементу в issue_id
Selections
). Как только issue_id
будет сохранен, канал filter
обновит результаты, показанные для отображения только результатов, соответствующих этой issue_id
. Затем, когда выбран один из этих результатов, его name
хранится рядом с issue_id
в issue_id
Selections
Надеюсь, что это полезно.
SelectedLine
или вы должны использовать ее текущую структуру?