ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

grid data can not be changed

Open lancelothuxi opened this issue 11 years ago • 11 comments

Hello, I'am currently using ui grid the same version as the demo site http://ui-grid.info/, but I can not update data in grid after ajax response, I re-assigned the value of ajax response to gridOptions.data, but the ui does not refresh, I've debug and see the gridOptions.data get the right value, just the UI stuck. I just referred http://ui-grid.info/docs/#/tutorial/112_swapping_data. By the way, the external pagination works correctly, when navigate to another page, the data get refreshed and the data as well.

lancelothuxi avatar Jan 30 '15 11:01 lancelothuxi

I think this comes down to the basics of object passing and pointers in javascript. Once you tell the grid that it's data is in some place (say, $scope.gridOptions.data), then it holds a pointer to that object. If you go replace that object by $scope.gridOptions.data = newData, then the grid still has a pointer to the old object.

There are two basic patterns for dealing with this.

Firstly, you can put your data somewhere else, and then tell the grid to get it by reference. So you do:

  $scope.data = myData;
  $scope.gridOptions.data = 'data';

  // later in your code
  $scope.data = newData

The grid will now evaluate $scope.data each time it wants the data, and so it will pick up if you've changed where that's pointing.

Alternatively, you can put you data directly onto the gridOptions, but then you can't change the object it refers to. So you'd do more:

  $scope.gridOptions.data = myData;

  // later in your code
  $scope.gridOptions.data.length = 0;
  $scope.gridOptions.data.concat(newData);

So you're replacing the contents of $scope.gridOptions.data, but not changing the object it points to.

PaulL1 avatar Jan 30 '15 12:01 PaulL1

Thanks so much, I think you are right and going to give a try.

lancelothuxi avatar Jan 30 '15 13:01 lancelothuxi

@PaulL1 I have similar refresh issue.

I tried this, but a result an empty grid, concat method doesn't change the original array.

$scope.gridOptions.data.length = 0;
$scope.gridOptions.data.concat(newData);

I tried this also, in this case grid not empty, but not refreshed with newdata.

$scope.gridOptions.data.length = 0;
$scope.gridOptions.data = $scope.gridOptions.data.concat(newData);

Can you help me, what am I doing wrong ?

extdevelopment avatar Feb 16 '15 19:02 extdevelopment

@PaulL1 $timeout helped, grid refreshed in next digest loop. I could not find any other solution.

SonataService.query(params).$promise.then(function(data) {
    $scope.gridOptions.data.length = 0;
    $timeout(function(){
        $scope.gridOptions.data = data.data;
    });
});

extdevelopment avatar Feb 18 '15 17:02 extdevelopment

@extdevelopment What is SonataService.query(params).$promise? Is it an AngularJS $q promise?

If not, then your operation in your .then() function is outside Angular's $digest cycle and that's why your changes are not occurring until you wrap them in a $timeout(). Timeout inherently triggers a new $digest.

c0bra avatar Feb 18 '15 19:02 c0bra

Looks like you can manipulate the data array quite safely, as this plunker shows: http://plnkr.co/edit/GIBGqGwWSGjjuJwJsc2F?p=preview. Storing it here in case I need it again in some other issue.

PaulL1 avatar Feb 18 '15 19:02 PaulL1

@c0bra SonataService.query(params).$promise is $resource angularjs $q promise. https://docs.angularjs.org/api/ngResource/service/$resource $promise: the promise of the original server interaction that created this instance or collection.

extdevelopment avatar Feb 28 '15 18:02 extdevelopment

I tried @extdevelopment method and it worked but it caused the grid to go empty and then refilled, my other option was to fill up the objects in the data array with new values and these reflected back to the grid without flicker or the need for $timeout, I didn't even need notifyDataChange(uiGridConstants.dataChange.ROW) and while the data update was on RESTful promise something like $resource it was already on a $digest loop (judging by trying $apply with exception throw already in a digest cycle) here is piece of my code using lodash

    _.forEach(data, function(d, i){
        _.assign($scope.gridOptions.data[i], d);
    });

This code is very dangerous if the data coming from the resource didn't match the same order/sort of the data in $scope.gridOptions.data things will break badly for me.

P.S. Not calling notifyDataChange will not call for cellClass to update it.

nadersoliman avatar Mar 20 '15 22:03 nadersoliman

I meet the similar problem, however I am using ui-grid = {data: myScopeGridData} to bind the backend data. However if I change the data source (basically I am do a switch between two json requests), the ui-grid does not get refreshed every other time, I did debug into the source code of ui-grid and see that the data from the latter ajax response is correctly passed to the rendered, yet it does not do the rendering corretly.

DongShi avatar Aug 21 '15 07:08 DongShi

$scope.gridOptions.data = newData.slice(firstRow, firstRow + paginationOptions.pageSize); $scope.gridOptions.data.concat(newData);// Data is there till this point

console.log( $scope.gridOptions.data)//length is 0 here.

Same issue, grid not rendering data on page change.

maddy25 avatar Feb 23 '16 22:02 maddy25

I use a function that watches my model ( $scope.model ) for changes and updates the grid.

$scope.$watch('model', function (newVal, oldVal) { if (oldVal !== newVal && newVal !== undefined) { $scope.gridOptions.data = newVal.data.someData; } }, true);

judsonmusic avatar Jun 07 '16 17:06 judsonmusic