meteor:使用嵌套的每个循环

时间:2015-08-19 22:23:02

标签: javascript meteor

我在使用流星中的每个循环时遇到一些问题。因为我的DB有以下SimpleSchema ...

collection.js

dGroup = new SimpleSchema({
    title:      { type: String, optional: true },
    element:    { type: [more], optional: true }
});

more = new SimpleSchema({
    description: { type: String, optional: true },
    anything:   { type: String, optional: true }
});

MongoDB.attachSchema(new SimpleSchema({
    mainTitle:  { type: String },
    slug:       { type: String, unique: true },
    language:   { type: String, defaultValue: "en" },
    group:      { type: [dGroup], optional: true },
}));

router.js

data: function () {
    return { 
         result: MongoDB.findOne({
            _id: this.params._id
        }) 
    };
}

...我想在每个循环中使用每个循环:

{{#each result.group}}
<table>
    <caption>{{mainTitle}}</caption>
    <tbody>
        {{#each element}}
            <tr>
                <td><input type="text" value="{{description}}"></td>
                <td><input type="text" value="{{anything}}"></td>
            </tr>
        {{/each}}
    </tbody>
</table>
{{/each}}

更新

我的问题是如何使用父元素中的元素。在这个例子中,我无法访问{{mainTitle}},因为它是while循环的父元素。

1 个答案:

答案 0 :(得分:3)

您可以使用../访问父数据上下文,例如:

{{#each result.group}}
  <table>
    <caption>{{mainTitle}}</caption>
    <tbody>
      {{#each element}}
        <tr>
          <th> {{../mainTitle}} </th>
        </tr>
        <tr>
          <td><input type="text" value="{{description}}"></td>
          <td><input type="text" value="{{anything}}"></td>
        </tr>
      {{/each}}
    </tbody>
  </table>
{{/each}}
相关问题