knockoutjs を使用して単純なカスケード コンボボックスを作成しようとしています。私の最初のコンボボックスは、viewmodel の 2 つのプロパティにバインドします。
コンボボックス オプションのソースとしてのBusinessLines
最初のコンボボックスで選択された項目としてSelectedBusinessLine 。
各 BusinessLine には、クラスターのコレクションがあります。
2 番目のコンボボックスは、コンボボックス オプション ソースの SelectedBusinessLine.Clusters オブザーバブルと、選択されたオプションの SelectedCluster にデータ バインドする必要があります。
問題は、2 番目のコンボボックスがまったく読み込まれないことです。
JsFiddleのソース(JsFiddle では、最初のバインディングも機能しません。初めて使用します)
JavaScript
var mainViewModel = null;
$(document).ready(function() {
var mainViewModelData = <%= JsonConvert.SerializeObject(NewRequestViewModel) %>;
mainViewModel = ko.mapping.fromJS(mainViewModelData);
ko.applyBindings(mainViewModel);
});
HTML
<div>
<table>
<tr>
<td>
Business Line
</td>
<td>
<select data-bind="options: BusinessLines,
optionsText: 'Title',
value: SelectedBusinessLine,
optionsCaption: 'Select Business Line..'">
</select>
</td>
</tr>
<tr>
<td>
Cluster
</td>
<td>
<select data-bind="options: SelectedBusinessLine.Clusters,
optionsText: 'Title',
value: SelectedCluster,
optionsCaption: 'Select Cluster..'">
</select>
</td>
</tr>
</table>
</div>
更新:
2 番目のソリューション(計算された小道具なし)
<select data-bind="options: SelectedBusinessLine() ? SelectedBusinessLine().Clusters() : [],
optionsText: 'Title',
value: SelectedCluster,
optionsCaption: 'Select Cluster..'">
</select>