AngularJS:防止再次添加数组中已有的项
AngularJS: Prevent item already in array from being added again
我有一个Angular SPA,它有一个购物车(数组),用户可以向其中添加项目。我想防止用户向购物车中添加任何特定项目两次。
function CartForm($scope) {
$scope.products = [{
"description": "BB-8 Droid",
"qty": "1",
"cost": "99"
}, {
"description": "C-3PO Droid",
"qty": "1",
"cost": "499"
}, {
"description": "R2-D2 Astromech Droid",
"qty": "1",
"cost": "899"
}, {
"description": "R5-D4 Astromech Droid",
"qty": "1",
"cost": "899"
}, {
"description": "IG-88 Bounty Hunter Droid",
"qty": "1",
"cost": "899"
}];
$scope.invoice = {
items: []
};
$scope.addItem = function(product) {
$scope.invoice.items.push(product);
},
$scope.removeItem = function(index) {
$scope.invoice.items.splice(index, 1);
},
$scope.total = function() {
var total = 0;
angular.forEach($scope.invoice.items, function(item) {
total += item.qty * item.cost;
})
return total;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<h2>Shopping Cart Example</h2>
<div ng:controller="CartForm">
<table class="table">
<thead>
<th>Description</th>
<th>Qty</th>
<th colspan="2">Price</th>
</thead>
<tr ng-repeat="product in products">
<td>{{product.description}}</td>
<td>{{product.qty}}</td>
<td>{{product.cost | currency }}</td>
<td>
<button class="btn btn-danger" ng-click="addItem(product)">ADD TO CART</button>
</tr>
</table>
<table class="table">
<tr>
<th>Description</th>
<th>Qty</th>
<th>Cost</th>
<th>Total</th>
<th></th>
</tr>
<tr ng:repeat="item in invoice.items">
<td>
<input type="text" ng:model="item.description" class="input-small">
</td>
<td>
<input type="number" ng:model="item.qty" ng:required class="input-mini">
</td>
<td>
<input type="number" ng:model="item.cost" ng:required class="input-mini">
</td>
<td>{{item.qty * item.cost | currency}}</td>
<td>
[<a href ng:click="removeItem($index)">X</a>]
</td>
</tr>
<tr>
<td></td>
<td></td>
<td>Total:</td>
<td>{{total() | currency}}</td>
</tr>
</table>
</div>
请参阅此处的工作JSFiddle:http://jsfiddle.net/tedleeatlanta/22591h2y/15/
您可以在AddItem
中添加一些逻辑来处理所有这些。
这不是最优雅的方式,但会让你朝着正确的方向前进——这样的方法效果很好:
$scope.addItem = function(product) {
var exist = false;
for(var i = 0; i < $scope.invoice.items.length;i++){
if ($scope.invoice.items[i].description==product.description)
{
// having to use parseInt here because your Qty isn't a number...naughty naughty
$scope.invoice.items[i].qty = parseInt($scope.invoice.items[i].qty)+1;
exist = true;
}
}
if (!exist)
$scope.invoice.items.push(product);
},
如果已经存在,它会增加数量,如果没有,则会增加数量
看到它在这里运行http://jsfiddle.net/22591h2y/16/
或者,对于不需要解析Int的东西,将对象数量更改为int,而不是字符串。
查看此更新http://jsfiddle.net/22591h2y/17/
相关文章:
- 向多维数组添加值和从多维数组请求值
- 如何将JS数组添加到Meteor自动表单条目中
- 在不使用循环的情况下,从一个数据库字符串值向javascript数组添加多个对象
- 将行从数组添加到谷歌图表
- 从不同的数组添加重复的图块 PIXI.js
- 如何在 Javascript 中向数组添加其他对象
- 将javascript数组添加到多维数组中
- Coderbyte数组添加解决方案--For循环与For循环
- Knockout.js向数组添加新项
- Highcharts将变量数组添加到序列中
- 向javascript对象数组添加值
- 我可以用Javascript向数组添加函数吗
- 将数组添加到另一个数组
- 你能给被劫持的JavaScript数组添加一个函数吗
- 我可以向数组添加对象键吗
- 如何向JSON数组添加新属性
- 通过将$resource查询数组添加到AngularJS中另一个数组的底部来进行分页加载
- 使用名称/值从 json 数组添加项
- Javascript数组添加 - 所有组合
- 从 json 文件向 javascript 数组添加值