How to sum results from a list and display result in a popup in ionic?

4

I have an actiosheet that opens a popup:

InthepopupIwantthetotalofthevaluesinthelisttobedisplayed.Thatis,Ineedtoadd/accumulatethevaluesinthelistanddisplaythesuminthepopup(inthecodebelowthisvalueisfixed).

Couldyouhelpme?

angular.module('starter.controller.listaItens', [])

.controller('ListaItensCtrl', function($scope, $state, $ionicPopup, $ionicActionSheet, ListaItens, Adicionar) {
   var btDetalhes = "Mais Detalhes";

   $scope.edit = ListaItens.Edit();
   $scope.title = ListaItens.NameList();
   $scope.hide = ListaItens.HideItens();
   $scope.data = { quantidade: ""};
   $scope.itens = ListaItens.ItemsList();

   $scope.buttons = [{type: 'normal', row: 1, method: '',
                     propertiers: {name: "Adicionar",  icon: "ion-plus",     ref: "#/app/adicionar/listaItens"}},

                     {type: 'option', row: 1,
                     propertiers: {name: "Opções",  icon: "ion-ios-gear-outline",       ref: "Opcoes()"}},

                     {type: 'flip', row: 1, method: 'SelectEditar()',
                     propertiers: [{name: "Editar", icon: "ion-ios-compose-outline",    ref: "#/app/"},
                                   {name: "Salvar", icon: "ion-ios-checkmark-empty",    ref: "#/app/"}]}];

   $scope.delBtn = function(item) {
      ListaItens.RemoveItem(item);
   };

   $scope.SelectEditar = function() {
      $scope.edit = ListaItens.Edit();
   };
  

   // POPUP
   $scope.qntBtn = function(item) {
      var myPopup = $ionicPopup.show({
         template: '<input type="text" ng-model="data.quantidade">',
         title: 'NomeProduto',
         subTitle: 'Altere a quantidade do produto',
         scope: $scope,
         buttons: [
            { text: 'Cancel' },
            {
               text: '<b>Alterar</b>',
               type: 'button-positive',
               onTap: function(e) {
                  intQuantidade = (this.scope.$parent.data.quantidade/1);
                  console.log(this.scope.$parent.data.quantidade);
                  this.scope.$parent.data.quantidade = "";
                  if (!intQuantidade) {
                     e.preventDefault();
                  } else {
                     return intQuantidade;
                  }
               }
            },
         ]
      });
      myPopup.then(function(res) {
         if(res) {
            ListaItens.UpdateItem(item, res);
         }
      });
   };

   //ACTIONSHEET
   $scope.Opcoes = function() {
      $ionicActionSheet.show({
         titleText: 'Opções',
         cancelText: 'Cancel',
         cancel: function() {
            console.log('CANCELLED');
         },
         buttons: [
            { text: btDetalhes },  //INDEX 0
            { text: 'Totalizar' }, //INDEX 1
         ],
         buttonClicked: function(index) {
            switch(index) {
               case 0:
                  btDetalhes = "Menos Detalhes"
                  $scope.hide = ListaItens.HideItens();
                  console.log($scope.hide);
               break;
               case 1:
                  //POPUP
                  var alertPopup = $ionicPopup.alert({
                     title: 'Total da Compra',
                     template: 'R$ 89,54'
                  });
                  alertPopup.then(function(res) {
                     console.log('Totalizado');
                  });
               break;
            }
            return true;
         },
         destructiveText: 'Limpar Lista',
         destructiveButtonClicked: function() {
            ListaItens.ClearList();
            console.log('LIMPO');
            return true;
         }
      });
   };
});
<ion-content>
   <ion-list class="item-nopadding">
      <ion-item class="item-text-wrap" ng-repeat="item in itens">
         <div class="row styleListItens" ng-disabled="edit" style="height: 57px;">

            <img ng-if="item.icon" style="width: 50px;align-self: center;margin: 0;padding-left: 5px;" src="{{item.icon}}">

            <div ng-if="!item.icon" class="col col-20 text-right" style="align-self: center;"><span><b>{{item.Quantidade}}</b>{{item.Unidade}}</span></div>

            <div class="col col-70" style="align-self: center;"><span style="font-weight: 500;line-height: initial">{{item.DescCupom}}</span><span ng-if="hide" style="display:block;display: block;font-size: 0.85em;font-weight: 300;line-height: initial;">{{item.CodBarras}}</span></div>

            <div class="col col-10 text-right" style="align-self: center;"><i ng-if="edit==true" style="padding-left:5px;color:silver"  class="ion-chevron-right"></i></div>

            <ion-option-button style="flex-direction: column;line-height: initial;padding: 5px !important;" ng-if="edit" class="button-positive" id="styleDeletBtn" ng-click="qntBtn(item)"><i style="flex: 1;" class="icon ion-plus-circled"></i><span style="display: block; flex: 1; font-size: 0.6em;">ADD</span></ion-option-button>

            <ion-option-button style="flex-direction: column;line-height: initial;padding: 5px !important;" ng-if="edit" class="button-assertive" id="styleDeletBtn" ng-click="delBtn(item)"><i style="flex: 1;" class="icon ion-trash-a"></i><span style="display: block; flex: 1; font-size: 0.6em;">EXCLUIR</span></ion-option-button>
         </div>
      </ion-item>
     </ion-list>
  </ion-content>
    
asked by anonymous 16.08.2016 / 16:10

1 answer

1

Considering that your $scope.itens is a vector, add this line to the qntBtn method, before creating the popup.

  $scope.data.quantidade = $scope.itens.reduce(function(total, item){
    total += item.valor
  }, 0)
    
02.09.2016 / 02:35