Adicionar items Ionic

Asked

Viewed 70 times

0

I need the values to change when clicking the + and -buttons, as in the image.inserir a descrição da imagem aqui

I took an example from Plunker (http://plnkr.co/edit/V1GEsL5IooR8MROuiAoc?p=preview) and I’m trying to adapt to what I need, but I’m not succeeding. Can someone give me a few tips? Follow what I’ve got so far:

    $scope.formData = [];

    $scope.products = [{
        'id': '1',
        name: 'Festa do Cabide'
    }, {
        'id': '2',
        name: 'Balada do Natal'
    }, {
        'id': '3',
        name: 'Almôndegas Radiotivas'
    }];

    $scope.changeQuantity = function (productId, quantity) {
        var newItem = true;
        angular.forEach($scope.formData, function (value, index) {
            console.log(value);
            console.log(index);
            if (value.product_id === productId) {
            //remove if quantity 0 or null
                if (quantity === 0 || quantity === null) {
                    $scope.formData.splice(index, 1);
                }else {
                    $scope.formData[index].quantity = quantity;
                }
                newItem = false;
            }
        });
        if (newItem) {
            $scope.formData.push({product_id: productId, quantity: quantity});
        }
    };
            <pre>{{ formData }}</pre>
            <ul ng-repeat="product in products">
              <li>{{ product.name }}</li>
              <label for="">Quantity: </label>

              <input type="number" ng-model="quantity" placeholder="product_{{product.id}}" value="" style="border: thin solid black">
              <i class="icon ion-ios-minus-outline" mouse-down-up ng-click="clickQuantity(product.id, quantity)"></i>
              <i class="icon ion-ios-plus-outline" mouse-down-up ng-click="changeQuantity(product.id, quantity)"></i>

1 answer

0

Another developer helped me and solved it as follows:

    $scope.ticketPlus = function(ticketIndex, itemIndex) {        
        $scope.tickets[ticketIndex]['items'][itemIndex]['quantity'] ++;
        console.log($scope.tickets);
    }

    $scope.ticketMinus = function(ticketIndex, itemIndex) {
        if(!$scope.tickets[ticketIndex]['items'][itemIndex]['quantity'] == 0) {
            $scope.tickets[ticketIndex]['items'][itemIndex]['quantity'] --;
        }
 
    }
<div class="row text-center">
  <div class="col">
    <i class="icon ion-ios-minus-outline" ng-click="ticketMinus(ticketIndex, itemIndex)"></i>
  </div>
  <div class="col">
    <input type="number" ng-model="item.quantity" placeholder="0">
  <div class="col">
    <i class="icon ion-ios-plus-outline" ng-click="ticketPlus(ticketIndex, itemIndex)"></i>
  </div>
</div>

Browser other questions tagged

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