gpt4 book ai didi

javascript - 推送覆盖以前的 ui-grid 行数据

转载 作者:行者123 更新时间:2023-11-28 04:17:36 25 4
gpt4 key购买 nike

使用 Angular 用户界面网格,我在每行的左侧添加了一个按钮,单击该按钮会将行添加到购物车,可以将重复的行添加到购物车,这样我就有几毫秒的时间使每一行都唯一,但是每次单击第一行的购物车按钮覆盖了前一行条目,我尝试初始化一个新数组,但这也不起作用。

请参阅下面的 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();
}]);

任何帮助将不胜感激。

请参阅 fiddle 演示: 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

最佳答案

您的 $scope.ProductViewModel.Products$scope.gridOptions.data 未引用同一个数组。

请参阅固定 fiddle :

$scope.init = function() {
$scope.ProductViewModel = productData;
$scope.ProductViewModel.Products = $filter('orderBy')($scope.ProductViewModel.Products, "ProductCode", false);
$scope.gridOptions.data = $scope.ProductViewModel.Products;
};

fiddle

关于javascript - 推送覆盖以前的 ui-grid 行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45696417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com