jquery.tablesorter.pager.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. (function($) {
  2. $.extend({
  3. tablesorterPager: new function() {
  4. function updatePageDisplay(c) {
  5. var s = $(c.cssPageDisplay,c.container).val((c.page+1) + c.seperator + c.totalPages);
  6. }
  7. function setPageSize(table,size) {
  8. var c = table.config;
  9. c.size = size;
  10. c.totalPages = Math.ceil(c.totalRows / c.size);
  11. c.pagerPositionSet = false;
  12. moveToPage(table);
  13. fixPosition(table);
  14. }
  15. function fixPosition(table) {
  16. var c = table.config;
  17. if(!c.pagerPositionSet && c.positionFixed) {
  18. var c = table.config, o = $(table);
  19. if(o.offset) {
  20. c.container.css({
  21. top: o.offset().top + o.height() + 'px',
  22. position: 'absolute'
  23. });
  24. }
  25. c.pagerPositionSet = true;
  26. }
  27. }
  28. function moveToFirstPage(table) {
  29. var c = table.config;
  30. c.page = 0;
  31. moveToPage(table);
  32. }
  33. function moveToLastPage(table) {
  34. var c = table.config;
  35. c.page = (c.totalPages-1);
  36. moveToPage(table);
  37. }
  38. function moveToNextPage(table) {
  39. var c = table.config;
  40. c.page++;
  41. if(c.page >= (c.totalPages-1)) {
  42. c.page = (c.totalPages-1);
  43. }
  44. moveToPage(table);
  45. }
  46. function moveToPrevPage(table) {
  47. var c = table.config;
  48. c.page--;
  49. if(c.page <= 0) {
  50. c.page = 0;
  51. }
  52. moveToPage(table);
  53. }
  54. function moveToPage(table) {
  55. var c = table.config;
  56. if(c.page < 0 || c.page > (c.totalPages-1)) {
  57. c.page = 0;
  58. }
  59. renderTable(table,c.rowsCopy);
  60. }
  61. function renderTable(table,rows) {
  62. var c = table.config;
  63. var l = rows.length;
  64. var s = (c.page * c.size);
  65. var e = (s + c.size);
  66. if(e > rows.length ) {
  67. e = rows.length;
  68. }
  69. var tableBody = $(table.tBodies[0]);
  70. // clear the table body
  71. $.tablesorter.clearTableBody(table);
  72. for(var i = s; i < e; i++) {
  73. //tableBody.append(rows[i]);
  74. var o = rows[i];
  75. var l = o.length;
  76. for(var j=0; j < l; j++) {
  77. tableBody[0].appendChild(o[j]);
  78. }
  79. }
  80. fixPosition(table,tableBody);
  81. $(table).trigger("applyWidgets");
  82. if( c.page >= c.totalPages ) {
  83. moveToLastPage(table);
  84. }
  85. updatePageDisplay(c);
  86. }
  87. this.appender = function(table,rows) {
  88. var c = table.config;
  89. c.rowsCopy = rows;
  90. c.totalRows = rows.length;
  91. c.totalPages = Math.ceil(c.totalRows / c.size);
  92. renderTable(table,rows);
  93. };
  94. this.defaults = {
  95. size: 10,
  96. offset: 0,
  97. page: 0,
  98. totalRows: 0,
  99. totalPages: 0,
  100. container: null,
  101. cssNext: '.next',
  102. cssPrev: '.prev',
  103. cssFirst: '.first',
  104. cssLast: '.last',
  105. cssPageDisplay: '.pagedisplay',
  106. cssPageSize: '.pagesize',
  107. seperator: "/",
  108. positionFixed: true,
  109. appender: this.appender
  110. };
  111. this.construct = function(settings) {
  112. return this.each(function() {
  113. config = $.extend(this.config, $.tablesorterPager.defaults, settings);
  114. var table = this, pager = config.container;
  115. $(this).trigger("appendCache");
  116. config.size = parseInt($(".pagesize",pager).val());
  117. $(config.cssFirst,pager).click(function() {
  118. moveToFirstPage(table);
  119. return false;
  120. });
  121. $(config.cssNext,pager).click(function() {
  122. moveToNextPage(table);
  123. return false;
  124. });
  125. $(config.cssPrev,pager).click(function() {
  126. moveToPrevPage(table);
  127. return false;
  128. });
  129. $(config.cssLast,pager).click(function() {
  130. moveToLastPage(table);
  131. return false;
  132. });
  133. $(config.cssPageSize,pager).change(function() {
  134. setPageSize(table,parseInt($(this).val()));
  135. return false;
  136. });
  137. });
  138. };
  139. }
  140. });
  141. // extend plugin scope
  142. $.fn.extend({
  143. tablesorterPager: $.tablesorterPager.construct
  144. });
  145. })(jQuery);