{"id":1124,"date":"2015-08-04T07:07:17","date_gmt":"2015-08-04T07:07:17","guid":{"rendered":"http:\/\/www.honobono-life.info\/wpeng\/?p=1124"},"modified":"2015-08-04T07:07:17","modified_gmt":"2015-08-04T07:07:17","slug":"paginate-the-users-with-angularjs","status":"publish","type":"post","link":"http:\/\/www.honobono-life.info\/wpeng\/paginate-the-users-with-angularjs\/","title":{"rendered":"Ruby on Rails Tutorial paginate the users with AngularJS"},"content":{"rendered":"<p>ref.<br \/>\r\nhttp:\/\/angular-ui.github.io\/bootstrap\/#\/pagination<br \/>\r\nhttp:\/\/getbootstrap.com\/components\/#pagination<\/p>\r\n\r\n<p><strong>1)AngularJS template view<\/strong><\/p>\r\n\r\n<p>$ vi app\/assets\/templates\/users\/index.html.erb<\/p>\r\n\r\n<div ng-non-bindable><pre class=\"prettyprint\">\r\n&lt;div ng-controller=&quot;UsersIndexCtrl&quot;&gt;\r\n  &lt;h1 class=&quot;text-center&quot;&gt;All users&lt;\/h1&gt;\r\n  &lt;pagination total-items=&quot;totalitems&quot;\r\n              ng-model=&quot;currentpage&quot;\r\n              max-size=&quot;maxsize&quot;\r\n              items-per-page=&quot;itemsperpage&quot;\r\n              class=&quot;pagination-sm&quot;\r\n              previous-text=&quot;Previous&quot;\r\n              next-text=&quot;Next&quot;\r\n              first-text=&quot;First&quot;\r\n              last-text=&quot;Last&quot;\r\n              boundary-links=&quot;true&quot;&gt;\r\n  &lt;\/pagination&gt;&lt;br \/&gt;\r\n  &lt;ul ng-repeat=&quot;user in currentitems&quot; class=&quot;users&quot;&gt;\r\n    &lt;li&gt;&lt;img alt=&quot;{{user.name}}&quot; src=&quot;https:\/\/secure.gravatar.com\/avatar\/{{hash(user.email)}}?s=52&quot; \/&gt;\r\n      &lt;a href=&quot;\/users\/{{user.id}}&quot;&gt;{{user.name}}&lt;\/a&gt;\r\n    &lt;\/li&gt;\r\n  &lt;\/ul&gt;\r\n  &lt;pagination total-items=&quot;totalitems&quot;\r\n              ng-model=&quot;currentpage&quot;\r\n              max-size=&quot;maxsize&quot;\r\n              items-per-page=&quot;itemsperpage&quot;\r\n              class=&quot;pagination-sm&quot;\r\n              previous-text=&quot;Previous&quot;\r\n              next-text=&quot;Next&quot;\r\n              first-text=&quot;First&quot;\r\n              last-text=&quot;Last&quot;\r\n              boundary-links=&quot;true&quot;\r\n              rotate=&quot;false&quot;&gt;\r\n  &lt;\/pagination&gt;\r\n&lt;\/div&gt;<\/pre><\/div>\r\n\r\n<p>2)AngularJS controller<\/p>\r\n\r\n<pre class=\"prettyprint\">\r\nmyModule.controller(&quot;UsersIndexCtrl&quot;, function($scope, userResource, flashService, $location, sessionResource, $q) {\r\n  $scope.maxsize = 5;\r\n  $scope.itemsperpage = 3;\r\n  $scope.currentpage = 1;\r\n  var start = 0;\r\n  var end = start + $scope.itemsperpage;\r\n\r\n  var qgetUser = function(deferred) {\r\n.........\r\n  }\r\n  var deferred = $q.defer();\r\n  deferred.promise.then(function (result) {\r\n    var user_info = result;\r\n    if (user_info.user.id &gt; 0) {\r\n      userResource.index({}, function(response) {\r\n        $scope.users = response;\r\n        $scope.totalitems = $scope.users.length;\r\n        $scope.currentitems = $scope.users.slice(start,end);\r\n\r\n        $scope.$watch(&#39;currentpage&#39;, function() {\r\n          start = ($scope.currentpage - 1) * $scope.itemsperpage;\r\n          end = start + $scope.itemsperpage;\r\n          $scope.currentitems = $scope.users.slice(start,end);\r\n        },true);\r\n      });\r\n    } else {\r\n      $location.path(&quot;\/signin&quot;);\r\n    }\r\n  },function (reason) {\r\n    console.log(&quot;qgetUser-Error&quot;);\r\n  })\r\n  qgetUser (deferred);\r\n\r\n  $scope.hash = function(email) {\r\n    return md5(email.toLowerCase());\r\n  };\r\n});<\/pre>","protected":false},"excerpt":{"rendered":"<p>ref. http:\/\/angular-ui.github.io\/bootstrap\/#\/pagination http:\/\/getbootstrap.com\/components\/#pagination 1)AngularJS template view $ vi app\/assets\/templates\/users\/index.html.erb &lt;div ng-controller=&quot;UsersIndexCtrl&quot;&gt; &lt;h1 class=&quot;text-center&quot;&gt;All users&lt;\/h1&gt; &lt;pagination total-items=&quot;totalitems&quot; ng-model=&quot;currentpage&quot; max-size=&quot;maxsize&quot; items-per-page=&quot;itemsperpage&quot; class=&quot;pagination-sm&quot; previous-text=&quot;Previous&quot; next-text=&quot;Next&quot; first-text=&quot;First&quot; last-text=&quot;Last&quot; boundary-links=&quot;true&quot;&gt; &lt;\/pagination&gt;&lt;br \/&gt; &lt;ul ng-repeat=&quot;user in currentitems&quot; class=&quot;users&quot;&gt; &lt;li&gt;&lt;img alt=&quot;{{user.name}}&quot; src=&quot;https:\/\/secure.gravatar.com\/avatar\/{{hash(user.email)}}?s=52&quot; \/&gt; &lt;a href=&quot;\/users\/{{user.id}}&quot;&gt;{{user.name}}&lt;\/a&gt; &lt;\/li&gt; &lt;\/ul&gt; &lt;pagination total-items=&quot;totalitems&quot; ng-model=&quot;currentpage&quot; max-size=&quot;maxsize&quot; items-per-page=&quot;itemsperpage&quot; class=&quot;pagination-sm&quot; previous-text=&quot;Previous&quot; next-text=&quot;Next&quot; first-text=&quot;First&quot; last-text=&quot;Last&quot; boundary-links=&quot;true&quot; rotate=&quot;false&quot;&gt; &lt;\/pagination&gt; &lt;\/div&gt; 2)AngularJS [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[137],"tags":[35,174,172,173],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/1124"}],"collection":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/comments?post=1124"}],"version-history":[{"count":1,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/1124\/revisions"}],"predecessor-version":[{"id":1125,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/1124\/revisions\/1125"}],"wp:attachment":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/media?parent=1124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/categories?post=1124"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/tags?post=1124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}