diff options
Diffstat (limited to 'src/main/resources/webapp/apps/core/PagingTableService.js')
-rwxr-xr-x | src/main/resources/webapp/apps/core/PagingTableService.js | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/main/resources/webapp/apps/core/PagingTableService.js b/src/main/resources/webapp/apps/core/PagingTableService.js new file mode 100755 index 0000000..af593df --- /dev/null +++ b/src/main/resources/webapp/apps/core/PagingTableService.js @@ -0,0 +1,72 @@ +function PagingTableService() { + var create = function ($scope, fetchCallback, options) { + options = options || {}; + var watcher = options.watcher || function(){}; + var self = { + rows: [], + startIndex: options.startIndex || 0, + count: options.count || 10 + }; + + var update = function(){ + fetchCallback(self.startIndex, self.count, function(data) { + self.rows = data.rows; + watcher(); + }); + }; + + self.first = function () { + self.startIndex = 0; + update(); + }; + + self.next = function () { + self.startIndex += self.count; + update(); + }; + + self.prev = function () { + if (self.startIndex == 0) { + return; + } + self.startIndex -= self.count; + update(); + }; + + // Do an initial fetch + update(); + + return self; + }; + + var defaultCallback = function(Resource, args) { + args = args || {}; + return function(startIndex, count, cb) { + if(startIndex) { + args.startIndex = startIndex; + } + if(count) { + args.count = count; + } + console.log("fetching", args); + Resource.query(args, function(data, headers) { + var totalResults = headers("total-results"); + console.log("totalResults", totalResults); + console.log("got data", data); + cb({ + totalResults: totalResults, + rows: data + }); + }); + }; + }; + + return { + create: create, + defaultCallback: defaultCallback + } +} + +angular. + module('pagingTableService', ['ngResource']). + factory('PagingTableService', PagingTableService); |