Pagination service and same is used for client pagination

This commit is contained in:
Nayan Ambali 2013-08-16 00:24:35 +05:30
parent b49f80da6a
commit 84f20be287
5 changed files with 64 additions and 7 deletions

1
.gitignore vendored
View File

@ -14,4 +14,5 @@ logs
results
npm-debug.log
.idea/*
image/Thumbs.db
**/.DS_Store

View File

@ -16,7 +16,7 @@
</tr>
</thead>
<tbody>
<tr ng-repeat="client in clients.pageItems">
<tr ng-repeat="client in clients.currentPageItems | filter:filterText">
<td><a href="#/viewclient/{{client.id}}">{{client.displayName}}</a></td>
<td><a href="#/viewclient/{{client.id}}">{{client.accountNo}}</a></td>
<td>{{client.staffName}}</td>
@ -24,4 +24,8 @@
</tr>
</tbody>
</table>
<ul class="pager">
<li class="previous"><a ng-click="clients.previous()" href="" ng-disabled="!clients.hasPrevious()">&larr; Prev</a></li>
<li class="next" ><a ng-click="clients.next()" href="" ng-disabled="!clients.hasNext()">Next &rarr;</a></li>
</ul>
</div>

View File

@ -1,13 +1,18 @@
(function(module) {
mifosX.controllers = _.extend(module, {
ClientController: function(scope, resourceFactory) {
ClientController: function(scope, resourceFactory , paginatorService) {
scope.clients = [];
resourceFactory.clientResource.getAllClients(function(data) {
scope.clients = data;
});
var fetchFunction = function(offset, limit, callback) {
resourceFactory.clientResource.getAllClients({offset: offset, limit: limit} , callback);
};
scope.clients = paginatorService.paginate(fetchFunction, 4);
}
});
mifosX.ng.application.controller('ClientController', ['$scope', 'ResourceFactory', mifosX.controllers.ClientController]).run(function($log) {
mifosX.ng.application.controller('ClientController', ['$scope', 'ResourceFactory', 'PaginatorService', mifosX.controllers.ClientController]).run(function($log) {
$log.info("ClientController initialized");
});
}(mifosX.controllers || {}));

View File

@ -20,7 +20,8 @@ define(['underscore', 'mifosX'], function() {
'ResourceFactoryProvider',
'HttpServiceProvider',
'AuthenticationService',
'SessionManager'
'SessionManager',
'Paginator'
],
directives: [
'DataTablesDirective',

46
js/services/Paginator.js Normal file
View File

@ -0,0 +1,46 @@
(function(module) {
mifosX.services = _.extend(module, {
PaginatorService: function(scope, httpService) {
this.paginate = function(fetchFunction, pageSize) {
var paginator = {
hasNextVar: false,
next: function() {
if (this.hasNextVar) {
this.currentOffset += pageSize;
this._load();
}
},
_load: function() {
var self = this;
fetchFunction(this.currentOffset, pageSize + 1, function(items) {
self.currentPageItems = items.pageItems;
self.hasNextVar = items.pageItems.length === pageSize + 1;;
});
},
hasNext: function() {
return this.hasNextVar;
},
previous: function() {
if(this.hasPrevious()) {
this.currentOffset -= pageSize;
this._load();
}
},
hasPrevious: function() {
return this.currentOffset !== 0;
},
currentPageItems: [],
currentOffset: 0
};
// Load the first page
paginator._load();
return paginator;
};
}
});
mifosX.ng.services.service('PaginatorService', ['$rootScope', 'HttpService', mifosX.services.PaginatorService]).run(function($log) {
$log.info("PaginatorService initialized");
});
}(mifosX.services || {}));