【问题标题】:create radiobutton group for each array returned as a document为作为文档返回的每个数组创建单选按钮组
【发布时间】:2016-11-24 02:17:41
【问题描述】:

topics 是一个返回一些文档的集合。每个文档都有几个字段。其中一个字段是字符串数组。 我试图将字符串数组的值表示为返回的每个文档的单选按钮组。我无法给每个组一个唯一的名称。我尝试了 console.log 语句,发现单选按钮组表现良好,但随后由于调用帮助程序的次数超过返回的文档数而变得混乱。

我的html

<template name=topics>
  {{#each topics}}
    <tr>
      <td><input type="checkbox" name="selectone" id="{{uniqueid}}"/></td>
      <td><textarea rows=4 name="topicname" readonly>{{name}} </textarea></td>
      <td><input type="text" value="{{dateCreated}}" name="datecreated" readonly/></td>
      <td>
        {{#each choices}}

          {{>radiogroup}}

        {{/each}}

      </td>
      <td><a href="#" name="edittopic">Edit</a></td>
    </tr>

  {{/each}}
</template>

<template name='radiogroup'>
  <li><input type=radio name="{{radiogroupname}}" />{{this}}</li>
</template>

我的js:

Template.topics.helpers({
  uniqueid: function() {
    Session.set('topicid',this._id);
    return this._id;
  },
  choices:function() {
    return this.choices;
  },
});

Template.radiogroup.helpers({
  radiogroupname: function() {
    return(Session.get('topicid'));
  },
});

【问题讨论】:

    标签: meteor nested each


    【解决方案1】:

    这不是对Session 变量的良好使用,因为您在循环中一遍又一遍地重用它来表示给定的_id。它会不断变化。由于您只需要父对象的_id,因此您可以使用空格键中的相对路径表示法在模板中访问它,如下所示:

    <template name='radiogroup'>
      <li><input type=radio name="{{../_id}}" />{{this}}</li>
    </template>
    

    那么你甚至不需要你的助手。在任何情况下,您都不需要您的 choices 助手,因为 choices 已经是可迭代的。

    【讨论】:

      猜你喜欢
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 2016-04-24
      • 1970-01-01
      • 2019-11-13
      • 1970-01-01
      相关资源
      最近更新 更多