Skip to content

Commit

Permalink
Add a quantity dropdown as part of the Add to Cart directive
Browse files Browse the repository at this point in the history
Add quantityMax attribute
Quantity remains when an item is added to cart and can update cart as (requested in snapjay#11)
ngCart.totalItems renamed to getTotalItems
ngCart.totalUniqueItems renamed to getTotalUniqueItems
  • Loading branch information
snapjay committed Jan 21, 2015
1 parent ce3fe2a commit 8bd5ec0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ngCart

Really simple shopping cart for AngularJS
-----------------------------------------------------------------
AngularJS module consists of a set of directives to help you quickly impliment a shopping cart on your AngularJS app
AngularJS module consists of a set of directives to help you quickly implement a shopping cart on your AngularJS app

Documentation
-----
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Shopping Cart built for AngularJS",
"keywords": ["AngularJS", "module", "directive", "shopping cart", "e-commerce", "cart", "basket", "shopping"],
"authors": {"name":"Dan Shreim", "email":"[email protected]>", "homepage":"http://www.snapjay.com/"},
"version": "0.0.2-rc.3",
"version": "0.0.3-rc.1",
"homepage": "http://ngcart.snapjay.com",
"license": "MIT",
"main": "dist/ngCart.js",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ngCart",
"private": false,
"version": "0.0.2-rc.3",
"version": "0.0.3-rc.1",
"description": "Really simple shopping cart for AngularJS",
"license": "MIT",
"tags": "AngularJS, directive, module, shopping cart, shopping basket",
Expand Down
22 changes: 17 additions & 5 deletions src/ngCart.directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ angular.module('ngCart.directives', [])
.controller('CartController',['$scope', 'ngCart', function($scope, ngCart) {

$scope.ngCart = ngCart;


console.log ($scope.quantity)
}])

.directive('ngcartAddtocart', ['ngCart', function(ngCart){
Expand All @@ -18,6 +17,7 @@ angular.module('ngCart.directives', [])
id:'@',
name:'@',
quantity:'@',
quantityMax:'@',
price:'@',
data:'='
},
Expand All @@ -26,9 +26,22 @@ angular.module('ngCart.directives', [])
link:function(scope, element, attrs){
scope.attrs = attrs;
scope.inCart = function(){
return ngCart.getItemById(attrs.id);
return ngCart.getItemById(attrs.id);
};

if (scope.inCart()){
scope.q = ngCart.getItemById(attrs.id).getQuantity();
} else {
scope.q = parseInt(scope.quantity);
}

scope.qtyOpt = [];
for (var i = 1; i <= scope.quantityMax; i++) {
scope.qtyOpt.push(i);
}

}

};
}])

Expand All @@ -52,5 +65,4 @@ angular.module('ngCart.directives', [])
transclude: true,
templateUrl: 'template/ngCart/summary.html'
};
}]);

}]);
8 changes: 4 additions & 4 deletions src/ngCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@ angular.module('ngCart', ['ngCart.directives'])
return this.getCart().items;
};

this.totalItems = function () {
this.getTotalItems = function () {
var count = 0;
var items = this.getItems();
angular.forEach(items, function (item) {
count += item.getQuantity();
count += item.getQuantity();
});
return count;
};

this.totalUniqueItems = function () {
this.getTotalUniqueItems = function () {
return this.getCart().items.length;
};

Expand Down Expand Up @@ -322,4 +322,4 @@ angular.module('ngCart', ['ngCart.directives'])
$scope.ngCart = ngCart;
}])

.value('version', '0.0.1-rc.2');
.value('version', '0.0.3-rc.1');
15 changes: 9 additions & 6 deletions template/ngCart/addtocart.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@

<div ng-hide="attrs.id">
<a class="btn btn-lg btn-primary" ng-disabled="true" ng-transclude></a>

</div>
<div ng-show="attrs.id">
<div ng-hide="inCart()">
<a class="btn btn-lg btn-primary"
ng-click="ngCart.addItem(id, name, price, quantity, data)"
<div>
<span ng-show="quantityMax">
<select name="quantity" id="quantity" ng-model="q"
ng-options=" v for v in qtyOpt"></select>
</span>
<a class="btn btn-sm btn-primary"
ng-click="ngCart.addItem(id, name, price, q, data)"
ng-transclude></a>
</div>
<div class="alert alert-info" ng-show="inCart()">
This item is in your cart
This item is in your cart <a ng-click="ngCart.removeItemById(id)">Remove</a>
</div>

</div>
</div>
4 changes: 2 additions & 2 deletions template/ngCart/summary.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
C25.395,21.21,25.027,21.575,24.576,21.575L24.576,21.575z"/>
</svg>
</div>
<div class="col-md-6">{{ ngCart.totalItems() }}
<ng-pluralize count="ngCart.totalItems()" when="{1: 'item', 'other':'items'}"></ng-pluralize>
<div class="col-md-6">{{ ngCart.getTotalItems() }}
<ng-pluralize count="ngCart.getTotalItems()" when="{1: 'item', 'other':'items'}"></ng-pluralize>
<br />{{ ngCart.totalCost() | currency }}
</div>
</div>

0 comments on commit 8bd5ec0

Please sign in to comment.