开发者

Knockout.js dynamic composable table

开发者 https://www.devze.com 2023-04-12 10:54 出处:网络
I\'m trying to make a \"dynamic table\" with knockout js and I\'m having a bit of difficulty. I want to have a \"Common Row Template\" with a nested template for the variable columns. Something like t

I'm trying to make a "dynamic table" with knockout js and I'm having a bit of difficulty. I want to have a "Common Row Template" with a nested template for the variable columns. Something like this:

<script type="text/x-jquery-tmpl" id="CommonRow">
<td><input type="text" data-bind="value: Nome" /></td>
<td data-bind="template: { name: $item.LabelOne + 'Row' }"></td>
<td><button class="fancybox edit" data-bind="click: Edit">Modifica</button></td>
<td><button class="remove" data-bind="click: Remove">Rimuovi</button></td&开发者_如何学运维gt;
</script>

So the second <td> will render a template, which will look like this:

<script type="text/x-jquery-tmpl" id="ScalaRow">
<td><input type="text" data-bind="value: PianiFuoriTerra" /></td>
<td><input type="text" data-bind="value: Foo" /></td>
</script>

This "works" but it has a big problem: the desired <td> are nested in the outer <td> with the template binding, causing improper alignment with the header (which also is structured the same way).

I tried using the {{tmpl}} syntax to avoid the wrapping <td> and this gets me the right html, but all the databinding and observable don't work anymore in the dynamic part.

Is there a way to render a block of <td> preserving the knockout observable functionality and avoiding any wrapping tag? Thanks.


Your best option is to look at using KO 1.3 beta. Here is a sample of doing something like what you want: http://jsfiddle.net/rniemeyer/wmDfv/

<table data-bind="foreach: items">
    <tr>
        <td data-bind="text: name"></td>
        <!-- ko template: type -->
        <!-- /ko -->
    </tr>
</table>

<script id="typeA" type="text/html">
    <td>typeA template</td>
</script>

<script id="typeB" type="text/html">
    <td>typeB template</td>
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消