【问题标题】:Nested Table seems to be causing null childnodes error嵌套表似乎导致空子节点错误
【发布时间】:2014-04-16 19:59:02
【问题描述】:

错误消息: 消息:无法读取 null 的属性“childNodes”

模板:(第 1 部分)

<tr>
    <td><input type="text" data-bind="value: $data.Mark" /></td>
    <td><input type="number" step="1" data-bind="value: $data.Quantity" /></td>
    <td><input type="text" data-bind="value: $data.Description" /></td>
    <td><input type="text" data-bind="value: $data.Type" /></td>
    <td><input type="number" data-bind="value: $data.BaseLength" /></td>
    <td><input type="number" data-bind="value: $data.TCXLNumber" /></td>
    <td><input type="number" data-bind="value: $data.TCXLLength" /></td>
    <td><input type="number" data-bind="value: $data.TCXRNumber" /></td>
    <td><input type="number" data-bind="value: $data.TCXRLength" /></td>
    <td><input type="number" data-bind="value: $data.SeatDepthLeft" /></td>
    <td><input type="number" data-bind="value: $data.SeatDepthRight" /></td>
    <td><input type="number" data-bind="value: $data.BCXCount" /></td>
    <td><input type="number" data-bind="value: $data.Uplift" /></td>
    <td data-bind="foreach: $data.Remarks">
        <input type="text" data-bind="value: $data" />
    </td>
    <td><button class="w100 round alert deleteEntity">Delete</button></td>
</tr>

模板:(第 2 部分)

<tr>
    <table>
        <thead>
            <tr>
                <th colspan="3">LoadInfo</th>
                <th colspan="2">Load1</th>
                <th colspan="2">Load2</th>
                <th rowspan="2"></th>
            </tr>
            <tr>
                <th>Type</th>
                <th>Category</th>
                <th>Position</th>
                <th>Value</th>
                <th>Distance</th>
                <th>Value</th>
                <th>Distance</th>
            </tr>
        </thead>
        <tbody data-bind="template: { name: 'LoadInfoTableTemplate', foreach: $data.LoadInfos }">
        </tbody>
    </table>
</tr>

查看:(原创)

<form>
    <table class="grid">
        <thead data-bind="template: { name: 'JoistsTableHeaderTemplate', with: $data[0] }"></thead>
        <tbody data-bind="template: { name: 'JoistsTableBodyTemplate', foreach: $data }"></tbody>
    </table>
</form>

视图:(修改)

<form>
    <table class="grid">
        <thead data-bind="template: { name: 'JoistsTableHeaderTemplate', with: $data[0] }"></thead>
        <tbody data-bind="template: { name: 'JoistsTableBodyTemplate', foreach: $data }"></tbody>
    </table>
    <table data-bind="foreach: $data" style="display:none;">
        <thead>
            <tr>
                <th colspan="3">LoadInfo</th>
                <th colspan="2">Load1</th>
                <th colspan="2">Load2</th>
                <th rowspan="2"></th>
            </tr>
            <tr>
                <th>Type</th>
                <th>Category</th>
                <th>Position</th>
                <th>Value</th>
                <th>Distance</th>
                <th>Value</th>
                <th>Distance</th>
            </tr>
        </thead>
        <tbody data-bind="template: { name: 'LoadInfoTableTemplate', foreach: $data.LoadInfos }">
        </tbody>
    </table>
</form>

说明:

Template-Part-1 完美运行,并且使用 Knockout 完美加载。如果在 Template-Part-1 工作之外完成 Template-Part-2(参见 View: (Modified))。如果我将 &lt;tr&gt;&lt;/tr&gt; 附加到 Template-Part-1 并将 Template-Part-2 放在 &lt;tr&gt;&lt;/tr&gt; 内,则会给出错误消息。

简单地说,我试图将 Template-Part-2 中的表格嵌套在将附加到 Template-Part-1 的表格行中。这会引发上面的错误消息。但是,如果我将 Template-Part-2 放在它自己的表中,放在表之后(并添加适当的 data-bind 属性),我希望将它嵌套进去,它可以完美地工作。

问题:有谁知道为什么试图嵌套我的表(模板-第 2 部分)会导致错误?我如何解决它?是否有一些淘汰规则不允许不需要绑定的东西被模板化?

【问题讨论】:

  • 你能不能创建一个 jsFiddle 来帮助解释这个问题。

标签: knockout.js nested-table


【解决方案1】:

我认为你的问题将是上下文。您的视图中都有 $data ,但根据上下文, $data 将是不同的对象。当您进入嵌套的foreach 循环时,$data 的值将更改为当前循环的对象。这是我一直在关注的东西,我发现Knockout Binding Context Documentation 很有帮助。

您还需要将&lt;td&gt;&lt;/td&gt; 添加到模板第 2 部分,因为它不是有效的 html。

<tr>
  <td>
    <table>
        <thead>
            <tr>
                <th colspan="3">LoadInfo</th>
                <th colspan="2">Load1</th>
                <th colspan="2">Load2</th>
                <th rowspan="2"></th>
            </tr>
            <tr>
                <th>Type</th>
                <th>Category</th>
                <th>Position</th>
                <th>Value</th>
                <th>Distance</th>
                <th>Value</th>
                <th>Distance</th>
            </tr>
        </thead>
        <tbody data-bind="template: { name: 'LoadInfoTableTemplate', foreach: $data.LoadInfos }">
        </tbody>
    </table>
  </td>
</tr>

【讨论】:

  • facepalm 我几乎可以肯定这是缺少&lt;td&gt;&lt;/td&gt; 的原因,我不敢相信我忽略了这一点。我确定我的上下文是正确的,因为这是我遇到问题时首先检查的内容。检查了10次。我不在工作场所,所以我不能说 100%,但我几乎可以肯定这是问题所在。谢谢。
  • 这绝对是问题所在,仍然不敢相信我忽略了这一点:/ 谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多