在Meteor中,如何在表格中对某些计算字段进行求和? [英] In Meteor, how can I do a sum of some computed fields in a table?

查看:126
本文介绍了在Meteor中,如何在表格中对某些计算字段进行求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完全改写了这个问题,因为我已经取得了一些进展,我还需要进一步澄清。

I have completely rewritten this question, because I have made some headway, and I also needed to clarify it further.

一般来说 - 我想计算一下表的行和列总计,无需重新计算每个单元格值两次。让我解释一下。

In general - I'd like to calculate row and column totals for a table, without having to recalculate each cell value twice. Let me explain.

我有一个集合,每个文档包含12个值。对于每个doc,我需要对这12个doc值执行两个缓慢昂贵的计算,产生两个结果,然后在表中显示这两个结果。
然后我还需要每行上两个计算结果的总和,以及每列所有计算结果的总和。

I have a collection and each doc contains 12 values. For each doc, I need to perform two slow expensive calculations on these 12 doc values producing two results and then display these two results in a table. Then I also need the sum of the two computed results on each row, and a sum of all the computed results for each column.

页面看起来像这样:

resa    resb    row1sum
resa    resb    row2sum
(etc)
col1sum col2sum

模板的一部分可能如下所示:

And a portion of the template might look like this:

<table>
{{#each docs}}
  <tr><td>{{resa}}</td> <td>{{resb}}</td> <td>{{rowsum}}</td></tr>
{{/each}}
<tr><td>{{colsum1}}</td> <td>{{colsum2}}</td>
</table>

对于每一行,我现在在游标帮助器中使用transform子句来生成结果和rowums 。

For each row, I am now using a transform clause in my cursor helper to generate the results and rowsums.

docs: function() {

return myCollection.find({},{transform: function(doc) {
  var resa = slowcalculation1(doc);
  doc.resa = resa;
  var resb = slowcalculation2(doc);
  doc.resb = resb;
  doc.rowsum = resa+resb;
  return doc;
  }
});

},

这适用于计算两个结果和rowum为每个doc都是被动的。

This works well to calculate the two results and rowsum for each doc and is reactive.

但我现在正试图找到一种有效计算列总和的方法。

But I am now trying to find a way to efficiently calculate the column sums.

我可以使用以下

Tracker:autorun(function()
{
  var col1tot=0;
  var col2tot=0;
  myCollection.find().forEach(function(doc) {
    var resa = slowcalculation1(doc);
    col1tot += resa;
    var resb = slowcalculation2(doc);
    col2tot += resb;
    });
  Session.set('col1',col1tot);
  Session.set('col2',col2tot);
  // then get these coltotals using helpers
});

但有更好的方法吗?就目前而言,为了生成页面,我为每个单元格调用slowcalculations()两次,并且我只想计算每个单元格值一次,因为我的计算是,ahem,slow。 :)

But is there a better way? As it stands, to generate the page, I am calling slowcalculations() twice for each cell, and I like to only calculate each cell value once, since my calculations are, ahem, slow. :)

我可以用某种方式计算列总和,但是在我的光标助手中吗?

Can I instead, somehow, calculate the column sums, but within in my cursor helper?

或者是否有其他方法可以反复采取整个集合并在一次通过中反应性地生成rowums和colsums?

Or is there some other way to reactively take the whole collection and reactively generate both the rowsums and colsums in a single pass?

推荐答案

所以,我相信我有一个答案。它似乎工作,至少。

So, I believe I have an answer. It seems to work, at least.

我修改了传递给模板的模板助手,它现在看起来像这样

I have modified my template helper that is passed to the template and it now looks like this

docs: function() {

var col1sum=0;
var col2sum=0; 

return myCollection.find({},{transform: function(doc) {
  var resa = slowcalculation1(doc);
  doc.resa = resa;
  var resb = slowcalculation2(doc);
  doc.resb = resb;
  doc.rowsum = resa+resb; // the rowsum

  col1sum += resa;
  col2sum += resb;
  Session.set('col1sum',col1sum);
  Session.set('col2sum',col2sum);
  // then get these colsums into template using helpers

  return doc;
  }
});

},

这计算行和,以及列总和,最小化slowcalculations()的次数。

This calcs the row sums, and the column sums, minimizing the number of times slowcalculations() are done.

这篇关于在Meteor中,如何在表格中对某些计算字段进行求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆