Angular ui grid how to show a loader
Asked Answered
P

6

13

I'm wondering how to show a simple loader before data was loaded. I'm using ng-grid-1.3.2 I'm googling but I didn't find any example. Bye

Phrasing answered 11/9, 2013 at 14:16 Comment(0)
V
9

like Maxim Shoustin suggested you can use the angularjs-spinner from Jim Lavin which uses (deprecated) Response Interceptors.

I think it's explained best here : http://codingsmackdown.tv/blog/2013/04/20/using-response-interceptors-to-show-and-hide-a-loading-widget-redux/

In a nutshell, in his first solution, what you have to do for your ng-grid app is:

1) Add the loading gif to your html (for loading gif look here)

<div id="loadingWidget" class="row-fluid ui-corner-all" style="padding: 0 .7em;" loading-widget >
    <div class="loadingContent">
        <p>
            <img alt="Loading  Content" src="images/ajax-loader.gif" /> Loading
        </p>
    </div>
</div>

2) In your code as soon as you have declared your app level module add the Response Interceptors for http requests to the configuration block

var app = angular.module('myCoolGridApp', ['ngGrid']);

app.constant('_START_REQUEST_', '_START_REQUEST_');
app.constant('_END_REQUEST_', '_END_REQUEST_');
app.config(['$httpProvider', '_START_REQUEST_', '_END_REQUEST_', function ($httpProvider, _START_REQUEST_, _END_REQUEST_) {
    var $http,
     interceptor = /* see extra details on codingsmackdown.tv */
    $httpProvider.responseInterceptors.push(interceptor); 
}

3) and then add your loadingWidget directive

app.directive('loadingWidget', ['_START_REQUEST_', '_END_REQUEST_', function (_START_REQUEST_, _END_REQUEST_) {
return {
    restrict: "A",
    link: function (scope, element) {
        element.hide();
        scope.$on(_START_REQUEST_, function () {element.show();});
        scope.$on(_END_REQUEST_, function () {element.hide();});
    }
 };
}]);

See more details at codingsmackdown

Vermiculation answered 13/9, 2013 at 11:29 Comment(2)
Finally I got it thanks I advice for noob like me this starting point endlessindirection.wordpress.com/2013/05/19/…Phrasing
"Deprecated"... as in we shouldn't use this method?Fie
T
5

I had the same question as you.

I find this nice tutorial about it: http://brianhann.com/ui-grid-the-easiest-customization-youll-ever-write/

He use vm.loading = true while fetching data from server and changed to false after complete.

var app = angular.module('app', ['ngTouch', 'ui.grid']);

app.controller('MainCtrl', ['$http', '$timeout', function ($http, $timeout) {
  var vm = this;

  vm.reset = reset;
  vm.noData = noData;
  vm.gridOptions = {
    columnDefs: [
      { field: 'name' },
      { field: 'age' }
    ]
  };

  reset();

  ////////////

  // Initialize our data source
  function init() {
    $http.get('data.json')
      .success(function (data) {
        vm.gridOptions.data = data;
      })
      .finally(function () {
        vm.loading = false;
      });
  }

  // Reset the data source in a timeout so we can see the loading message
  function reset() {
    vm.loading = true;

    vm.gridOptions.data = [];

    $timeout(function () {
      init();
    }, 1000);
  }

  function noData() {
    vm.gridOptions.data = [];
  }
}]);

In the HTML, he uses ng-hide to show/hide the spinner based on values of gridOptions.data and vm.loading:

<div id="grid1" ui-grid="vm.gridOptions" class="grid">
    <div class="grid-msg-overlay" ng-hide="!vm.loading">
      <div class="msg">
        <span>
          Loading Data...
          <i class="fa fa-spinner fa-spin"></i>
        </span>
      </div>
    </div>
    <div class="grid-msg-overlay" ng-hide="vm.loading || vm.gridOptions.data.length">
      <div class="msg">
        <span>No Data</span>
      </div>
    </div>
</div>

Here is the Plunker of the final version shown.

Terrazzo answered 30/11, 2015 at 13:9 Comment(0)
S
2

You have angularjs-spinner, see GitHub sources

Spic answered 11/9, 2013 at 14:27 Comment(3)
Thanks, sorry to bother you but may be there are a quick to use example for ng-grid :)Phrasing
Why you want to use ng-grid? its strangeSpic
I'm quite new at angular tools so I've though it'd be a good one but I need a loader ! :) so could you give me an hint on what is best to use for a grid + pagination + loader ?Phrasing
P
1

I also needed a similar behavior and I came across this answer but I needed to show something inside the grid itself so here is something I put together. My idea is that I change the gridOptions on the fly and show a loader as a row inside the grid.

loaderOptions = {
        "columnDefs": [{
            "name": "",
            "field": "loading",
            "enableColumnMenu": false,
            "cellTemplate": '<div style="width:90px; margin:auto;"><span class="glyphicon glyphicon-refresh glyphicon-refresh-animate"></span>&nbsp;Loading...</div>'
        }],
        "data": [{
            "loading": ""
        }] 
    };
Palembang answered 17/8, 2015 at 18:45 Comment(0)
S
1

Simply,by adding this code in your html part:

<img alt="loading..." src='images/ajax-loader.gif")' /> loading message...

and the following code in your app.controller script:

 $http.get(yourdataUrl)
   .then(function (response) {
       $scope.records = response.data;
       $("#loadingWidget").hide();
   });

it works fine for me!

Silviasilviculture answered 14/2, 2017 at 8:6 Comment(0)
W
0

The HTML code-sample

 <img ng-show="loading" src="~/img/loding.jpg" />
 <div class="ngtyle" ng-grid="myGridView"></div>

The AngularJs code-sample

var app = angular.module('App', ['ngGrid']);
app.controller('MainCtrl', function ( $scope, myService ) {
  $scope.loading = true;

  myService.get().then( function ( response ) {
    $scope.items = response.data;
  })
  .finally(function() {
    $scope.loading = false;
  });

 $scope.myGridView = {
    data: 'dataList',
    columnDefs: 'myDisplayColumns'};
});    
Woolgrower answered 11/9, 2013 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.