0

I am trying to get the sum of each of my columns in a table that is using ng-repeat. I followed an example I found online, but it's loading 0 for the subtotal.

Here's the filter I've got in my controller:

.filter('sumByKey', function() {
    return function(data, key) {
        if (typeof(data) === 'undefined' || typeof(key) === 'undefined') {
            return 0;
        }

        var sum = 0;
        for (var i = data.length - 1; i >= 0; i--) {
            sum += parseInt(data[i][key]);
        }

        return sum;
    };
});

Then my view looks like this:

 <table class="table table-bordered table-hover table-responsive">
    <thead>            
    <tr>
        <th>
            Date Ordered
        </th>
        <th>
            Retail
        </th>
        <th>
            Airtime
        </th>
        <th>
            Spiff
        </th>
        <th>
            Retail
        </th>
        <th>
            Cost
        </th>
        <th>
            Profit
        </th>
    </tr>
    </thead>
    <tbody>
        <tr ng-repeat="sale in vm.sales">
            <td>
                {{ sale.dateOrdered }}
            </td>
            <td>
                {{ sale.firstLoadRetail}}
            </td>
            <td>
                {{ sale.instantProfitAirTime }}
            </td>
            <td>
                {{ sale.instantProfitSpiff }}
            </td>
            <td>
                {{ sale.netRetail }}
            </td>
            <td>
                {{ sale.netCost }}
            </td>
            <td>
                {{ sale.netProfit }}
            </td>
        </tr>
        <tr>
            <th>
                Subtotal
            </th>
            <td >{{ sales | sumByKey : 'firstLoadRetail'  }}</td>
            <td>{{ sales | sumByKey : 'instantProfitAirTime' }}</td>
            <td>{{ sales | sumByKey : 'instantProfitSpiff' }}</td>
            <td>{{ sales | sumByKey : 'netRetail' }}</td>
            <td>{{ sales | sumByKey : 'netCost' }}</td>
            <td>{{ sales | sumByKey : 'netProfit' }}</td>
        </tr>
    </tbody>
</table>

What am I doing wrong? Thanks in advance.

1 Answer 1

1

You forgot the vm. part.

        <td >{{ vm.sales | sumByKey : 'firstLoadRetail'  }}</td>
        <td>{{ vm.sales | sumByKey : 'instantProfitAirTime' }}</td>
        <td>{{ vm.sales | sumByKey : 'instantProfitSpiff' }}</td>
        <td>{{ vm.sales | sumByKey : 'netRetail' }}</td>
        <td>{{ vm.sales | sumByKey : 'netCost' }}</td>
        <td>{{ vm.sales | sumByKey : 'netProfit' }}</td>
Sign up to request clarification or add additional context in comments.

1 Comment

I won't. I hate how it makes you wait 10 minutes, but I'll make sure to do it you. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.