简体   繁体   English

流星-从父模板到子模板之间传递数据

[英]METEOR - Passing data from between parent template to a child template

I'm trying to create a two-way table with two collections in meteorJS where i could pass the name of one collection from the parent template to child template. 我正在尝试使用meteorJS中的两个集合创建一个双向表,在其中我可以将一个集合的名称从父模板传递给子模板。

Item is the x-axis and Item2 is the y-axis. Item是x轴,Item2是y轴。

My Collections : 我的收藏:

db.Item.insert({
    "name" : "hello",
    "param1" : "ok",
    "param2" : "ok",
    "parma3" : "na"
});

db.Item.insert({
    "name" : "bonjour",
    "param1" : "na",
    "param2" : "ok",
    "parma3" : "ok"
});

db.Item2.insert({
    "id" : "01",
    "nameIt2" : "nameS"
});

db.Item2.insert({
    "id" : "02",
    "nameIt2" : "name"
});

My Code HTML : 我的代码HTML:

 <template name="home">
   <table class="table maintable">
      <!--Entête du tableau (première ligne)-->
      <tbody>
        {{#each event}}
          {{> eventItem}}
        {{/each}}
      </tbody>
   </table>
</template>


<template name="eventItem">
  <tr>
    {{#each event2}}
      {{> eventItem2}}
    {{/each}}
  </tr>
</template>

<template name="eventItem2">
  <td>
    {{nameItem}}
  </td>
</template>

My JS code : 我的JS代码:

Template.home.helpers({
event() {
    return Item.find();
  },
});

Template.eventItem.helpers({
event2() {
    return Item2.find();
  },
});

Template.eventItem.helpers({
event2() {
    return Item2.find();
  },
});

Template.eventItem2.helpers({
nameItem() {
    return **Name field of my collection Item**
  },
});

Is there a way to pass the Name of each Item from the parent template eventItem to child template eventItem2 ? 有没有办法将每个项目名称从父模板eventItem传递到子模板eventItem2

Yes, you could use Template.parentData to access the data of parent template: 是的,您可以使用Template.parentData访问父模板的数据:

Template.eventItem2.helpers({
  nameItem() {
    return Template.parentData().name;
  },
});

You just need to set value in you child template in order pass the data. 您只需要在子模板中设置值即可传递数据。

You can pass data like this: 您可以像这样传递数据:

<template name="home">
   <table class="table maintable">
      <!--Entête du tableau (première ligne)-->
      <tbody>
        {{#each event}}
          {{> eventItem childEvent=this}}
        {{/each}}
      </tbody>
   </table>
</template>


<template name="eventItem">
  <tr>
    {{#each childEvent}}
      {{> eventItem2 nameItem=this}}
    {{/each}}
  </tr>
</template>

<template name="eventItem2">
  <td>
    {{nameItem}}
  </td>
</template>

*Note: This represent the current value in #each *注意:这代表#each中的当前值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM