Syntax Highlighter

Wednesday, May 22, 2013

Superheroic dynamic pagination with AngularJs

Data binding, custom directives, reusable components, dependency injection, you name it! AngularJs has all you need in order to boost front-end productivity. I've been using it for quite a while now and recently I came by an interesting problem: dynamic pagination. I'm not sure if you understand what I mean by "dynamic". I wanted to implement an admin page containing a paginated table holding all the site's signed-up users There would be a searchbar as well, and the number of pages and filtering should occur dynamically as the user types in the query.
Using data binders and clever event handling this can be easily accomplished by angular and the result is incredibly satisfying. Let me show you the code. First add these stylesheets:
<link href="" rel="stylesheet">
<link href="" rel="stylesheet">
Now the table view. It binds any change to the text input to the function $, adds all the pagedItems[currentPage] array to the table's body and sets up the pagination logic in the table's foot:
        <div ng-controller="ctrlRead">
            <div class="input-append">
                <input type="text" ng-model="query" ng-change="search()" class="input-large search-query" placeholder="Search">
             <span class="add-on"><i class="icon-search"></i></span>
            <table class="table table-striped table-condensed table-hover">
                        <th class="id">Id&nbsp;</th>
                        <th class="name">Name&nbsp;</th>
                        <th class="description">Description&nbsp;</th>
                        <th class="field3">Field 3&nbsp;</th>
                        <th class="field4">Field 4&nbsp;</th>
                        <th class="field5">Field 5&nbsp;</th>
                    <td colspan="6">
                        <div class="pagination pull-right">
                                <li ng-class="{disabled: currentPage == 0}">
                                    <a href ng-click="prevPage()">« Prev</a>
                                <li ng-repeat="n in range(pagedItems.length)"
                                    ng-class="{active: n == currentPage}"
                                    <a href ng-bind="n + 1">1</a>
                                <li ng-class="{disabled: currentPage == pagedItems.length - 1}">
                                    <a href ng-click="nextPage()">Next »</a>
                    <tr ng-repeat="item in pagedItems[currentPage] | orderBy:sortingOrder:reverse">
We need only build the filteredItems array after each call to $ accordingly. This can be done by passing the classic needle/haystack function as $filter's matching function and then split the result based on the number of elements per page. But talk is cheap, I know:
//classical way of finding a substring (needle) in a given string (haystack)
    var searchMatch = function (haystack, needle) {
        if (!needle) {
            return true;
        return haystack.toLowerCase().indexOf(needle.toLowerCase()) !== -1;

    // filter the items following the search string
    $ = function () {
        $scope.filteredItems = $filter('filter')($scope.items, function (item) {
            for(var attr in item) {
                    if (searchMatch(item[attr], $scope.query))
                        return true;
            return false;
        // take care of the sorting order
        if ($scope.sortingOrder !== '') {
            $scope.filteredItems = $filter('orderBy')($scope.filteredItems, $scope.sortingOrder, $scope.reverse);
        $scope.currentPage = 0;
        // now group by pages
    // divide elements by page
    $scope.groupToPages = function () {
        $scope.pagedItems = [];
        for (var i = 0; i < $scope.filteredItems.length; i++) {
            if (i % $scope.itemsPerPage === 0) {
                $scope.pagedItems[Math.floor(i / $scope.itemsPerPage)] = [ $scope.filteredItems[i] ];
            } else {
                $scope.pagedItems[Math.floor(i / $scope.itemsPerPage)].push($scope.filteredItems[i]);
Finally, just add the paging logic for the table's foot and call $ so we show everything after the page loads.
    $scope.range = function (end) {
        var ret = [];
        for(var i = 0; i < end; i++) {
        return ret;
    $scope.prevPage = function () {
        if ($scope.currentPage > 0) {
    $scope.nextPage = function () {
        if ($scope.currentPage < $scope.pagedItems.length - 1) {
    $scope.setPage = function () {
        $scope.currentPage = this.n;

    // create filtered items for the first time
Just add this code to our view's controller and we're done! Check out this fiddle with our working code and some mock data! Now, of course this is only useful when all the necessary data can be fetched from the server quickly, so that the search is consistent. If you cannot provide Angular with all the necessary data, perhaps you should consider infinite scrolling instead. If that's the case, check this out. Have fun!