Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 4th, 2012  |  syntax: None  |  size: 1.32 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. config = {
  2.     'items_per_page': 3,
  3.     'container_id': 'elements'
  4. };
  5.  
  6. function Pagination(config, objects) {
  7.     this.config = config;
  8.     this.objects = objects;
  9.     this.current_page = 1;
  10.     this.offset = this.config.items_per_page;
  11. }
  12.  
  13.  
  14. Pagination.prototype = {
  15.     getDataForPage : function () {
  16.         var data = [];
  17.         for (var i = this.current_page-1; i < this.offset; i++) {
  18.             data.push(this.objects[i]);
  19.         }
  20.  
  21.         this.current_page += this.config.items_per_page;
  22.         this.offset = (this.config.items_per_page + (this.current_page - 1));
  23.         return data;
  24.     },
  25.     next : function () {
  26.         var fragment = document.createDocumentFragment();
  27.         var data = this.getDataForPage();
  28.         var element, container;
  29.  
  30.         for (var i = 0; i < data.length; i++) {
  31.             container = document.createElement('div');
  32.  
  33.             for (attr in data[i]) {
  34.                 element = document.createElement('p');
  35.                 element.innerHTML = attr + ": " + data[i][attr].toString();
  36.                 container.appendChild(element);
  37.             }
  38.  
  39.             fragment.appendChild(container);
  40.         }
  41.  
  42.         document.getElementById(this.config.container_id).appendChild(fragment.cloneNode(true));
  43.     },
  44.     back : function () {
  45.         //incoming
  46.     }
  47. };
  48.  
  49. pagination = new Pagination(config, results);