How to add results from a list and display result in an Ionic popup?

Asked

Viewed 790 times

4

I have an actiosheet that opens a popup:

inserir a descrição da imagem aqui

In the popup I want the total values of the list to be displayed. That is, I need to add / accumulate the values of the list and display the sum in the pop-up (in the code below this value is fixed).

You could help me?

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>

  • Hello Henrique, welcome to Stack Overflow. Despite the code examples, it’s not very clear to me where the problem is. Are you having trouble displaying the value in the pop-up? On changing the field in the template? On totaling the list value? If you edit the question including details about your difficulty, about the error obtained and what you are looking for I have with me that the community will probably reverse these votes to close the question and find a way to help you.

  • @Anthonyaccioly thank you so much for the tip. I edited and put right what I need, I hope you did right! Can you help me? It’s a very simple question but I’m starting my walk now.

  • Hi Henry, baby? Improved enough :). In practical terms what does it mean? You want where it is today fixed R$ 89,54 the sum of all item.Quantidade * item.Unidade? Try to describe what you need (maybe even with a print screen to make it clearer).

  • 1

    All good @Anthonyaccioly good morning! I put up a print now. Exactly what I want to appear.

  • 2

    I don’t know if you are really a popup or a dialog, but maybe this helps you, you can use the material angular to do this, I already used to take the value that was typed in a field and add with other 2, to then display in a dialog. https://material.angularjs.org/latest/demo/dialog

1 answer

1

whereas his $scope.itens is a vector, add this line to the method qntBtn, before creating the popup.

  $scope.data.quantidade = $scope.itens.reduce(function(total, item){
    total += item.valor
  }, 0)

Browser other questions tagged

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