suman goud
suman goud

Reputation: 157

How to calculate summary of result in Total result from table row

am able to calculate two inputs and saving result in result input and adding new row , but i want to calculate summary of result values in total Result(this input is outside of tr)

[here is my code][1]



 https://jsbin.com/gewigopevo/edit?html,js,output

Upvotes: 0

Views: 87

Answers (3)

Chakravarty A
Chakravarty A

Reputation: 84

try using watch

Add below lines in addRow1 function:

$scope.$watchCollection("rvm", function() {
    $scope.Total = total();
});

And then create a function total() and get the sum of all the result values.

function total() {
    var totalNumber = 0;
    for(var i=0; i<$scope.rvm.length; i++){
      totalNumber = totalNumber + parseInt($scope.rvm[i].result);
    }
    return totalNumber;
  }

Upvotes: 0

Conrad Lotz
Conrad Lotz

Reputation: 8818

I had a look at you code. I added a counter for total that aggregates the total after every add event occurs there calculating a running total. I pass this to a $scope variable outside of the table.

In the controller

    $scope.outsidetotal = 0; //outside of table scope
    $scope.rvm = [{}];
    var total = 0;
    $scope.total = 0;

  $scope.addRow1 = function (index) {
        $scope.rvm[index].result = $scope.rvm[index].val1 - $scope.rvm[index].val2;
        $scope.rvm.push({})
        $scope.outsidetotal = $scope.total += $scope.rvm[index].result;  
    }

DEMO

Upvotes: 1

Nishant123
Nishant123

Reputation: 1966

Move the

$scope.Total = total

out of the for loop and place

$scope.rvm.push({})

at the end of the addRow1 function

Your function should look like below

$scope.addRow1 = function (index) {
      $scope.rvm[index].result = $scope.rvm[index].val1 - $scope.rvm[index].val2;
        var total = 0;
        for (var i = 0; i < $scope.rvm.length; i++) {
          total += parseInt($scope.rvm[i].result);

        }
      $scope.Total = total;
      $scope.rvm.push({});
    }

FULL EXAMPLE

Upvotes: 2

Related Questions