0

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!

Drewness
  • 5,004
  • 4
  • 32
  • 50
Murrax
  • 19
  • 1
  • 6
  • I don't understand what your problem is. Why is the code you provided not accomplishing what you want it to? – JeffryHouser Feb 27 '14 at 18:07
  • Can you create a plunker or jsfiddle and show the problem? – madhured Feb 27 '14 at 18:12
  • The actual code is pasting each new pair of values every time I update the row values (for example, a new numbre in quantity or a new unit). So Im getting [{"unit":"Ton","quantity":"1","sku":"106029072"}, {"unit":"Ton","quantity":"2","sku":"106029072"}] As you can see the SKU (ID) is the same so it should be only one set of values for that product. – Murrax Feb 27 '14 at 18:18
  • I am creating a plunker in the next mins – Murrax Feb 27 '14 at 18:18
  • Unfortunately I can't create a plunker due to certain config in the server. Basically I am trying to update an element in the array with specific values rather than pushing a new pair of values with new values. – Murrax Feb 27 '14 at 18:40
  • I was able to build it :) http://plnkr.co/QTdoggyKhubbB3F3VeO1 – Murrax Feb 27 '14 at 19:09

3 Answers3

0

Check if this work

Controller

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

{{gridOptions.selectedItems}}

I found it here.

Community
  • 1
  • 1
madhured
  • 443
  • 2
  • 8
0

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. :)

Murrax
  • 19
  • 1
  • 6
0

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);
parliament
  • 21,544
  • 38
  • 148
  • 238