使用角度ui网格我在每行左侧都包含一个按钮,单击该按钮会将行添加到购物车中,可以将重复的行添加到购物车中,这样我就可以获得毫秒来使每行唯一,但每次点击我在第一行的购物车按钮覆盖了上一行条目,我尝试初始化一个新数组但是它也没有工作。
请参阅下面的JSFIDDLE了解工作演示
JS:
var myApp = angular.module('myApp', ['ui.grid']);
myApp.controller('myCtrl', ['$scope', '$filter', function($scope, $filter) {
var productData = {
"Products": [{
"TagLevel1": null,
"ProductName": "Carrot",
"ProductCode": "car-001",
"IsSelected": null,
"ClientLineId": null,
"Active": true
}, {
"TagLevel1": null,
"ProductName": "Cucumber",
"ProductCode": "cuc-001",
"IsSelected": null,
"ClientLineId": null,
"Active": true
}, {
"TagLevel1": null,
"ProductName": "Cabbage",
"ProductCode": "cab-001",
"IsSelected": null,
"ClientLineId": null,
"Active": true
}, {
"TagLevel1": null,
"ProductName": "Lettuce",
"ProductCode": "let-001",
"IsSelected": null,
"ClientLineId": null,
"Active": true
}]
};
var actionTemplate = '<div style="text-align:center;vertical-align:middle;line-height:35px;"><button id="{{row.entity.ProductId + \'_AddToCartBtn\'}}" title="Add to cart" ng-class="(row.entity.IsSelected == true ? \'iconBtnClicked iconBtnClick fa fa-cart-plus fa-2x\':\'iconBtn iconBtnClick fa fa-shopping-cart fa-2x\')" ng-click="grid.appScope.AddToCart(row.entity, $event)" /></div>';
$scope.gridOptions = {
rowHeight: 35,
showGridFooter: false,
enableFiltering: false,
enableColumnMenus: false,
columnDefs: [
{ field: 'IsSelected', name: 'IsSelected', width: '85', displayName: 'ACTION', enableSorting: false, enableFiltering:false, cellTemplate: actionTemplate },
{ field: 'ProductCode', name: 'ProductCode', width: '200', displayName: 'PRODUCT CODE' },
{ field: 'ProductName', name: 'ProductName', width: '800', displayName: 'PRODUCT NAME' },
],
onRegisterApi: function(gridApi) {
$scope.gridApi = gridApi;
}
};
$scope.init = function() {
$scope.ProductViewModel = productData;
$scope.gridOptions.data = $filter('orderBy')($scope.ProductViewModel.Products, "ProductCode", false);
$scope.ProductViewModel.Products = [];
};
//button on GUI represented with a cart icon on each row, when clicked, add the product to the cart
$scope.AddToCart = function(rowData, event) {
rowData.IsSelected = true
rowData.ClientLineId = new Date().getUTCMilliseconds();
console.log('ClientIdUsed:' + rowData.ClientLineId);
$scope.ProductViewModel.Products.push(rowData);
console.log('PVM.Products: ' + JSON.stringify($scope.ProductViewModel.Products));
};
$scope.init();
}]);
非常感谢任何帮助。
请参阅小提琴演示: JSFIDDLE我正在将数组写入控制台。
更新
我这样做是为了工作:
$scope.AddToCart = function(rowData, event) {
$scope.ProductViewModel.Products.push({"TagLevel1":null,"ProductName":rowData.ProductName, "ProductCode":rowData.ProductCode, "IsSelected": true, "ClientLineId": new Date().getUTCMilliseconds(), "Active":rowData.Active, "$$hashKey":rowData.$$hashKey });
console.log('PVM.Products: ' + JSON.stringify($scope.ProductViewModel.Products));
};
是否有更清洁(更易于维护)的方式?
修改
控制台中的最后一个数组(如果我点击卷心菜行4次)应如下所示:
[{"TagLevel1":null,"ProductName":"Cabbage","ProductCode":"cab-001","IsSelected":true,"ClientLineId":142,"Active":true,"$$hashKey":"uiGrid-0007"},{"TagLevel1":null,"ProductName":"Cabbage","ProductCode":"cab-001","IsSelected":true,"ClientLineId":285,"Active":true,"$$hashKey":"uiGrid-0007"},{"TagLevel1":null,"ProductName":"Cabbage","ProductCode":"cab-001","IsSelected":true,"ClientLineId":376,"Active":true,"$$hashKey":"uiGrid-0007"},{"TagLevel1":null,"ProductName":"Cabbage","ProductCode":"cab-001","IsSelected":true,"ClientLineId":962,"Active":true,"$$hashKey":"uiGrid-0007"}]
*请注意唯一的ClientLineId
答案 0 :(得分:0)
$ scope.ProductViewModel.Products 和 $ scope.gridOptions.data 不会引用同一个数组。
请看固定小提琴:
$scope.init = function() {
$scope.ProductViewModel = productData;
$scope.ProductViewModel.Products = $filter('orderBy')($scope.ProductViewModel.Products, "ProductCode", false);
$scope.gridOptions.data = $scope.ProductViewModel.Products;
};
答案 1 :(得分:0)
我通过创建一个新对象并在每次点击时将键和值循环到新对象中来修复它。
$scope.AddToCart = function(rowData, event) {
rowData.IsSelected = true;
var newObj = {};
$.each(Object.keys(rowData), function(i,v){
if(v === "ClientLineId")
newObj[v] = new Date().getUTCMilliseconds();
else if(v === "IsSelected")
newObj[v] = true;
else
newObj[v] = rowData[v];
});
$scope.ProductViewModel.Products.push(newObj);
console.log(JSON.stringify($scope.ProductViewModel.Products));
};
请参阅FIXED FIDDLE