Note that there are some explanatory texts on larger screens.

plurals
  1. POKnockout.js repeat rows in table for nested model / containerless "foreach" inside table
    primarykey
    data
    text
    <p>I’m building an application to track orders of tailor made products. Each product can have many custom details. The screen to add products to an order and customize each one should look like this:</p> <pre><code>&lt;button&gt;+&lt;/button&gt;&lt;!-- "Add new product line" button --&gt; &lt;table&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;&lt;/th&gt; &lt;th&gt;Producto&lt;/th&gt;&lt;!-- The product category or type --&gt; &lt;th&gt;Modelo&lt;/th&gt;&lt;!-- The product --&gt; &lt;th&gt;Cantidad&lt;/th&gt;&lt;!-- Quantity --&gt; &lt;th&gt;Unitario&lt;/th&gt;&lt;!-- Unit Price --&gt; &lt;th&gt;Mano de Obra&lt;/th&gt;&lt;!-- The price of the product itself --&gt; &lt;th&gt;Genero&lt;/th&gt;&lt;!-- The price of the customization --&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr class="producto"&gt;&lt;!-- Product line --&gt; &lt;td&gt;&lt;button&gt;-&lt;/button&gt;&lt;/td&gt;&lt;!-- "Remove" button, should remove the product and it's customizations --&gt; &lt;td&gt;&lt;select&gt;Producto&lt;/select&gt;&lt;/td&gt;&lt;!-- Choose category --&gt; &lt;td&gt;&lt;select&gt;Modelo&lt;/select&gt;&lt;/td&gt;&lt;!-- Choose product --&gt; &lt;td&gt;&lt;input type="text" class="cantidad" /&gt;&lt;/td&gt;&lt;!-- Enter quantity --&gt; &lt;td&gt;&lt;input type="text" class="unitario" /&gt;&lt;/td&gt;&lt;!-- Enter unit price --&gt; &lt;td&gt;$ &lt;span class="mano_obra"&gt;&lt;/span&gt;&lt;/td&gt;&lt;!-- Line total. The product lines calculates on this column --&gt; &lt;td&gt;&lt;button&gt;+&lt;/button&gt;&lt;/td&gt;&lt;!-- "Add customization" button. Should add a line like the next &lt;tr&gt; --&gt; &lt;/tr&gt; &lt;tr class="genero"&gt;&lt;!-- Customization line --&gt; &lt;td&gt;&lt;button&gt;-&lt;/button&gt;&lt;/td&gt;&lt;!-- "Remove" button, should remove only this customization line --&gt; &lt;td&gt;Genero&lt;/td&gt;&lt;!-- Fixed text --&gt; &lt;td&gt;&lt;input type="text" class="genero" /&gt;&lt;/td&gt;&lt;!-- Enter customization description --&gt; &lt;td&gt;&lt;input type="text" class="cantidad" /&gt;&lt;/td&gt;&lt;!-- Enter quantity --&gt; &lt;td&gt;&lt;input type="text" class="unitario" /&gt;&lt;/td&gt;&lt;!-- Enter unit price --&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;!-- On customizations, this column is empty --&gt; &lt;td&gt;$ &lt;span class="genero"&gt;&lt;/span&gt;&lt;/td&gt;&lt;!-- Line total. The customizations calculates on this column --&gt; &lt;/tr&gt; &lt;tr class="genero"&gt; &lt;!-- Another customization for the first product --&gt; &lt;/tr&gt; &lt;tr class="genero"&gt; &lt;!-- Another one --&gt; &lt;/tr&gt; &lt;tr class="producto"&gt; &lt;!-- A different product --&gt; &lt;/tr&gt; &lt;tr class="genero"&gt; &lt;!-- The new product customization --&gt; &lt;/tr&gt; &lt;!-- (etc) --&gt; &lt;/tbody&gt; &lt;tfoot&gt; &lt;tr&gt; &lt;td colspan="5"&gt;Subtotales&lt;/td&gt;&lt;!-- Fixed text --&gt; &lt;td&gt;$ &lt;span class="subtotal_mano_obra"&gt;&lt;/span&gt;&lt;/td&gt;&lt;!-- SUM of each line total, for products --&gt; &lt;td&gt;$ &lt;span class="subtotal_genero"&gt;&lt;/span&gt;&lt;/td&gt;&lt;!-- SUM of each line total, for customizations --&gt; &lt;/tr&gt; &lt;/tfoot&gt; &lt;/table&gt; </code></pre> <p>I've tried to do this:</p> <pre><code>&lt;tbody data-bind='foreach: lineas_pedido'&gt; &lt;tr class="producto"&gt; &lt;!-- All the bindings here works fine --&gt; &lt;/tr&gt; &lt;!-- ko foreach: generos --&gt; &lt;tr class="genero"&gt; &lt;!-- ... --&gt; &lt;/tr&gt; &lt;!-- /ko --&gt; &lt;/tbody&gt; </code></pre> <p>But after getting errors and looking, came to this: <a href="https://stackoverflow.com/questions/8115896/knockout-js-containerless-foreach-not-working-with-table">Knockout.js containerless &quot;foreach&quot; not working with &lt;table&gt;</a></p> <p>So, I found this plugin: <a href="https://github.com/mbest/knockout-repeat" rel="nofollow noreferrer">https://github.com/mbest/knockout-repeat</a> And now my code looks like this:</p> <pre><code>&lt;tbody data-bind='foreach: lineas_pedido'&gt; &lt;tr class="producto"&gt; &lt;!-- All the bindings here works fine --&gt; &lt;/tr&gt; &lt;tr class="genero" data-bind="repeat: {foreach: generos, item: '$genero'}"&gt; &lt;!-- ... --&gt; &lt;/tr&gt; &lt;/tbody&gt; </code></pre> <p>My question is: Is there any way to avoid using the plugin, and accomplish the same result using native KO templating/bindings?</p> <p>Thanks in advance.</p> <p><strong>Edit</strong>: </p> <p><a href="http://jsfiddle.net/dXsyj/3/" rel="nofollow noreferrer" title="jsfiddle">Here</a> is the jsfiddle, I've added a resource linking to my sample data (categories and products).</p> <p><a href="http://elboyero.oestedatos.com/deco.oestedatos.com/nuevo_pedido2.html" rel="nofollow noreferrer" title="My real scenario">Here</a> is the actual code on my testing host.</p> <p>Also, I've used <a href="http://knockoutjs.com/examples/cartEditor.html" rel="nofollow noreferrer">this</a> example as the start point.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload