sum the total in an ng-repeat

Asked

Viewed 90 times

-2

I am a beginner in German and I have the following problem and I don’t know how to solve it!

I have a table and I need to calculate the total waste adding up all prices. Inside ng-repeat has a column where it displays all the values of residuals, but I don’t know the syntax to read these column items and add.

Someone could help me?

$scope.somarValores = function(){
	    $scope.total = $scope.pedidos.residuoModel.preco + $scope.pedidos.residuoModel.preco
	}
<tbody>
                        <tr ng-repeat="pedidosTabela in pedidos | filter: pesquisaPed"ng-click="selecionaPedido(pedidosTabela)">
                          <td>{{pedidosTabela.id}}</td>   
                          <td>{{pedidosTabela.data}}</td>
                          <td>{{pedidosTabela.fornecedorModel.nome}}</td>
                          <td>{{pedidosTabela.categoriaModel.nome}}</td>
                          <td>{{pedidosTabela.residuoModel.nome}}</td>                          
                          <td>{{pedidosTabela.residuoModel.status}}</td>
                          <td>{{pedidosTabela.residuoModel.preco | currency}}</td>
                          <td>{{pedidosTabela.residuoModel.peso}}</td>
                          <td>{{pedidosTabela.residuoModel.situac}}</td>
                          <td><label ="{{pedidosTabela.residuoModel.preco}+{{pedidosTabela.residuoModel.preco}}"/></td>  
                        </tr>               
                      </tbody>               
                      
                      
                      
                      
                      
                      
                      
                    

   <table class="table table-bordered">
                      <thead>
                        <tr>
                          <th></th>
                          <th></th>
                          <th></th>
                          <th></th>
                          <th></th>
                          <th></th>
                          <th></th>
                          <th></th>
                          <th></th>
                          <th>Total: {{total}}</th>
                                          
                        </tr>
                      </thead> 
                      <br><br><br><br>
                  <a class="btn btn-success" ng-click="somarValores()" role="button" placeholder="">Totalizadores  </a>
                  

1 answer

0


You’ll need to do a breakdown to calculate everything.

$scope.somarValores = function(){
    $scope.total = 0;
    for(var i = 0; i < $scope.pedidos.length; i++) {
       $scope.total += $scope.pedidos[i].residuoModel.preco
    }
}
  • or a reduce: https://medium.com/@raullesteves/javascript-understanding-the-reduce-once-for-all-c4cbaa16e380

  • I didn’t know the reduce, I think it’s better than a for.

  • 1

    Matheus Barni It worked for me!!! thank you very much dude you are the dude bro

  • Cezar thanks also for the tip, I will test this way too, a hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.