1

Please see what I need to do at the end of this question.

I have the following relationship between Category and Products (1-n).

enter image description here

This is the html table with the inventory

<table>
<thead>
    <tr>
        <th>#</th>
        <th>Category</th>
        <th>Products quantity</th>
    </tr>   
</thead>
<tbody>
    <tr data-ng-repeat="category in categories" data-ng-repeat="getProductsByCategory(category)">
        <td>{{category.name}}</td>
        <td>{{products[category.id] | sumByKey:'quantity'}}</td>
    </tr>
</tbody>
<tfooter>
    <tr>
        <td colspan="2">total</td>
        <td></td>
    </tr>
</footer>   

And this is the result, as you can see I'm using the 'sumByKey' filter in order to get the sum of all products by its category

enter image description here

The respective function to get all products by its category

$scope.products = [];
$scope.getProductsByCategory = function(category){
    $http.get("getProductsByCategory/"+category.id)
    .success(function (data, status, headers, config) {
        $scope.products[category.id] = data;
    })
    .error(function (data, status, header, config) {
        $log.error("Error");
    });
};

And the filter to sum all quantities

app.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;
    };
});

In order to get the total in the inventory table I have been trying to use the same filter (sumByKey) but it doesn't work. Any idea to get the respective result?

2
  • 1
    As it appears you're making an Api call, I would highly recommend doing this in the database, in your stored procedure call or view. Then you can bring it down as part of your response data. Commented Nov 21, 2016 at 1:28
  • 1
    stackoverflow.com/a/40697761/3279156 ----- This can help you Commented Nov 21, 2016 at 1:37

2 Answers 2

2

What you can do is call the filter in the controller and add the results to a total.

$scope.total = 0;

$scope.sum = function(value) {
    var results = // sum returned by your filter;

    // maintains count of all category sums
    $scope.total += results;

    // amount that goes back to the view
    return results; 
}

Then just bind $scope.total to your view.

<td>{{products[category.id] | sum('quantity')}}</td>
Sign up to request clarification or add additional context in comments.

Comments

0

Seems that $scope.products will be [ProductArrayCat1, ProductArrayCat2,ProductArrayCat3]

So all you have to do is throw the 3 arrays to the sumByKey filter respectively.

<tfooter>
<tr>
    <td colspan="2">total</td>
    <td>{{total}}</td>
</tr>

$scope.total=0;
$scope.getProductsByCategory $scope.getProductsByCategory= function(category){
$http.get("getProductsByCategory/"+category.id)
.success(function (data, status, headers, config) {
    $scope.products[category.id] = data;
    $scope.total += $filter('sumByKey')(data,'quantity');
});
};

Comments

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.