root/trunk/paginateddatasource.js

Revision 79, 2.5 kB (checked in by mischa, 3 years ago)

move functions where they belong, in the parent class

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// Make a datasource paginatable
2// The data must be in array form
3
4// All you have to do is include this library. Then if you want to convert a datasource to be
5// paginated, simply call myDataSource.makePaginated().
6DataSource.prototype.extend({
7  makePaginated: function (opts) {
8      if (opts)
9          this.dataField = opts.dataField;
10
11    this.extend({
12
13  // returns how many pages there are
14  getPageCount: function () {
15    return Math.ceil(this.totalLength() / this.getItemsPerPage()) || 1;
16  },
17
18  // returns what page we're on
19  getCurrentPage: function () {
20    return this.currentPage;
21  },
22
23  // returns how many items are on a page
24  getItemsPerPage: function () {
25    return this.itemsPerPage;
26  },
27
28                  // set items per page
29                  setItemsPerPage: function (itemCount) {
30                    if (itemCount > 0)
31                      this.itemsPerPage = itemCount;
32
33                    this._updated();
34                  },
35
36                  // set the current page
37                  setCurrentPage: function (page) {
38                    if (page > this.getPageCount() - 1)
39                      page = this.getPageCount() - 1;
40
41                    if (page < 0)
42                      page = 0;
43
44                    this.currentPage = page;
45
46                    this._updated();
47                  },
48
49                  // go to the next page
50                  goNextPage: function () {
51                    this.setCurrentPage(this.getCurrentPage() + 1);
52
53                    this._updated();
54                  },
55
56                  // go to the previous page
57                  goPrevPage: function () {
58                    this.setCurrentPage(this.getCurrentPage() - 1);
59
60                    this._updated();
61                  }
62
63                  });
64
65    // overloaded data accessor that returns data for the current page
66  this.data = function () {
67    var theData = this.theData;
68
69    if (this.dataField && theData) {
70        theData = theData[this.dataField];
71    }
72
73    if (!theData || !theData.length)
74      return [];
75
76    var startIndex = this.getCurrentPage() * this.getItemsPerPage();
77    var endIndex = theData.fitIndex(startIndex + this.getItemsPerPage());
78
79    // slice the array
80    var newData = [];
81    var j=0;
82    for (var i = startIndex; i <= endIndex; i++)
83      newData[j++] = theData[i];
84
85    return newData;
86  };
87
88  // return all the data, not just the current page's
89  this.allData = function () {
90    return this.theData;
91  };
92
93  this.isPaginated = true;
94  this.itemsPerPage = 7;
95  this.currentPage = 0;
96  }});
Note: See TracBrowser for help on using the browser.