Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. <!doctype html>
  2. <html ng-app="app">
  3. <head>
  4. <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
  5. <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
  6. <link href="http://code.ionicframework.com/ionicons/2.0.0/css/ionicons.min.css" rel="stylesheet" type="text/css" />
  7. <link href="custom/css/admin.css" rel="stylesheet" type="text/css" />
  8. <link href="custom/css/skin-blue.css" rel="stylesheet" type="text/css" />
  9. <link rel="stylesheet" href="http://ui-grid.info/release/ui-grid.css" type="text/css">
  10. <link rel="stylesheet" href="main.css" type="text/css">
  11. <script src="plugins/jQuery/jQuery-2.1.3.min.js"></script>
  12. <script src="bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
  13. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.js"></script>
  14. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular-touch.js"></script>
  15. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular-animate.js"></script>
  16. <script src="http://ui-grid.info/docs/grunt-scripts/csv.js"></script>
  17. <script src="http://ui-grid.info/docs/grunt-scripts/pdfmake.js"></script>
  18. <script src="http://ui-grid.info/docs/grunt-scripts/vfs_fonts.js"></script>
  19. <script src="http://ui-grid.info/release/ui-grid.js"></script>
  20. <script src="infinite_app.js"></script>
  21. </head>
  22. <body>
  23. <div ng-controller="MainCtrl">
  24. <button id="reset" class="button" ng-click="reset()">Reset</button>
  25. <span> &nbsp; &nbsp; First page: {{ firstPage }} &nbsp; &nbsp; Last page: {{ lastPage }} &nbsp; &nbsp; data.length: {{ data.length }} </span>
  26. <div ui-grid="gridOptions" class="grid" ui-grid-infinite-scroll></div>
  27. </div>
  28. </body>
  29. </html>
  30.  
  31. var app = angular.module('app', ['ngTouch', 'ui.grid', 'ui.grid.infiniteScroll']);
  32.  
  33. app.controller('MainCtrl', ['$scope', '$http', '$timeout', function ($scope, $http, $timeout) {
  34. $scope.gridOptions = {
  35. infiniteScrollRowsFromEnd: 40,
  36. infiniteScrollUp: true,
  37. infiniteScrollDown: true,
  38. columnDefs: [
  39. { name:'equipmentId'},
  40. { name:'equipmentName' },
  41. { name:'creationDate' }
  42. ],
  43. data: 'data',
  44. onRegisterApi: function(gridApi){
  45. gridApi.infiniteScroll.on.needLoadMoreData($scope, $scope.getDataDown);
  46. gridApi.infiniteScroll.on.needLoadMoreDataTop($scope, $scope.getDataUp);
  47. $scope.gridApi = gridApi;
  48. }
  49. };
  50.  
  51. $scope.data = [];
  52.  
  53. $scope.firstPage = 2;
  54. $scope.lastPage = 2;
  55. var header = {headers: {
  56. 'Accept': 'application/json',
  57. 'user-token': 'Some Secure Token to connect with REST',
  58. 'Namespace': 'false'
  59. }
  60. };
  61.  
  62. $scope.getFirstData = function() {
  63. return $http.get('http://ServerIP:8080/rest/utl/employee?limit=1000',header)
  64. .then(function(response) {
  65. var newData = $scope.getPage(response.data.collection.element, $scope.lastPage);
  66. $scope.data = $scope.data.concat(newData);
  67. });
  68. };
  69.  
  70. $scope.getDataDown = function() {
  71. return $http.get('http://ServerIP:8080/rest/utl/employee?limit=1000',header)
  72. .then(function(response) {
  73. $scope.lastPage++;
  74. var newData = $scope.getPage(response.data.collection.element, $scope.lastPage);
  75. $scope.gridApi.infiniteScroll.saveScrollPercentage();
  76. $scope.data = $scope.data.concat(newData);
  77. return $scope.gridApi.infiniteScroll.dataLoaded($scope.firstPage > 0, $scope.lastPage < 4).then(function() {$scope.checkDataLength('up');});
  78. })
  79. .catch(function(error) {
  80. return $scope.gridApi.infiniteScroll.dataLoaded();
  81. });
  82. };
  83.  
  84. $scope.getDataUp = function() {
  85. return $http.get('http://ServerIP:8080/rest/utl/employee?limit=1000',header)
  86. .then(function(response) {
  87. $scope.firstPage--;
  88. var newData = $scope.getPage(response.data.collection.element, $scope.firstPage);
  89. $scope.gridApi.infiniteScroll.saveScrollPercentage();
  90. $scope.data = newData.concat($scope.data);
  91. return $scope.gridApi.infiniteScroll.dataLoaded($scope.firstPage > 0, $scope.lastPage < 4).then(function() {$scope.checkDataLength('down');});
  92. })
  93. .catch(function(error) {
  94. return $scope.gridApi.infiniteScroll.dataLoaded();
  95. });
  96. };
  97.  
  98. $scope.getPage = function(data, page) {
  99. var res = [];
  100. for (var i = (page * 100); i < (page + 1) * 100 && i < data.length; ++i) {
  101. res.push(data[i]);
  102. }
  103. return res;
  104. };
  105.  
  106. $scope.checkDataLength = function( discardDirection) {
  107. // work out whether we need to discard a page, if so discard from the direction passed in
  108. if( $scope.lastPage - $scope.firstPage > 3 ){
  109. // we want to remove a page
  110. $scope.gridApi.infiniteScroll.saveScrollPercentage();
  111.  
  112. if( discardDirection === 'up' ){
  113. $scope.data = $scope.data.slice(100);
  114. $scope.firstPage++;
  115. $timeout(function() {
  116. // wait for grid to ingest data changes
  117. $scope.gridApi.infiniteScroll.dataRemovedTop($scope.firstPage > 0, $scope.lastPage < 4);
  118. });
  119. } else {
  120. $scope.data = $scope.data.slice(0, 400);
  121. $scope.lastPage--;
  122. $timeout(function() {
  123. // wait for grid to ingest data changes
  124. $scope.gridApi.infiniteScroll.dataRemovedBottom($scope.firstPage > 0, $scope.lastPage < 4);
  125. });
  126. }
  127. }
  128. };
  129.  
  130. $scope.reset = function() {
  131. $scope.firstPage = 2;
  132. $scope.lastPage = 2;
  133.  
  134. // turn off the infinite scroll handling up and down - hopefully this won't be needed after @swalters scrolling changes
  135. $scope.gridApi.infiniteScroll.setScrollDirections( false, false );
  136. $scope.data = [];
  137.  
  138. $scope.getFirstData().then(function(){
  139. $timeout(function() {
  140. // timeout needed to allow digest cycle to complete,and grid to finish ingesting the data
  141. $scope.gridApi.infiniteScroll.resetScroll( $scope.firstPage > 0, $scope.lastPage < 4 );
  142. });
  143. });
  144. };
  145.  
  146. $scope.getFirstData().then(function(){
  147. $timeout(function() {
  148. // timeout needed to allow digest cycle to complete,and grid to finish ingesting the data
  149. // you need to call resetData once you've loaded your data if you want to enable scroll up,
  150. // it adjusts the scroll position down one pixel so that we can generate scroll up events
  151. $scope.gridApi.infiniteScroll.resetScroll( $scope.firstPage > 0, $scope.lastPage < 4 );
  152. });
  153. });
  154.  
  155. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement