简体   繁体   中英

Updating elements in selectedItems array in ng-grid

I am getting the selected rows of my ng-grid and showing certain values in a separated array. My problem is that when I change some values in the selected row, I need to update the element rather than pushing a new pair of values.

$scope.$watch('mySelectedItems', function() {
    $scope.gridOptions.selectedItems.forEach(function(entry) {
        var myEntry = {unit: entry.myUnit, quantity: entry.qty, sku: entry.sku};
        $scope.result1.push(myEntry);
    });
}, true);

Note: The mySelectedItems is just a new array containing the same data in selectedItems[] (ng-grid's default array)

I know it has to do with the push() function, but I can't find a way to update the existing pair of values. Has anyone did something similar in the past?

Thanks!

Check if this work

Controller

$scope.gridOptions = { data: 'myData', selectedItems: [] }; HTML

{{gridOptions.selectedItems}}

I found it here .

I was able to achieve this by modifying the watch function with a new array and adding a second array outside of it. You can check the function in lines 28 to 36 in the js. :)

You have to first get the position of the item you want to update, then replace it using the indexer.

$scope.$watch('mySelectedItems', function() {
   $scope.gridOptions.selectedItems.forEach(function(entry) {
      var myEntry = {unit: entry.myUnit, quantity: entry.qty, sku: entry.sku};
      var position = $scope.result1.map(function(item) { return item.sku; }).indexOf(myEntry.sku);
      if(position != -1) $scope.result1[position] = myEntry;
  });
}, true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM