@@ -67,8 +67,10 @@ Finally, we can reset the data, which gets us back to the middle page and sets t
67
67
<file name="app.js">
68
68
var app = angular.module('app', ['ngTouch', 'ui.grid', 'ui.grid.infiniteScroll']);
69
69
70
- app.controller('MainCtrl', ['$scope', '$http', '$timeout', function ($scope, $http, $timeout) {
71
- $scope.gridOptions = {
70
+ app.controller('MainCtrl', function ($scope, $http, $timeout) {
71
+ var vm = this;
72
+
73
+ vm.gridOptions = {
72
74
infiniteScrollRowsFromEnd: 40,
73
75
infiniteScrollUp: true,
74
76
infiniteScrollDown: true,
@@ -79,117 +81,117 @@ Finally, we can reset the data, which gets us back to the middle page and sets t
79
81
],
80
82
data: 'data',
81
83
onRegisterApi: function(gridApi){
82
- gridApi.infiniteScroll.on.needLoadMoreData($scope, $scope. getDataDown);
83
- gridApi.infiniteScroll.on.needLoadMoreDataTop($scope, $scope. getDataUp);
84
- $scope .gridApi = gridApi;
84
+ gridApi.infiniteScroll.on.needLoadMoreData($scope, getDataDown);
85
+ gridApi.infiniteScroll.on.needLoadMoreDataTop($scope, getDataUp);
86
+ vm .gridApi = gridApi;
85
87
}
86
88
};
87
89
88
90
$scope.data = [];
89
91
90
- $scope .firstPage = 2;
91
- $scope .lastPage = 2;
92
+ vm .firstPage = 2;
93
+ vm .lastPage = 2;
92
94
93
- $scope.getFirstData = function() {
95
+ function getFirstData () {
94
96
return $http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/10000_complex.json')
95
97
.then(function(response) {
96
- var newData = $scope.getPage(response.data, $scope.lastPage);
98
+ var newData = getPage(response.data, vm.lastPage);
99
+
97
100
$scope.data = $scope.data.concat(newData);
98
101
});
99
- };
102
+ }
100
103
101
- $scope.getDataDown = function() {
104
+ function getDataDown () {
102
105
return $http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/10000_complex.json')
103
106
.then(function(response) {
104
- $scope .lastPage++;
105
- var newData = $scope. getPage(response.data, $scope .lastPage);
106
- $scope .gridApi.infiniteScroll.saveScrollPercentage();
107
+ vm .lastPage++;
108
+ var newData = getPage(response.data, vm .lastPage);
109
+ vm .gridApi.infiniteScroll.saveScrollPercentage();
107
110
$scope.data = $scope.data.concat(newData);
108
- return $scope .gridApi.infiniteScroll.dataLoaded($scope .firstPage > 0, $scope .lastPage < 4).then(function() {$scope. checkDataLength('up');});
111
+ return vm .gridApi.infiniteScroll.dataLoaded(vm .firstPage > 0, vm .lastPage < 4).then(function() {checkDataLength('up');});
109
112
})
110
113
.catch(function(error) {
111
- return $scope .gridApi.infiniteScroll.dataLoaded();
114
+ return vm .gridApi.infiniteScroll.dataLoaded();
112
115
});
113
- };
116
+ }
114
117
115
- $scope.getDataUp = function() {
118
+ function getDataUp () {
116
119
return $http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/10000_complex.json')
117
120
.then(function(response) {
118
- $scope .firstPage--;
119
- var newData = $scope. getPage(response.data, $scope .firstPage);
120
- $scope .gridApi.infiniteScroll.saveScrollPercentage();
121
+ vm .firstPage--;
122
+ var newData = getPage(response.data, vm .firstPage);
123
+ vm .gridApi.infiniteScroll.saveScrollPercentage();
121
124
$scope.data = newData.concat($scope.data);
122
- return $scope .gridApi.infiniteScroll.dataLoaded($scope .firstPage > 0, $scope .lastPage < 4).then(function() {$scope. checkDataLength('down');});
125
+ return vm .gridApi.infiniteScroll.dataLoaded(vm .firstPage > 0, vm .lastPage < 4).then(function() {checkDataLength('down');});
123
126
})
124
127
.catch(function(error) {
125
- return $scope .gridApi.infiniteScroll.dataLoaded();
128
+ return vm .gridApi.infiniteScroll.dataLoaded();
126
129
});
127
- };
130
+ }
128
131
129
- $scope.getPage = function(data, page) {
132
+ function getPage (data, page) {
130
133
var res = [];
131
134
for (var i = (page * 100); i < (page + 1) * 100 && i < data.length; ++i) {
132
135
res.push(data[i]);
133
136
}
134
137
return res;
135
- };
138
+ }
136
139
137
- $scope.checkDataLength = function( discardDirection) {
140
+ function checkDataLength ( discardDirection) {
138
141
// work out whether we need to discard a page, if so discard from the direction passed in
139
- if( $scope .lastPage - $scope .firstPage > 3 ){
142
+ if( vm .lastPage - vm .firstPage > 3 ){
140
143
// we want to remove a page
141
- $scope .gridApi.infiniteScroll.saveScrollPercentage();
144
+ vm .gridApi.infiniteScroll.saveScrollPercentage();
142
145
143
146
if( discardDirection === 'up' ){
144
147
$scope.data = $scope.data.slice(100);
145
- $scope .firstPage++;
148
+ vm .firstPage++;
146
149
$timeout(function() {
147
150
// wait for grid to ingest data changes
148
- $scope .gridApi.infiniteScroll.dataRemovedTop($scope .firstPage > 0, $scope .lastPage < 4);
151
+ vm .gridApi.infiniteScroll.dataRemovedTop(vm .firstPage > 0, vm .lastPage < 4);
149
152
});
150
153
} else {
151
154
$scope.data = $scope.data.slice(0, 400);
152
- $scope .lastPage--;
155
+ vm .lastPage--;
153
156
$timeout(function() {
154
157
// wait for grid to ingest data changes
155
- $scope .gridApi.infiniteScroll.dataRemovedBottom($scope .firstPage > 0, $scope .lastPage < 4);
158
+ vm .gridApi.infiniteScroll.dataRemovedBottom(vm .firstPage > 0, vm .lastPage < 4);
156
159
});
157
160
}
158
161
}
159
- };
162
+ }
160
163
161
- $scope .reset = function() {
162
- $scope .firstPage = 2;
163
- $scope .lastPage = 2;
164
+ vm .reset = function() {
165
+ vm .firstPage = 2;
166
+ vm .lastPage = 2;
164
167
165
168
// turn off the infinite scroll handling up and down - hopefully this won't be needed after @swalters scrolling changes
166
- $scope .gridApi.infiniteScroll.setScrollDirections( false, false );
169
+ vm .gridApi.infiniteScroll.setScrollDirections( false, false );
167
170
$scope.data = [];
168
171
169
- $scope. getFirstData().then(function(){
172
+ getFirstData().then(function(){
170
173
$timeout(function() {
171
174
// timeout needed to allow digest cycle to complete,and grid to finish ingesting the data
172
- $scope .gridApi.infiniteScroll.resetScroll( $scope .firstPage > 0, $scope .lastPage < 4 );
175
+ vm .gridApi.infiniteScroll.resetScroll( vm .firstPage > 0, vm .lastPage < 4 );
173
176
});
174
177
});
175
178
};
176
179
177
- $scope. getFirstData().then(function(){
180
+ getFirstData().then(function(){
178
181
$timeout(function() {
179
182
// timeout needed to allow digest cycle to complete,and grid to finish ingesting the data
180
183
// you need to call resetData once you've loaded your data if you want to enable scroll up,
181
184
// it adjusts the scroll position down one pixel so that we can generate scroll up events
182
- $scope .gridApi.infiniteScroll.resetScroll( $scope .firstPage > 0, $scope .lastPage < 4 );
185
+ vm .gridApi.infiniteScroll.resetScroll( vm .firstPage > 0, vm .lastPage < 4 );
183
186
});
184
187
});
185
-
186
- }]);
188
+ });
187
189
</file>
188
190
<file name="index.html">
189
- <div ng-controller="MainCtrl">
190
- <button id="reset" class="button" ng-click="reset()">Reset</button>
191
- <span> First page: {{ firstPage }} Last page: {{ lastPage }} data.length: {{ data.length }} </span>
192
- <div ui-grid="gridOptions" class="grid" ui-grid-infinite-scroll></div>
191
+ <div ng-controller="MainCtrl as $ctrl ">
192
+ <button id="reset" class="button" ng-click="$ctrl. reset()">Reset</button>
193
+ <span> First page: {{ $ctrl. firstPage }} Last page: {{ $ctrl. lastPage }} data.length: {{ data.length }} </span>
194
+ <div ui-grid="$ctrl. gridOptions" class="grid" ui-grid-infinite-scroll></div>
193
195
</div>
194
196
</file>
195
197
<file name="main.css">
0 commit comments