Skip to content
  • Works
  • Services
  • About
  • Blog
  • Contact

Start free trial

We bring together everything that’s required to build websites. Reach more customers, save time and money, and boost sales.

Edit Content

    • Facebook
    • Instagram
    • Twitter
    Purchase Theme
    AngularJS, JS

    Pagination in AngularJS for Typekit API – Show More Pagination in AngularJS

    March 4, 2013 M. Mahbubur Rahman No comments yet

    Pagination in web applications is a very common phenomenon and frequently used to split large result set into pages. The evolution of pagination with creativity has brought us from [1,2,3] to automatic pagination (like you see on google images). Naturally the number based pagination is becoming less and less common these days. We are seeing “Load more”, “Show more” type of pagination being adopted more perhaps. We’ll discuss about this in a separate UX article later. For today we will attempt to build something similar to “Show More” Pagination in AngularJS Framework.

    Demo View It Online
    Download Source Files

    Before we begin, we need a considerably large dataset. We have chosen typekit api to do the dirty job. Let’s get into code thing, shall we ?

    As you can see here, we used one of the preferred angular code conventions, used $http and a factory as you can see below.

    [cc lang=”javascript”]

    ‘use strict’;

    var App = angular.module(‘PaginationApp’, [‘services.SharedServices’]);

    App.controller(‘MainCtrl’, [‘$scope’, ‘$filter’, ‘TypekitService’, function ($scope, $filter, Typekit) {
    $scope.loadData = function () {
    var url = ‘https://typekit.com/api/v1/json/libraries/full?page=’ + $scope.page + ‘&per_page=’ + $scope.per_page + ‘&callback=JSON_CALLBACK’;
    Typekit.getTypekits(url).then(function (response) {
    $scope.more = response.data.library.families.length === $scope.per_page;
    $scope.families = $scope.families.concat(response.data.library.families);
    $scope.status_bar = “Showing ” + ($scope.families.length === 0 ? “0” : “1”) + ” to ” + $filter(‘number’)($scope.families.length) + ” of ” + $filter(‘number’)(response.data.library.pagination.count) + ” entries”;

    });
    }

    $scope.show_more = function () {
    $scope.page += 1;
    $scope.loadData();
    }

    $scope.has_more = function () {
    return $scope.more;
    }

    $scope.per_page = 10;
    $scope.page = 1;
    $scope.families = [];
    $scope.more = true;
    $scope.status_bar = “”;
    $scope.loadData();
    }]);

    App.factory(‘TypekitService’, [‘$http’, function ($http) {
    return {
    getTypekits: function (url) {
    return $http.jsonp(url);
    }
    }
    }]);

    /** Ajax Spinner **/
    angular.module(‘services.SharedServices’, []).config(function ($httpProvider) {
    $httpProvider.responseInterceptors.push(‘myHttpInterceptor’);
    var spinnerFunction = function (data, headersGetter) {
    $(“#loading”).show();
    return data;
    };
    $httpProvider.defaults.transformRequest.push(spinnerFunction);
    }).factory(‘myHttpInterceptor’, function ($q, $window) {
    return function (promise) {
    return promise.then(function (response) {
    $(“#loading”).hide();
    return response;
    }, function (response) {
    $(“#loading”).hide();
    return $q.reject(response);
    });
    };
    });

    /** Ajax Spinner **/

    [/cc]

    First, we initialized angular module which invokes angular root elements for this application.


    'use strict';

    var App = angular.module('PaginationApp', ['services.SharedServices']);

    MainCtrl is the main controller for this application with injection of $scope, $filter and TypeKitService. Where,
    – $scope glue data model between view and controller
    – $filter is used for formatting data displayed to the user and
    – TypeKitService is a custom angular service which will return $http promise object.

    The controller MainCtrl invoked from the view template

    We also, initialized some scope variable of controller MainCtrl. All of those scope variable are self explanatory.


    $scope.per_page = 10;
    $scope.page = 1;
    $scope.families = [];
    $scope.more = true;
    $scope.status_bar = "";
    $scope.loadData();

    TypekitService is a custom angular service and getTypekits is a method of it which takes URL as a parameter. For, different domain data source we had to use JSONP $http service provider which return a promise.$http.jsonp(url);

    Now, Typekit service is ready to providing us the JSON object with a promise callback. We used $scope.families scope variable for storing the returned JSON data object.
    $scope.families = $scope.families.concat(response.data.library.families);

    If you look carefully then you will see, we used array concatenation to concatenating returned data with $scope.families array’s existing data. In the template portion we used an ng-repeat directive to iterate through the object and come up with a view.

    [cc lang=”html”]

    {{family.id}} {{family.name}} {{family.link}}

    [/cc]

    For each and every click on the Show more button we increased the current page number and sent a request to the server.

    $scope.show_more = function () {
    $scope.page += 1;
    $scope.loadData();
    }

    In template portion we used angular ng-click directive to binding that function.

    We also made the Show more button hidden if it has no more page to paginate.

    $scope.has_more = function () {
    return $scope.more;
    }

    -which return $scope.more boolean scope variable and it’s also updated after each request.
    $scope.more = response.data.library.families.length === $scope.per_page;

    In the template part we used angular ng-show directive to binding that function.

    We also added pagination status message in both top and bottom of the records.

    $scope.status_bar = "Showing " + ($scope.families.length === 0 ? "0" : "1") + " to " + $filter('number')($scope.families.length) + " of " + $filter('number')(response.data.library.pagination.count) + " entries";

    Finally, we bind $scope.status_bar scope variable with our view template.

    {{status_bar}}

    In addition, we also added an Ajax spinner to show loading message when a request is send to the server.

    Here is a screenshot how it looks like.

    Pagination in AngularJS for Typekit API

    That wasn’t that difficult, was it ? Any questions, improvements, suggestions – you know what to do :-).

    • angularjs
    • api
    • javascript
    • pagination
    • Pagination In AngularJS
    • Show More Pagination In angularJS
    • typekit
    M. Mahbubur Rahman

    Post navigation

    Previous
    Next

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Search

    Categories

    • Agency (1)
    • AngularJS (5)
    • blog (7)
    • Branding (2)
    • Competitive Marketing Analysis (1)
    • Design (4)
    • Development (10)
    • JS (3)
    • Keywords Analysis (1)
    • Keywords Research (1)
    • Latest News (3)
    • Life at iViveLabs (2)
    • Lifestyle (1)
    • Marketing (6)
    • Onpage Optimization (1)
    • Photography (3)
    • SEO (6)
    • Travel (3)
    • Web development (2)

    Tags

    angularjs api Apple Mac Pro Desktop badmintion beautyful flowers of darjeeling beauty of darjeeling competitive audit report competitive marketing analysis filters flowers of darjeeling get more conversion get more traffic impression from pinterest internet marketing analysis report ivivelabs javascript javscript journey keywords research and analysis link assistant maowa Mawa more conversion more traffic natural beauty darjeeling New Pinterest Web Analytics organic keywords organig keywords research pagination Pagination In AngularJS panam nagar Pin My Post pinterest impression SEO seo audit report Show More Pagination In angularJS sonargaon stories The Future Of The Pro Desktop Track Pinning Activity travel typekit weather wordpress wp-cli

    Related posts

    AngularJS, blog, Web development

    AngularJS weather forecast Widget [Part 1]

    March 30, 2014 M. Mahbubur Rahman No comments yet

    Ok, we’re going to create this today.   Demo View It Online Download Source Files   And we’re going to achieve this using AngularJS Twitter Bootstrap 3 Open Weather Map API CSS3 Of course we can make similar things using only jQuery but let’s make something cool and learn some stuff on the way.   […]

    AngularJS, JS

    How to make a custom filter in AngularJS ?

    February 26, 2013 M. Mahbubur Rahman No comments yet

    AngularJS is a great open-source JavaScript Framework maintained by Google. This framework has got some compelling features. Filters is one of them for formatting the data in users locale. So we’ve decided to write a simple custom angular filter to sort clients table filtered by company. Demo View It Online Download Source Files At first, […]

    AngularJS, JS

    Making a Quick RSS Feed Reader using AngularJS

    February 20, 2013 M. Mahbubur Rahman No comments yet

    Since AngularJS is the default JavaScript Framework for Front End Application at iVive, we decided to post some angularJS articles from now on. At first, we’re coming up with a nifty minimal RSS Feed Reader with few lines of code. Demo View It Online Download Source Files You clicked the demo button, didn’t you ? […]

    Our beautiful designs open the door to a realm of limitless possibilities, where imagination knows no bounds.

    Features
    • Page builder
    • Theme options
    • Theme builder
    • Template library
    Resources
    • Support center
    • Documentation
    • Community
    • Hosting
    Follow us
    • Behance
    • Dribbble
    • Facebook
    • Instagram
    Get in touch
    • hello@company.com

    © Sierra WordPress Theme. All Rights Reserved.

    • Terms & Conditions
    • Privacy Policy