jquery.tablesorter.pager.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. function sprintf(format, etc) {
  2. var arg = arguments;
  3. var i = 1;
  4. return format.replace(/%((%)|s)/g, function (m) { return m[2] || arg[i++] })
  5. }
  6. (function($) {
  7. $.extend({
  8. tablesorterPager: new function() {
  9. function updatePageDisplay(c) {
  10. var s = $(c.cssPageDisplay,c.container).val((c.page+1) + c.seperator + c.totalPages);
  11. }
  12. function setPageSize(table,size) {
  13. var c = table.config;
  14. if (size == -1)
  15. {
  16. size = c.totalRows;
  17. }
  18. c.size = size;
  19. c.totalPages = Math.ceil(c.totalRows / c.size);
  20. c.pagerPositionSet = false;
  21. moveToPage(table);
  22. fixPosition(table);
  23. }
  24. function fixPosition(table) {
  25. var c = table.config;
  26. if(!c.pagerPositionSet && c.positionFixed) {
  27. var c = table.config, o = $(table);
  28. if(o.offset) {
  29. c.container.css({
  30. top: o.offset().top + o.height() + 'px',
  31. position: 'absolute'
  32. });
  33. }
  34. c.pagerPositionSet = true;
  35. }
  36. }
  37. function moveToFirstPage(table) {
  38. var c = table.config;
  39. c.page = 0;
  40. moveToPage(table);
  41. }
  42. function moveToLastPage(table) {
  43. var c = table.config;
  44. c.page = (c.totalPages-1);
  45. moveToPage(table);
  46. }
  47. function moveToNextPage(table) {
  48. var c = table.config;
  49. c.page++;
  50. if(c.page >= (c.totalPages-1)) {
  51. c.page = (c.totalPages-1);
  52. }
  53. moveToPage(table);
  54. }
  55. function moveToPrevPage(table) {
  56. var c = table.config;
  57. c.page--;
  58. if(c.page <= 0) {
  59. c.page = 0;
  60. }
  61. moveToPage(table);
  62. }
  63. function moveToPage(table) {
  64. var c = table.config;
  65. if(c.page < 0 || c.page > (c.totalPages-1)) {
  66. c.page = 0;
  67. }
  68. renderTable(table,c.rowsCopy);
  69. }
  70. function checkAll(table, pager, value)
  71. {
  72. // Mark all the displayed items as check or unchecked depending on the value
  73. $(table).find(':checkbox[name^=selectObj]').attr('checked', value);
  74. // Set the 'selectionMode' for the future objects to load
  75. if (value)
  76. {
  77. table.config.selectionMode = 'negative';
  78. }
  79. else
  80. {
  81. table.config.selectionMode = 'positive';
  82. }
  83. $(pager).find(':input[name=selectionMode]').val(table.config.selectionMode);
  84. // Reset the list of saved selection...
  85. resetStoredSelection(pager);
  86. updateCounter(table, pager);
  87. return true;
  88. }
  89. function resetStoredSelection(pager)
  90. {
  91. $(':input[name^=storedSelection]', pager).remove();
  92. }
  93. function storeSelection(table, pager, id, value)
  94. {
  95. var valueToStore = value;
  96. if (table.config.selectionMode == 'negative')
  97. {
  98. valueToStore = !(valueToStore);
  99. }
  100. if (valueToStore)
  101. {
  102. if (table.config.select_mode == 'single')
  103. {
  104. $(':input[name^=storedSelection]', pager).remove(); // Remove any previous selection
  105. }
  106. if ($('#'+id, pager).length ==0)
  107. {
  108. $(pager).append($('<input type="hidden" id="'+id+'" name="storedSelection[]" value="'+id+'"></input>'));
  109. }
  110. }
  111. else
  112. {
  113. if ($('#'+id, pager).length !=0)
  114. {
  115. $('#'+id, pager).remove();
  116. }
  117. }
  118. updateCounter(table, pager);
  119. }
  120. function loadSelection(table, pager)
  121. {
  122. table.config.selectionMode = $(pager).find(':input[name=selectionMode]').val();
  123. updateCounter(table, pager);
  124. }
  125. function updateCounter(table, pager)
  126. {
  127. var ex = $(':input[name^=storedSelection]', pager).length;
  128. var s = ex;
  129. if (table.config.selectionMode == 'negative')
  130. {
  131. s = table.config.totalRows - ex;
  132. }
  133. $('.selectedCount',pager).text(s);
  134. if (table.config.cssCount != '')
  135. {
  136. $(table.config.cssCount).val(s);
  137. $(table.config.cssCount).trigger('change');
  138. }
  139. }
  140. function getData(table, start, end)
  141. {
  142. if (table.ajax_request)
  143. {
  144. table.ajax_request.abort();
  145. table.ajax_request = null;
  146. }
  147. var c = table.config;
  148. var s = c.sortList[0];
  149. var s_col = null;
  150. var s_order = null;
  151. if (s != undefined)
  152. {
  153. s_col = s[0];
  154. s_order = (s[1] == 0) ? 'asc' : 'desc';
  155. }
  156. $('#loading', table.config.container).html('<img src="../images/indicator.gif" />');
  157. table.ajax_request = $.post(AddAppContext(GetAbsoluteUrlAppRoot()+"pages/ajax.render.php"),
  158. { operation: 'pagination',
  159. filter: c.filter,
  160. extra_param: c.extra_params,
  161. start: start,
  162. end: end,
  163. sort_col: s_col,
  164. sort_order: s_order,
  165. select_mode: c.select_mode,
  166. display_key: c.displayKey,
  167. display_list: c.displayList
  168. },
  169. function(data)
  170. {
  171. table.ajax_request = null; // Ajax request completed
  172. oData = $(data);
  173. var tableBody = $(table.tBodies[0]);
  174. // clear the table body
  175. $.tablesorter.clearTableBody(table);
  176. for(var i = 0; i < end-start; i++) {
  177. //tableBody.append(rows[i]);
  178. //var o = rows[i];
  179. var r = $(oData[i]);
  180. var l = r.length;
  181. for(var j=0; j < l; j++) {
  182. //tableBody[0].appendChild(r);
  183. tableBody[0].appendChild(r[j]);
  184. }
  185. }
  186. fixPosition(table,tableBody);
  187. applySelection(table);
  188. $(table).trigger("applyWidgets");
  189. if( c.page >= c.totalPages ) {
  190. moveToLastPage(table);
  191. }
  192. updatePageDisplay(c);
  193. updateCounter(table, table.config.container);
  194. renderPager(table, table.config.container);
  195. $(table).tableHover();
  196. $('#loading', table.config.container).empty();
  197. });
  198. }
  199. function applySelection(table)
  200. {
  201. var c = table.config;
  202. if (c.selectionMode == 'negative')
  203. {
  204. $(table).find(':checkbox[name^=selectObj]').attr('checked', true);
  205. }
  206. if (table.config.select_mode == 'multiple')
  207. {
  208. $(table).find(':checkbox[name^=selectObj]').each(function() {
  209. var id = parseInt(this.value, 10);
  210. if ($('#'+id, table.config.container).length > 0)
  211. {
  212. if (c.selectionMode == 'positive')
  213. {
  214. $(this).attr('checked', true);
  215. }
  216. else
  217. {
  218. $(this).attr('checked', false);
  219. }
  220. }
  221. });
  222. $(table).find(':checkbox[name^=selectObj]').change(function() {
  223. storeSelection(table, table.config.container, this.value, this.checked);
  224. });
  225. }
  226. else if (table.config.select_mode == 'single')
  227. {
  228. $(table).find('input[name^=selectObject]:radio').each(function() {
  229. var id = parseInt(this.value, 10);
  230. if ($('#'+id, table.config.container).length > 0)
  231. {
  232. if (c.selectionMode == 'positive')
  233. {
  234. $(this).attr('checked', true);
  235. }
  236. else
  237. {
  238. $(this).attr('checked', false);
  239. }
  240. }
  241. });
  242. $(table).find('input[name^=selectObject]:radio').change(function() {
  243. storeSelection(table, table.config.container, this.value, this.checked);
  244. });
  245. }
  246. }
  247. function renderPager(table, pager)
  248. {
  249. var c = table.config;
  250. var s = c.page - 2;
  251. var nb = Math.ceil(c.totalRows / c.size);
  252. if (s < 0)
  253. {
  254. s = 0;
  255. }
  256. var e = s +5;
  257. if (e > nb)
  258. {
  259. e = nb;
  260. s = e - 5;
  261. if (s < 0) s = 0;
  262. }
  263. txt = '';
  264. for(var i=s; i<e; i++)
  265. {
  266. var page = 1+i;
  267. var link = ' '+page+' ';
  268. if (i != c.page)
  269. {
  270. link = ' <span page="'+i+'" id="gotopage_'+i+'">'+page+'</span> ';
  271. }
  272. else
  273. {
  274. link = ' <span class="curr_page" page="'+i+'">'+page+'</span> ';
  275. }
  276. txt += link;
  277. }
  278. txt += '';
  279. $('#total', pager).text(c.totalRows);
  280. $('#index', pager).html(txt);
  281. for(var j=s; j<e; j++)
  282. {
  283. $('#gotopage_'+j, pager).click(function(){
  284. var idx = $(this).attr('page');
  285. table.config.page = idx;
  286. moveToPage(table);
  287. });
  288. }
  289. }
  290. function renderTable(table) {
  291. var c = table.config;
  292. //var l = rows.length;
  293. var s = (c.page * c.size);
  294. var e = (s + c.size);
  295. if(e > c.totalRows ) {
  296. e = c.totalRows;
  297. }
  298. getData(table, s, e);
  299. }
  300. this.appender = function(table,rows) {
  301. var c = table.config;
  302. if (c.totalRows == 0)
  303. {
  304. c.totalRows = rows.length;
  305. }
  306. c.totalPages = Math.ceil(c.totalRows / c.size);
  307. renderTable(table,rows);
  308. };
  309. this.defaults = {
  310. size: 10,
  311. offset: 0,
  312. page: 0,
  313. totalRows: 0,
  314. totalPages: 0,
  315. container: null,
  316. cssNext: '.next',
  317. cssPrev: '.prev',
  318. cssFirst: '.first',
  319. cssLast: '.last',
  320. cssPageDisplay: '.pagedisplay',
  321. cssPageSize: '.pagesize',
  322. cssCount: '',
  323. seperator: "/",
  324. positionFixed: false,
  325. appender: this.appender,
  326. filter: '',
  327. extra_params: '',
  328. select_mode: '',
  329. totalSelected: 0,
  330. selectionMode: 'positive',
  331. displayKey: true,
  332. displayList: []
  333. };
  334. this.construct = function(settings) {
  335. return this.each(function() {
  336. config = $.extend(this.config, $.tablesorterPager.defaults, settings);
  337. var table = this, pager = config.container;
  338. this.ajax_request = null;
  339. $(this).trigger("appendCache");
  340. config.size = parseInt($(".pagesize",pager).val());
  341. $(config.cssFirst,pager).click(function() {
  342. moveToFirstPage(table);
  343. return false;
  344. });
  345. $(config.cssNext,pager).click(function() {
  346. moveToNextPage(table);
  347. return false;
  348. });
  349. $(config.cssPrev,pager).click(function() {
  350. moveToPrevPage(table);
  351. return false;
  352. });
  353. $(config.cssLast,pager).click(function() {
  354. moveToLastPage(table);
  355. return false;
  356. });
  357. $(config.cssPageSize,pager).change(function() {
  358. setPageSize(table,parseInt($(this).val()));
  359. return false;
  360. });
  361. $(table).find(':checkbox.checkAll').removeAttr('onclick').click(function() {
  362. return checkAll(table, pager, this.checked);
  363. });
  364. $(table).bind('load_selection', function() {
  365. loadSelection(table, pager);
  366. applySelection(table);
  367. });
  368. });
  369. };
  370. }
  371. });
  372. // extend plugin scope
  373. $.fn.extend({
  374. tablesorterPager: $.tablesorterPager.construct
  375. });
  376. })(jQuery);