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

How to reload data received async?

Open darknos opened this issue 13 years ago • 7 comments

When my page is loading I don't have any data available. Just requested it from cloud so I need to fill grid after data received.

I tried to "refresh" grid data by using $scope.$apply but without success... :(

Did I missed something?

darknos avatar Dec 05 '12 23:12 darknos

If you look at the main.js file in the workbench folder in source you will see a server side paging example. You need to do something similar in your controller. (except the server side paging part) like this:

    $scope.myData = [];
    self.getDataAsync = function () {
        setTimeout(function () {            
            $scope.myData = myAjaxQuery();
            if (!$scope.$$phase) {
                $scope.$apply();
            }
        }, 3000);
    };
    $scope.gridOptions = {
        data: 'myData',
        columnDefs: [{ field: 'name', displayName: 'Very Long Name Title', sortable: false, headerClass: 'foo', width:'auto' },
                     { field: 'allowance', aggLabelFilter: 'currency', cellTemplate: 'partials/cellTemplate.html', width: 'auto' },
                     { field: 'birthday', cellFilter: 'date', width: 'auto' },
                     { field: 'paid', cellFilter: 'checkmark', width: 'auto' }]
    };
    self.getDataAsync();

orneryd avatar Dec 06 '12 07:12 orneryd

Is this the only way to update ng-grid if you have performed an async operation and updated the $scope array that is already "bound" to the grid? I hope not as this seems slightly convoluted since the grid should already be watching the bound scope variable containing the updated array. Why wouldn't there simply be a grid.refresh() method that then applies the new data already present in the scope variable? I must be missing a key point here somewhere.

MMRandy avatar Jul 25 '14 15:07 MMRandy

@MMRandy I agree this is a huge issue. This plunkr shows that when you load completely different datasets, it breaks the table. http://plnkr.co/edit/BtxcXPZ06zvHuNFQczMv?p=preview

Anyone feel free to answer this stackoverflow question http://stackoverflow.com/questions/27384751/swapping-data-in-angular-ui-grid-new-columns-not-visible-when-changing-dataset

juliusakula avatar Dec 09 '14 17:12 juliusakula

Maybe I am thinking about this wrong, why not just do this:

app.controller('MainCtrl', ['$scope', function ($scope) { $scope.swapData = function() { if ($scope.gridOpts.data === data1) { $scope.gridOpts.columnDefs = [ { name:'firstName' }, { name:'lastName' }, { name:'company' }, { name:'punk' } ]; $scope.gridOpts.data = data2; } else { $scope.gridOpts.columnDefs = [ { name:'firstName' }, { name:'lastName' }, { name:'company' }, { name:'employed' } ]; $scope.gridOpts.data = data1; } };

Based on what I see out on stackoverflow, this would seem easy to do since it has the column names in the json anyway (or you could pull out the columns using something like lo-dash).

kevin-sage avatar Dec 09 '14 17:12 kevin-sage

Here is the updated sample in plunker. http://plnkr.co/edit/OFt86knctJxcbtf2MwYI?p=preview

kevin-sage avatar Dec 09 '14 17:12 kevin-sage

This issue is from ~2 years ago. It should probably not be used to determine what should or should not be done to get a certain result in the grid.

c0bra avatar Dec 10 '14 13:12 c0bra

This plunker shows how to refresh ui-grid based on different data source. Note the use of columnDefs for declaring view based on data.
http://plnkr.co/edit/6khgBVXnIXMDPjSSwLgl?p=preview

arfankgp avatar Sep 24 '16 14:09 arfankgp