使用角度ui網格我已經在每行的左側包含一個按鈕,點擊時會將該行添加到購物車,重複的行可以添加到購物車,所以我得到毫秒每行都是唯一的,但是第一行中每次點擊我的購物車按鈕都會覆蓋前一行的條目,我試圖初始化一個新的數組,但是這也不起作用。推覆蓋以前的網格rowdata
請參閱下面的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();
}]);
任何幫助將不勝感激。我想寫數組到控制檯。
UPDATE
我把它做這個工作:
$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的
感謝您試用但那不是我是想什麼,我不希望被點擊的行重新添加到同一電網,我只是希望它用我clientLineId並最終打印到不同的網格,現在我只想看到它每次單擊同一行時都會將其打印到具有唯一clientlineID的控制檯。我只是碰巧在這個例子中使用了$ scope.ProductViewModel.Products,但把它推到了像$ scope.ProductViewModel.SelectedProducts之類的東西,這個數組應該只有多行用不同的客戶端ID點擊的行,希望是有道理的。 – Blindsyde
請看我更新的**編輯** – Blindsyde
沒關係。我稍後會刪除答案。 –