Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Total amount in ng-repeat

I need helping with calculating the total amount of tickets.number in this ng-repeat

HTML

<tr ng-repeat="tickets in listTickets | filter: searchText | orderBy: sortorder:reverse">
    <td>{{ tickets.match }}</td>
    <td>{{ tickets.number }}</td>
    <td>{{ tickets.company }}</td>
    <td>{{ tickets.contact }}</td>
    <td>{{ tickets.mail }}</td>
    <td>{{ tickets.phone }}</td>
    <td><button class="btn btn-info" ng-click="edit(tickets._id)">Edit</button> </td>
    <td><button class="btn btn-danger" ng-click="delete(tickets._id)">Radera</button></td>   
</tr>
<tr>
    <td colspan="8"> Total of: {{ totalTickets() }} tickets</td>
</tr>

My controller.js

 $scope.totalTickets = function(){

 }
like image 613
mackeemackee Avatar asked Dec 20 '16 09:12

mackeemackee


People also ask

What is Ng-repeat?

Definition and Usage. The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.

Where is the last element in NG-repeat?

You can use $last variable within ng-repeat directive. Take a look at doc. Where computeCssClass is function of controller which takes sole argument and returns 'last' or null .

How do I get an index in NG-repeat?

Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.

How do I filter in NG-repeat?

The ng-repeat values can be filtered according to the ng-model in AngularJS by using the value of the input field as an expression in a filter. We can set the ng-model directive on an input field to filter ng-repeat values.


2 Answers

You can use ECMAScript 5 Array.prototype.map() and Array.prototype.reduce() to get the total sum.

AngularJS method totalTickets passing the filteredArr array parameter as you are filtering in the view ng-repeat:

$scope.totalTickets = function (filteredArr) {
  return filteredArr
    .map(function (o) {
        return o.number;
    })
    .reduce(function (a, b) {
        return a + b;
    }, 0);
};

AngularJS view:

<tr ng-repeat="tickets in listTickets | filter: searchText as filteredArr | orderBy: sortorder:reverse">
    <td>{{ tickets.match }}</td>
    <td>{{ tickets.number }}</td>
    <td>{{ tickets.company }}</td>
    <td>{{ tickets.contact }}</td>
    <td>{{ tickets.mail }}</td>
    <td>{{ tickets.phone }}</td>
    <td><button class="btn btn-info" ng-click="edit(tickets._id)">Edit</button></td>
    <td><button class="btn btn-danger" ng-click="delete(tickets._id)">Radera</button></td>   
</tr>
<tr>
    <td colspan="8"> Total of: {{ totalTickets(filteredArr) }} tickets</td>
</tr>

Code example:

var listTickets = [{number: 1},{number: 2},{number: 3},{number: 4}],
    total = listTickets.map(o => o.number).reduce((a, b) => a + b, 0);

console.log(total);
like image 184
Yosvel Quintero Arguelles Avatar answered Oct 04 '22 17:10

Yosvel Quintero Arguelles


You can also use filter to calculate a total.

Html

<tr ng-repeat="tickets in listTickets | filter: searchText | orderBy: sortorder:reverse">
    <td>{{ tickets.match }}</td>
    <td>{{ tickets.number }}</td>
    <td>{{ tickets.company }}</td>
    <td>{{ tickets.contact }}</td>
    <td>{{ tickets.mail }}</td>
    <td>{{ tickets.phone }}</td>
    <td><button class="btn btn-info" ng-click="edit(tickets._id)">Edit</button> </td>
    <td><button class="btn btn-danger" ng-click="delete(tickets._id)">Radera</button></td>   
</tr>
<tr>
    <td colspan="8"> Total of: <span data-ng-bind="tickets.total=(listTickets | total:'number')"></span> tickets</td>
</tr>

Controller.js

app.filter('total', function(){
    return function(input, property) {
        var total = 0;
        angular.forEach(input, function(value, key) { total += parseFloat(value[property]) });
        return total;
    }
})
like image 31
Aravinthan K Avatar answered Oct 04 '22 16:10

Aravinthan K