I have integrated angular ui grid into my project and added a button on the left of each row. Clicking this button should add the respective row to the cart. Since duplicate rows can be added, I am trying to make each row unique by using milliseconds. However, every time I click the cart button on row one, it overwrites the previous entry. I attempted initializing a new array, but that did not solve the issue.
For a working demo, please refer to the JSFIDDLE link provided below.
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 = [];
};
$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();
}]);
Any suggestions or assistance would be highly appreciated.
View the fiddle demo for reference: JSFIDDLE. The array output is logged in the console.
UPDATE
To resolve the issue, I adjusted the function as follows:
$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));
};
Is there a more efficient and maintainable way to achieve this?
EDIT
The final array output in the console (after clicking the cabbage row 4 times) should appear like this:
[{"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"}]
*Make note of the unique ClientLineIds generated