Skip to content

Instantly share code, notes, and snippets.

Created October 29, 2014 15:18
Show Gist options
  • Save anonymous/03ce1f08fc38d12af378 to your computer and use it in GitHub Desktop.
Save anonymous/03ce1f08fc38d12af378 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html ng-app>
<head>
<script src="http://code.jquery.com/jquery.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
<style id="jsbin-css">
body { background-color: #f7f7f7; }
.wrapper {
margin: 16px 24px;
padding: 12px 16px;
background-color: #fcfcfc;
border-radius: 2px;
}
</style>
</head>
<body ng-controller="MainController">
<div class="wrapper">
<table class="table">
<tbody>
<tr>
<td>Name: </td>
<td><input type="text" ng-model="PName"></td>
</tr>
<tr>
<td>Price: </td>
<td><input type="number" ng-model="Price"></td>
</tr>
<tr>
<td>Count: </td>
<td><input type="number" ng-model="Count"></td>
</tr>
<tr>
<td colspan="2">
<button ng-click="buy(PName, Price, Count)" class="btn btn-primary">Buy</button>
</td>
</tr>
</tbody>
</table>
<div class="checkbox">
<label for="debug">
<input type="checkbox" id="debug" ng-model="EnableDebug">
Enable Debug
</label>
</div>
<div class="result">
<p>"{{PName}}" ({{Count|number}}) with price ${{Price|number}} at total {{subtotal(Price, Count)|number}}</p>
</div>
<table class="table">
<thead>
<tr>
<th>No#</th>
<th>Name</th>
<th>Count</th>
<th>Price</th>
<th>Total</th>
<th>Del</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in cart | orderBy:Order">
<td>{{$index + 1}}</td>
<td>{{item.PName}}</td>
<td>
<a ng-click="modify($index, -1)">-</a>
{{item.Count}}
<a ng-click="modify($index, +1)">+</a>
</td>
<td>{{item.Price}}</td>
<td>{{subtotal(item.Price, item.Count)}}</td>
<td><button ng-click="del($index)" class="btn btn-danger">Del</button></td>
</tr>
<tr>
<td>#</td>
<td></td>
<td></td>
<td></td>
<td>{{Total}}</td>
<td></td>
</tr>
</tbody>
</table>
<pre ng-show="EnableDebug">{{cart|json}}</pre>
</div>
<script id="jsbin-javascript">
var MainController = function ($scope) {
$scope.cart = [];
$scope.PName = 'T-Shirt';
$scope.Price = 1000;
$scope.Count = 5;
$scope.Total = 0;
$scope.Order = 'Total';
$scope.subtotal = function (price, count) {
var result = price * count;
if (count > 9) {
result *= 0.9;
}
return result;
};
$scope.del = function (index) {
$scope.cart.splice(index, 1);
$scope.sum_up();
};
$scope.buy = function (name, price, count) {
$scope.cart.push({
PName: name,
Price: price,
Count: count
});
$scope.sum_up();
};
$scope.modify = function(index, count) {
var obj = $scope.cart[index];
obj.Count += count;
};
$scope.sum_up = function () {
var sum = 0;
for (var i = 0; i < $scope.cart.length; i++) {
var obj = $scope.cart[i];
sum += $scope.subtotal(obj.Price, obj.Count);
}
$scope.Total = sum;
};
$scope.cart.push({
PName: "Hat",
Price: 500,
Count: 6
});
$scope.cart.push({
PName: "Wallet",
Price: 700,
Count: 4
});
$scope.sum_up();
};
</script>
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html>
<html ng-app>
<head>
<script src="//code.jquery.com/jquery.min.js"><\/script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"><\/script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"><\/script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body ng-controller="MainController">
<div class="wrapper">
<table class="table">
<tbody>
<tr>
<td>Name: </td>
<td><input type="text" ng-model="PName"></td>
</tr>
<tr>
<td>Price: </td>
<td><input type="number" ng-model="Price"></td>
</tr>
<tr>
<td>Count: </td>
<td><input type="number" ng-model="Count"></td>
</tr>
<tr>
<td colspan="2">
<button ng-click="buy(PName, Price, Count)" class="btn btn-primary">Buy</button>
</td>
</tr>
</tbody>
</table>
<div class="checkbox">
<label for="debug">
<input type="checkbox" id="debug" ng-model="EnableDebug">
Enable Debug
</label>
</div>
<div class="result">
<p>"{{PName}}" ({{Count|number}}) with price ${{Price|number}} at total {{subtotal(Price, Count)|number}}</p>
</div>
<table class="table">
<thead>
<tr>
<th>No#</th>
<th>Name</th>
<th>Count</th>
<th>Price</th>
<th>Total</th>
<th>Del</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in cart | orderBy:Order">
<td>{{$index + 1}}</td>
<td>{{item.PName}}</td>
<td>
<a ng-click="modify($index, -1)">-</a>
{{item.Count}}
<a ng-click="modify($index, +1)">+</a>
</td>
<td>{{item.Price}}</td>
<td>{{subtotal(item.Price, item.Count)}}</td>
<td><button ng-click="del($index)" class="btn btn-danger">Del</button></td>
</tr>
<tr>
<td>#</td>
<td></td>
<td></td>
<td></td>
<td>{{Total}}</td>
<td></td>
</tr>
</tbody>
</table>
<pre ng-show="EnableDebug">{{cart|json}}</pre>
</div>
</body>
</html></script>
<script id="jsbin-source-css" type="text/css">body { background-color: #f7f7f7; }
.wrapper {
margin: 16px 24px;
padding: 12px 16px;
background-color: #fcfcfc;
border-radius: 2px;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">var MainController = function ($scope) {
$scope.cart = [];
$scope.PName = 'T-Shirt';
$scope.Price = 1000;
$scope.Count = 5;
$scope.Total = 0;
$scope.Order = 'Total';
$scope.subtotal = function (price, count) {
var result = price * count;
if (count > 9) {
result *= 0.9;
}
return result;
};
$scope.del = function (index) {
$scope.cart.splice(index, 1);
$scope.sum_up();
};
$scope.buy = function (name, price, count) {
$scope.cart.push({
PName: name,
Price: price,
Count: count
});
$scope.sum_up();
};
$scope.modify = function(index, count) {
var obj = $scope.cart[index];
obj.Count += count;
};
$scope.sum_up = function () {
var sum = 0;
for (var i = 0; i < $scope.cart.length; i++) {
var obj = $scope.cart[i];
sum += $scope.subtotal(obj.Price, obj.Count);
}
$scope.Total = sum;
};
$scope.cart.push({
PName: "Hat",
Price: 500,
Count: 6
});
$scope.cart.push({
PName: "Wallet",
Price: 700,
Count: 4
});
$scope.sum_up();
};</script></body>
</html>
body { background-color: #f7f7f7; }
.wrapper {
margin: 16px 24px;
padding: 12px 16px;
background-color: #fcfcfc;
border-radius: 2px;
}
var MainController = function ($scope) {
$scope.cart = [];
$scope.PName = 'T-Shirt';
$scope.Price = 1000;
$scope.Count = 5;
$scope.Total = 0;
$scope.Order = 'Total';
$scope.subtotal = function (price, count) {
var result = price * count;
if (count > 9) {
result *= 0.9;
}
return result;
};
$scope.del = function (index) {
$scope.cart.splice(index, 1);
$scope.sum_up();
};
$scope.buy = function (name, price, count) {
$scope.cart.push({
PName: name,
Price: price,
Count: count
});
$scope.sum_up();
};
$scope.modify = function(index, count) {
var obj = $scope.cart[index];
obj.Count += count;
};
$scope.sum_up = function () {
var sum = 0;
for (var i = 0; i < $scope.cart.length; i++) {
var obj = $scope.cart[i];
sum += $scope.subtotal(obj.Price, obj.Count);
}
$scope.Total = sum;
};
$scope.cart.push({
PName: "Hat",
Price: 500,
Count: 6
});
$scope.cart.push({
PName: "Wallet",
Price: 700,
Count: 4
});
$scope.sum_up();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment