Simple shopping cart, how to add remove items from cart base on the quantity


#1

Hi, i hope someone can help in here…
i like to create a simple shopping cart
this is the list of items i display with ng-repeat=item in items in list item
in this list html i also have a quantity that is zero (0) for each item

$scope.items = [
    {'code': '1111', 'name':'itemA', 'price':1,},
    {'code': '2222', 'name':'itemB', 'price':2,},
    {'code': '3333', 'name':'itemC', 'price':3,}
    ];

this is the list of order created by the user (cart)
i like to add items in to it (add with code, name, price and quantity)
if user select item quantity is more than 0 i like to add it to the order array
and if the user reduce the quantity to zero, i like to remove the item from the array

$scope.order = [];

what function i should add to do it (add/remove)??


#2

anyone can help in here please.?


#3
$scope.addItem = function(){
 var newItem = {
"code":$scope.newItemCode,
"name":$scope.newItemName,
"price":$scope.newPrice
 }
$scope.items.push(newItem);
}


 $scope.remove = function(item) { 
    var index = $scope.items.indexOf(item);
    $scope.items.splice(index, 1);     
   }

#4

thank you !

but how do bring in the quantity thing?
the remove from order array should be when the quantity is 0
and the add item should be when quantity is more than 0

any idea?


#5

It depends on your design. What is your html for quantity. Do you want to dynamically remove the item when user enters qt = 0 or is there a button


#6

yes, this is what i need
what i think to do now is to add to my items array also a quantity:0;
than in this qty i will have a simple ng-click to change the qty

than in the functions of additem and remove i like it to “watch” the qty’s and when an item qty in more than zero than to add it to the order array,…

you have an idea how to do it?