我有以下场景,我使用 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>
现在我需要的是,当从服务器发送数据时,选择框应该显示与绑定对象对应的正确项目。