次のシナリオがあります。ko.mapping を使用してサーバーからデータを取得しており、次の形式に変換されています。
var viewModel = {
name: "Abc",
educations: [{ course: "C1", countryID: 1, cityID = 2},
{ course: "C2", countryID: 2, cityID = 3}]
}
また、次の 2 つの配列変数があります。
var countries = [{id=1,name="UAE"},{id=2,name="USA"},];
var cities = [{id=1,name="Dubai", cid=1},{id=2,name="Al-Ain", cid=1},
{id=3,name="Newyork", cid=2},{id=4,name="Houston", cid=2},];
このデータを表示/編集するために、次のHTMLがあります
<div>
<input type="text" data-bind="value: Name"/>
<table data-bind="template: { name: 'cet', foreach: educations }">
</table>
</div>
<script type="text/html" id="cet">
<tr>
<td>
<select data-bind="options: countries, optionsText: 'name', optionsValue: 'id', optionsCaption: 'Select...', value: countryID"></select>
</td>
<td>
<select data-bind="options: cities, optionsText: 'name', optionsValue: 'id', optionsCaption: 'Select...', value: cityID"></select>
</td>
</tr>
</script>
ここで必要なのは、データがサーバーから送信されるときに、バインドされたオブジェクトに対応する正しいアイテムが選択ボックスに表示されることです。