Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am building a Meteor app where I have to display a variable length table of calculation results. The calculations are done in Meteor and displayed in cells of the rows - each cell in the table is a numeric result based on a complex calculation. Finally I want to display a total calculation for each row.

calcresult1 calcresult2 row1sum
calcresult3 calcresult4 row2sum
:
(variable number of rows)

How can I efficiently calculate the row sums reactively from the calcresults on each row?

Can I setup a single session variable, sum to it when rendering the cells in the row, and then flush the total as each rowsum is to be rendered?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
119 views
Welcome To Ask or Share your Answers For Others

1 Answer

If the rows have the same number of cells each time, you could pass the results from each cell helper to a final helper.

<template name="calcTable">
    <table>
        {{#each calcRow}}
            <tr>
                <td>{{calcresult1}}</td>
                <td>{{calcresult2}}</td>
                <td>{{rowsum calcresult1 calcresult2}}</td>
            </tr>
        {{/each}}
    </table>
</template name="calcTable">

-

Template.calcTable.helpers({
    calcresult1: function() {
        return result;
    },

    calcresult2: function() {
        return result;
    },

    rowsum: function(calcresult1, calcresult2) {
        return calcresult1 + calcresult2;
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...