1

Recently I create a AngularJs App . Thats code are below

HTML :

      <div ng-app="" ng-controller="Hello">
            <ul>
                <li ng-repeat="x in greeting">
                    {{ x.user_name }}
                </li>
            </ul>
        </div>

And my JS code is:

function Hello($scope, $http) {
$http.get('http://localhost/google/cibl/dashboard/get_all_user').
    success(function(data) {
        $scope.greeting = data;
    });

}

Its working fine. This http service give me 2000 row now i want to paginate this by AngularJs. How can I do that ?

Drop Shadow
  • 845
  • 3
  • 12
  • 28
  • There might already be one answer here: http://stackoverflow.com/questions/10816073/how-to-do-paging-in-angularjs – joydesigner Jan 13 '15 at 04:14

3 Answers3

1

In your controller

    app.controller('Hello', function($scope){
        $scope.pageSize = 10;
        $scope.currentPage = 0;

        $scope.changePage = function(page){
            $scope.currentPage = page;
        }
    })

In your mark up, you should have

    <div ng-app="" ng-controller="Hello">
        <ul>
            <li ng-repeat="x in greeting | startFrom: currentPage * pageSize | limitTo: pageSize">
                {{ x.user_name }}
            </li>
        </ul>
    </div>

We're missing the startFrom filter so lets create that

    app.filter('startFrom', function() {
        return function(input, start) {
            start = +start; //parse to int
            return input.slice(start);
        }
    });

Now all thats left is the paginating panel, I'll leave it up to you to pretty it with css

    <ul class="pagination" >
        <li ng-repeat="page in pagination" ng-class="{'active':currentPage == page}"><a ng-click="changePage(page)">{{page + 1}}</a></li>
    </ul>

Notes:

  1. The reason why we use changePage() instead of currentPage = page is due to ng-repeat which could break some of the variables
  2. In your anchor () tag, instead of ng-click, you can use a href to mark the page and in your controller, watch the page ref and change based on the queries. The benefits to this is that when you decide to do SEO for your website, it will be ready for that!

    href="#!/partialname?page={{page}}"

yangli-io
  • 16,760
  • 8
  • 37
  • 47
  • Actually I am new on angularJS, I dont understand how to put those code. Should my JS file look like this [My JS File](https://docs.google.com/document/d/1ynCel_tLCVv1eS0yxY7A5OefA6aaJGez8U8h5OGJyiE/edit) – Drop Shadow Jan 13 '15 at 07:25
  • All the stuff in that app controller i wrote for you can go into your Hello function. These are synonymous ways of declaring controllers. – yangli-io Jan 13 '15 at 07:36
  • @YangLi can we use dir-pagination directive for any markup like div or any other custom directives? – Aravind Reddy Dec 28 '17 at 06:54
0

You can do this way:

Pagination Example: http://jsfiddle.net/2ZzZB/56/

Found it in this question:

Pagination on a list using ng-repeat

Community
  • 1
  • 1
0

At least I got a solution and its work properly :

HTML :

<div ng-controller="userController" class="jumbotron">
        <h2>User List</h2> 

        <table  class="table table-striped">
            <thead>
                <tr>
                    <th>User </th>
                    <th>Group</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <tr dir-paginate="u in users | itemsPerPage : 5">
                    <td>{{u.user_name}}</td>
                    <td>{{u.user_type}}</td>

                </tr>                         
            </tbody>
        </table>                
        <dir-pagination-controls on-page-change="pageChanged(current)" template-url="<?php echo base_url(); ?>js/dirPagination.tpl.html"></dir-pagination-controls>
    </div>

and JS : Here i use AngularJs pagination directive

function userController($scope, $http) {
    $scope.users = [];
    $scope.total = 0;
    $scope.perPage = 25; // this should match however many results your API puts on one page
    getUsers(1);

    $scope.pagination = {
        current: 1
    };

    $scope.pageChanged = function (newPage) {
        getUsers(newPage);
    };

    function getUsers(pageNumber) {
            // this is just an example, in reality this stuff should be in a service
            $http.get(app.baseUrl + 'dashboard/get_all_user/' + pageNumber)
                    .success(function (data) {
                        console.log(data);
                        $scope.users = data.users;
                        $scope.total = data.total;
            })
            .error(function (data) {
                console.log(data);
                alert("There was a problem. Please try again later.");
            });
        }
};
var myApp = angular.module('myApp', ['angularUtils.directives.dirPagination']);
var app = app || {};
app.baseUrl = '<?= base_url() ?>';
myApp.controller('userController', userController);
Drop Shadow
  • 845
  • 3
  • 12
  • 28