How to get the @index of nested #each in meteor

时光毁灭记忆、已成空白 提交于 2019-12-23 08:36:22

问题


I have a 10x10 array representing 10 rows with 10 cells each. I want to draw a grid and set each cell's background-color according to the value in the array:

a 0 value will be white and a 1 value will be black

I've set up this CSS:

.cell{
  height: 20px;
  width: 20px;
  float: left;
  margin: 0;
  padding: 0;
}

.cell.live{
  background-color: black;
}

.cell.dead {
  background-color: white;
}

I created a helper that will return 'live' or 'dead' according to the value in the array according to 2 arguments: x and y

here's the code:

Template.grid.helpers({
    cellState: function(x, y) {
      if(screenArray[x][y] === 1){
        return 'live';
      }
      else {
        return 'dead';
      }
    }
  });

my problem is that i don't know how to get the @index of both of my #each loops

here's my template, i couldn't find a solution for the ?????

<template name="grid">
  <div class="gridWrapper">
  {{#each row in rows}}
    <div class="row">
      {{#each cell in row}}
        <div class="cell {{cellState @index ?????}}">{{this}}</div>
      {{/each}}
    </div>
  {{/each}}
</div>
</template>

回答1:


You need to use let to capture the index, like:

{{#let rowIndex=@index}}
    {{#each cell in row}}
        <div class="cell {{cellState @index rowIndex}}">{{this}}</div>
    {{/each}}
{{/let}}


来源:https://stackoverflow.com/questions/32728619/how-to-get-the-index-of-nested-each-in-meteor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!