流星:计算集合中的数据

时间:2016-05-07 10:12:43

标签: meteor

任何人请我帮你。在此之前我提出了问题,但我找到了这个解决方案。我已创建代码来计算集合中的变量。我可以逐个计数而不是按组计算结果。多数民众赞成我的代码,我想算这个,但代码没有给我任何补偿。我想要这样的结果:

  

PTR 1   KOM 4

This my code:
<template name="laporankategori"> 
  <table class="table">
    <thead>
      <tr>
        <th>Jenis Peralatan</th>
        <th>Kuantiti</th>
      </tr>
    </thead>
    <tbody>
      {{#each profil}}
        <tr>
          <td>{{PTR}}</td>
          <td>{{KOM}}</td>  
        </tr>   
      {{/each}} 
    </tbody>
  </table>
</template>

// js

Template.laporankategori.helpers({
  profil: function() {
    return Profil.find({kategori: { $in: ['PTR', 'KOM'] } }).count();
  }
});

2 个答案:

答案 0 :(得分:0)

每当您使用{{#each ...}}进行迭代时,您的助手应该返回游标或数组。你的助手正在返回一个标量:计数。在{{#each }}区块中,您引用了{{PTR}}{{KOM}},但那些不存在。

我怀疑你在这种情况下实际上并没有找到计数,你的助手应该只是:

Template.laporankategori.helpers({
  profil: function() {
    return Profil.find({kategori: { $in: ['PTR', 'KOM'] } });
  }
});

此外,您通常不需要在帮助中计算内容,因为在模板中您可以引用{{profil.count}}并直接获取光标的数量。

答案 1 :(得分:0)

<template name="laporankategori"> 
 <table class="table">
  <thead>
   <tr>
     <th>Jenis Peralatan</th>
     <th>Kuantiti</th>
   </tr>
  </thead>
  <tbody>
   {{#each profil}}
     <tr>
       <td>{{count}}</td>
     </tr>
   {{/each}} 
  </tbody>
 </table>
</template>

// JS

Template.laporankategori.helpers({
 profil: function() {
  var PTR = { 
    count: Profil.find({kategori: { $in: ['PTR'] } }).count()
  };
  var KOM = { 
    count : Profil.find({kategori: { $in: ['KOM'] } }).count()
  };
  var resultArr = [PTR, KOM];
  return resultArr;
 }
});
相关问题