1

I have an ng-grid that I am trying to fill with some search results. The queries work as long as the result has a different amount of rows... If a query has the same amount of rows as the previous query... the result will not show.

    // after a rest call 
    // we have the result
    // #1 Clean gridData and local array
      $scope.gridData.length = 0;    
      _labels.length = 0;

    // #2 feed the grid 
    // these two functions add rows to the grid + data to the rows (and my local array) 
            addLabels(result.Labels);
            addPhrases(result.Phrases);


    // Second version attempting to call $scope.$apply
    // does not work either

      $scope.gridData.length = 0;
        if (!$scope.$$phase) {
            $scope.$apply();
        }

        _labels.length = 0;
        addLabels(result.Labels);
        addPhrases(result.Phrases);
        if (!$scope.$$phase) {
            $scope.$apply();
        }

here is a plunker that demonstrates this: http://plnkr.co/edit/gSFtuL?p=preview

note that if you push one more (or one less) item, the grid refreshes

2 Answers2

0

It seems that ngGrid specifically watches the array.length before it passes in the options.data. Copied from here:

$scope.$parent.$watch(options.data, dataWatcher);
$scope.$parent.$watch(options.data + '.length', function() {
    dataWatcher($scope.$eval(options.data));
});

I can't explain why, but it works for me if I assign an empty array in stead of the resetting the arrays length. Like this:

  $scope.changeData = function() {
    /* $scope.myData.length =0; -- this doesn't work */
    $scope.myData = [];
    $scope.myData.push({name: "aMoroni", age: 51});
    $scope.myData.push({name: "bMoroni", age: 52});
    $scope.myData.push({name: "cMoroni", age: 53});
    $scope.myData.push({name: "dMoroni", age: 54});
    $scope.myData.push({name: "eMoroni", age: 55});
  };                   
AardVark71
  • 3,928
  • 2
  • 30
  • 50
0

I added this as an issue on ng-grids github site. It turned out that others had had the same problem, and that there is a solution: (you need to get your hands dirty though and alter the ng-grid code.)

https://github.com/angular-ui/ng-grid/issues/632

Im guessing it will make it into version 3.0

  • Using $watch with the objectEquality parameter set to true is an expensive operations though. See also http://stackoverflow.com/questions/14712089/how-to-deep-watch-an-array-in-angularjs and http://stackoverflow.com/questions/15721295/angularjs-watch-not-being-triggered-for-changes-to-an-array-of-objects – AardVark71 Oct 23 '13 at 12:21