我想初始化一个select初始值.我有一个像我这样的后端返回的Json对象:
[{Nom:"xxx", TypeIld:1, ....},{Nom:"xxx", TypeId:1, ....}]
Run Code Online (Sandbox Code Playgroud)
我有一个typeIds数组,声明如下:
[{ Nom: "Plats", TypeId: 0 },
{ Nom: "Crudités", TypeId: 1 },
{ Nom: "Tartes Salées", TypeId: 2}]
Run Code Online (Sandbox Code Playgroud)
我想在一个表中显示我的所有记录,其中select为typeId初始化为正确的值.
这是我的代码:
<form class="PlatsCuisinesEditor">
<table data-bind="visible: platscuisines().length > 0">
<thead><tr><th></th><th>Nom</th><th>Description</th><th>Prix</th><th>Frequence</th><th>Type</th><th></th></tr></thead>
<tbody data-bind='template: { name: "PCRowTemplate", foreach: platscuisines }'></tbody>
</table>
<br />
<div style="margin-top:10px;">
<button data-bind="enable: platscuisines().length > 0" type="submit">Enregistrer les plats</button>
</div>
</form>
<script type="text/html" id="PCRowTemplate">
<tr>
<td><input class="required" data-bind="value: Nom, uniqueName: true"/></td>
<td>
<select data-bind="options: viewModel.platstypes, optionsText:'Nom'"></select>
</td>
</tr>
</script>
<script type="text/javascript"> …Run Code Online (Sandbox Code Playgroud)