Add items to the local database as you select from the list

Asked

Viewed 198 times

1

I want to make a list of products and as the person is clicking on Add (+) or Decrease (-) go showing the quantity in (+) or (-) and when clicking on "Continue Ordering" Register all items listed on a Websql (Localstorage) and follow to next screen.

My question is how to do this, and whenever the amount value is = 0 do not store in the local bank, only if > 0.

And I don’t think it can be on the "Keep Asking" button because I believe there is no way to save a complete array (I’m not sure about this).

Well, my view and this:

My View:

<ion-view view-title="Comprar Bebidas Adicionais" hide-nav-bar="false" >     

        <ion-refresher pulling-text="Puxe para atualizar..."  on-refresh="doRefresh()"></ion-refresher>
        <ion-list class="card list">
            <div class="item item-input">
                <i class="icon ion-search placeholder-icon"></i>
                <input type="search" ng-model="q" placeholder="Procurar" aria-label="filter bebidasextras" />
            </div>
        </ion-list>

        <div class="list" ng-repeat="bebida in bebidasextras" >

          <div class="item item-button-right">
            <b>{{bebida.ad_bebida_titulo}}</b> - R$ {{bebida.ad_bebida_valor}}

          <!-- AQUI SÃO OS BOTÕES DE ADICIONAR E DIMINUIR ITENS -->      
           <div class="button-bar">
                 <button class="button button-clear button-block button-positive"> <!-- adiciona-->
                      <i class="ion-plus"></i>
                 </button>
                 <button class="button button-stable button-clear button-block button-small">{{item.cart_item_qty}}</button> <!-- aqui quero mostrar quantidade, este campo ainda não existe -->
                  <button class="button button-clear button-block button-positive"> <!-- botão de dominuir -->
                      <i class="ion-minus"></i>
                 </button> 
              </div>               
          </div>            

        </div>  

         <button class="button button-block button-assertive" ng-click="updateBebida()"> <!-- Chama para armazenar num WebSQL -->
                Continuar o Pedido
         </button>  

Which presents this result:

View Exemplo

My Controller:

 .controller("exBebidasCtrl", function($scope,$rootScope,$state,$ionicScrollDelegate,$http,$httpParamSerializer,$stateParams,$timeout,$ionicLoading,$ionicPopup,$ionicPopover,$ionicSlideBoxDelegate,$ionicHistory,ionicMaterialInk,ionicMaterialMotion, sharedCartService,CartServiceBebidasEx, $location, DBLocalAdBebidas){

    // VERIFICA SE HÁ BEBIDAS EXTRAS

    $scope.bebidasextras = [];

    var promise = $http.get('http://nhac.esy.es/api_carrinho/lista_bebida_extra.php?json=restaurantes')
      .success(function(retorno) {
        console.log(retorno);
        $scope.bebidasextras = retorno; // não precisa fazer retorno.data

            $scope.user = {
                bebidasextras: [$scope.bebidasextras[1]]
              };
              $scope.checkAll = function() {
                $scope.user.bebidasextras = angular.copy($scope.bebidasextras);
              };
              $scope.uncheckAll = function() {
                $scope.user.bebidasextras = [];
              };
              $scope.checkFirst = function() {
                $scope.user.bebidasextras = [];
                $scope.user.bebidasextras.push($scope.bebidasextras[0]);
              };
              $scope.setToNull = function() {
                $scope.user.bebidasextras = null;
              };


    })
    .error(function(erro) {        
        console.log(erro);
    });    


        // PEGA BEBIDAS SELECIONADAS      
        $scope.updateBebida = function(){
            var p = [];
            for (var i = 0; i < $scope.bebidasextras.length; i++) {
               var item = $scope.bebidasextras[i];
                console.log(item);
               if ( item.selected) {
                p.push(item )  
               }

            }

            $scope.selectedItems = p;
             var item;

    //aqi vai percorrer todo o conteudo da vriavep p, poderia ate dar
    //uma otimaizada e deixar junto com o p.push(item), ai "economiza" um for
    for(var i in p){
        item = p[i];
        console.log(item);
        var titulo = p.filter(function(e) {
            return e.titulo_promo == item.ad_bebida_titulo
            conlole.log(titulo);
        })


        // TESTA SE GUARDA A BEBIDA SELECIONADA

        // INSERINDO DADOS LOCALMENTE
        DBLocal.localdb();

        DBLocal.db.transaction(function(res) {
            res.executeSql("INSERT INTO AD_BEBIDAS (nome_bebida_ad, valor, quantidade) VALUES(?,?,?);", [item.ad_bebida_titulo, item.ad_bebida_valor, CAMPO QUANTIDADE???]);
        });
    }
}
})

My question is how to do this, and whenever the amount value is = 0 do not store in the local bank.

  • Someone here to help me?

No answers

Browser other questions tagged

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