我正在構建跟蹤定製產品訂單的應用程序。每個產品可以有許多自定義的細節。將產品添加到訂單和定製每一個屏幕應該是這樣的:Knockout.js在表格中重複嵌套模型/無內容「foreach」中的表格
<button>+</button><!-- "Add new product line" button -->
<table>
<thead>
<tr>
<th></th>
<th>Producto</th><!-- The product category or type -->
<th>Modelo</th><!-- The product -->
<th>Cantidad</th><!-- Quantity -->
<th>Unitario</th><!-- Unit Price -->
<th>Mano de Obra</th><!-- The price of the product itself -->
<th>Genero</th><!-- The price of the customization -->
</tr>
</thead>
<tbody>
<tr class="producto"><!-- Product line -->
<td><button>-</button></td><!-- "Remove" button, should remove the product and it's customizations -->
<td><select>Producto</select></td><!-- Choose category -->
<td><select>Modelo</select></td><!-- Choose product -->
<td><input type="text" class="cantidad" /></td><!-- Enter quantity -->
<td><input type="text" class="unitario" /></td><!-- Enter unit price -->
<td>$ <span class="mano_obra"></span></td><!-- Line total. The product lines calculates on this column -->
<td><button>+</button></td><!-- "Add customization" button. Should add a line like the next <tr> -->
</tr>
<tr class="genero"><!-- Customization line -->
<td><button>-</button></td><!-- "Remove" button, should remove only this customization line -->
<td>Genero</td><!-- Fixed text -->
<td><input type="text" class="genero" /></td><!-- Enter customization description -->
<td><input type="text" class="cantidad" /></td><!-- Enter quantity -->
<td><input type="text" class="unitario" /></td><!-- Enter unit price -->
<td> </td><!-- On customizations, this column is empty -->
<td>$ <span class="genero"></span></td><!-- Line total. The customizations calculates on this column -->
</tr>
<tr class="genero">
<!-- Another customization for the first product -->
</tr>
<tr class="genero">
<!-- Another one -->
</tr>
<tr class="producto">
<!-- A different product -->
</tr>
<tr class="genero">
<!-- The new product customization -->
</tr>
<!-- (etc) -->
</tbody>
<tfoot>
<tr>
<td colspan="5">Subtotales</td><!-- Fixed text -->
<td>$ <span class="subtotal_mano_obra"></span></td><!-- SUM of each line total, for products -->
<td>$ <span class="subtotal_genero"></span></td><!-- SUM of each line total, for customizations -->
</tr>
</tfoot>
</table>
我試着這樣做:
<tbody data-bind='foreach: lineas_pedido'>
<tr class="producto">
<!-- All the bindings here works fine -->
</tr>
<!-- ko foreach: generos -->
<tr class="genero">
<!-- ... -->
</tr>
<!-- /ko -->
</tbody>
但得到錯誤和尋找後,來到了這一點: Knockout.js containerless "foreach" not working with <table>
所以,我發現這個插件:https://github.com/mbest/knockout-repeat 現在我的代碼看起來是這樣的:
<tbody data-bind='foreach: lineas_pedido'>
<tr class="producto">
<!-- All the bindings here works fine -->
</tr>
<tr class="genero" data-bind="repeat: {foreach: generos, item: '$genero'}">
<!-- ... -->
</tr>
</tbody>
我的問題是:有沒有辦法避免使用插件,並使用本機KO模板/綁定完成相同的結果?
在此先感謝。
編輯:
Here是的jsfiddle,我已經添加了鏈接到我的樣本數據(類別和產品)的資源。
Here是我測試主機上的實際代碼。
此外,我用this作爲起點。
是否會更容易爲人們幫助,如果您發佈在http://jsfiddle.net/ – 2012-03-16 03:37:41
小提琴也許你可以關閉瑞普這一個:http://jsfiddle.net/rniemeyer/dXsyj/ 。我很感興趣的是,當你在tbody內部使用無容器的foreach時,你會發現錯誤來自哪裏。 – 2012-03-16 03:57:33
您提供的示例效果很好,所以我認爲我的錯誤在其他地方。您可以在更新後的版本和我的網站中看到代碼。希望你能幫助。謝謝! – 2012-03-16 04:59:37