Skip to content

Instantly share code, notes, and snippets.

@fabiopaiva
Created July 6, 2015 20:46
Show Gist options
  • Save fabiopaiva/e55f1ed20f54ebffd86b to your computer and use it in GitHub Desktop.
Save fabiopaiva/e55f1ed20f54ebffd86b to your computer and use it in GitHub Desktop.
Angular + Apigility pagination
angular.module('app')
.controller('IndexCtrl', function ($scope){
$scope.navigate('http://host-api/index');
});
angular.module('app')
.controller('MainCtrl', function ($scope, $http){
$scope.getNumber = function (num) {
return new Array(num);
}
$scope.navigate = function (url, page) {
var base = url.split('?')[0];
var query = url.split('?')[1];
var params = {};
if (query) {
var string_values = query.split('&');
for (var i = 0; i < string_values.length; i++) {
params[string_values[i].split('=')[0]] = string_values[i].split('=')[1];
}
}
// forçar a substituir ou criar o parâmetro page se existir
if (page) {
params.page = page;
}
if ($(params).length) {
url = base + '?' + $.param(params);
}
$scope.currentPage = url;
$http.get(url).success(function (data) {
$scope.paginator = data;
if (params.page) {
$scope.paginator.pageActive = params.page;
} else {
$scope.paginator.pageActive = 1;
}
}).error(function (data, status) {
if (data.detail == 'Invalid page provided' && status == 409) {
if (params.page && params.page > 1) {
$scope.navigate(url, params.page - 1);
}
}
});
}
});
<ul class="pagination pagination-sm">
<li ng-if="paginator._links.prev">
<a href="#"
ng-click="navigate(paginator._links.prev.href)">
&lt;&lt;
</a>
</li>
<li ng-repeat="i in getNumber(paginator.page_count) track by $index"
ng-attr-class="{{paginator.pageActive == $index+1 && 'active'}}"
>
<a
ng-click="navigate(paginator._links.first.href, ($index + 1))"
href="#"
>{{$index + 1}}
</a>
</li>
<li ng-if="paginator._links.next">
<a href="#"
ng-click="navigate(paginator._links.next.href)" >
&gt;&gt;
</a>
</li>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment