/src/main/webapp/public/js/ie.paging.js

http://thoughtsite.googlecode.com/ · JavaScript · 104 lines · 60 code · 13 blank · 31 comment · 19 complexity · ee7655d2e6f341e05b2102ef594fd830 MD5 · raw file

  1. /* Copyright 2010 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS.
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License
  14. */
  15. /**
  16. * Library for ideaexchange js Paging
  17. *
  18. * @author Abhishek
  19. */
  20. ie.Paging = {
  21. previousCss : 'previous',
  22. nextCss : 'next',
  23. selectedPageCss : 'page_sel',
  24. pageCss : 'page_no',
  25. recordPerPage : '20',
  26. pageRange : 10
  27. };
  28. /**
  29. * get HTML by providing offset
  30. */
  31. ie.Paging.getHTML = function(jsonData, callback) {
  32. var outputHtml = '';
  33. if(undefined != jsonData.previous && jsonData.previous >= 0) {
  34. outputHtml += '<a title="Previous" onclick="' + callback + '(' + jsonData.previous + ')" href="javascript:void(0);" class="' + this.previousCss + '">&laquo; Previous</a>';
  35. }
  36. if(undefined != jsonData.next && jsonData.next >= 0) {
  37. outputHtml += '<a title="Next" onclick="' + callback + '(' + jsonData.next + ')" href="javascript:void(0);" class="' + this.previousCss + '">Next &raquo;</a>';
  38. }
  39. return outputHtml;
  40. }
  41. /**
  42. * get HTML by providing offset
  43. */
  44. ie.Paging.getHTMLByOffset = function(totalRecords, startOffset) {
  45. if(0 == totalRecords % this.recordPerPage) {
  46. var totalPageCount = totalRecords/this.recordPerPage + 1;
  47. }
  48. else {
  49. var totalPageCount = totalRecords/this.recordPerPage;
  50. }
  51. selectedPage = (startOffset / this.recordPerPage) + 1;
  52. this.getHTMLByPage(totalPageCount, startOffset);
  53. }
  54. /**
  55. * get HTML by providing offset
  56. */
  57. ie.Paging.getHTMLByPage = function(totalPageCount, selectedPage) {
  58. var minRange = (this.pageRange % 2 == 0) ? (this.pageRange / 2) - 1 : (this.pageRange - 1) / 2;
  59. var maxRange = (this.pageRange % 2 == 0) ? minRange + 1 : minRange;
  60. // selectedPage will be current page
  61. var minPage = selectedPage - minRange;
  62. var maxPage = selectedPage + maxRange;
  63. minPage = (minPage < 1) ? 1 : minPage;
  64. maxPage = (maxPage < (minPage + this.pageRange - 1)) ? minPage + this.pageRange - 1 : maxPage;
  65. // totalPageCount is total pages for records
  66. if (maxPage > totalPageCount) {
  67. minPage = (minPage > 1) ? totalPageCount - this.pageRange + 1 : 1;
  68. maxPage = totalPageCount;
  69. }
  70. minPage = (minPage < 1) ? 1 : minPage;
  71. var outputHtml = '';
  72. // Add previous page link
  73. if (selectedPage != 1) {
  74. outputHtml += '<a title="Previous" href="#" class="' + this.previousCss + '">&laquo;</a>';
  75. }
  76. var pageCount = 1;
  77. for ( var counter = minPage; counter < maxPage; counter++) {
  78. if (selectedPage == counter) {
  79. outputHtml += '<a href="#" class="' + this.selectedPageCss + '">'
  80. + counter + '</a>';
  81. } else {
  82. outputHtml += '<a href="#" class="' + this.pageCss + ' '
  83. + this.pageCss + '">' + counter + '</a>';
  84. }
  85. pageCount++;
  86. }
  87. if (selectedPage < totalPageCount) {
  88. outputHtml += '<a title="Next" href="#" class="' + this.nextCss + '">&raquo;</a>';
  89. }
  90. return outputHtml;
  91. }