table.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. Table = function(config) {
  2. this.init(config);
  3. };
  4. Table.prototype = {
  5. init: function(config) {
  6. this.config = config;
  7. this.configSorting();
  8. },
  9. configSorting: function() {
  10. var self = this;
  11. $('#' + self.config.id + ' .sorting').each(function(index, element) {
  12. var elem = $(element);
  13. var column = elem.attr('name');
  14. $(element).click(function() {
  15. if (elem.hasClass('sorting-asc')) {
  16. self.changeOrder(column, 'DESC');
  17. } else {
  18. self.changeOrder(column, 'ASC');
  19. }
  20. });
  21. if (column == self.config.orderBy) {
  22. elem.addClass('sorting-' + (self.config.asc ? 'asc' : 'desc'));
  23. }
  24. });
  25. },
  26. changeOrder: function (orderBy, order) {
  27. var params = {
  28. 'pageNo': this.config.pageNo,
  29. 'pageSize': this.config.pageSize,
  30. 'orderBy': orderBy,
  31. 'order': order
  32. };
  33. var targetParams = {};
  34. $.extend(targetParams, this.config.params, params);
  35. var url = this.buildUrl(targetParams);
  36. location.href = url;
  37. },
  38. changePageNo: function(pageNo) {
  39. if (pageNo != this.config.pageNo) {
  40. var params = {
  41. 'pageNo': pageNo,
  42. 'pageSize': this.config.pageSize,
  43. 'orderBy': this.config.orderBy,
  44. 'order': this.config.asc ? 'ASC' : 'DESC'
  45. };
  46. var targetParams = {};
  47. $.extend(targetParams, this.config.params, params);
  48. var url = this.buildUrl(targetParams);
  49. location.href = url;
  50. }
  51. return false;
  52. },
  53. changePageSize: function(pageSize) {
  54. if (pageSize != this.config.pageSize) {
  55. var params = {
  56. 'pageNo': this.config.pageNo,
  57. 'pageSize': pageSize,
  58. 'orderBy': this.config.orderBy,
  59. 'order': this.config.asc ? 'ASC' : 'DESC'
  60. };
  61. var targetParams = {};
  62. $.extend(targetParams, this.config.params, params);
  63. var url = this.buildUrl(targetParams);
  64. location.href = url;
  65. }
  66. return false;
  67. },
  68. buildUrl: function (params) {
  69. var url = location.pathname;
  70. var separator = url.indexOf('?') == -1 ? '?' : '&';
  71. for (var key in params) {
  72. var value = params[key];
  73. if (typeof value == 'undefined' || value == null || value == '') {
  74. continue;
  75. }
  76. url += separator + key + '=' + encodeURIComponent(value);
  77. if (separator == '?') {
  78. separator = '&';
  79. }
  80. }
  81. return url;
  82. },
  83. configPagination: function(expr) {
  84. var self = this;
  85. $(expr).pagination(this.config.totalCount, {
  86. callback: function(pageIndex, jq) {
  87. self.changePageNo(pageIndex + 1);
  88. }, // 点击页数执行的回调函数
  89. current_page: this.config.pageNo - 1, // 当前页码
  90. items_per_page: this.config.pageSize //每页显示几项
  91. });
  92. },
  93. configPageInfo: function(expr) {
  94. var start = (this.config.pageNo - 1) * this.config.pageSize + 1;
  95. var end = start + this.config.resultSize - 1;
  96. // var html = "共" + this.config.totalCount + "条记录 显示" + start + "到" + end + '条记录';
  97. var html = '';
  98. if (this.config.totalCount == 0) {
  99. html = this.messages['page.empty'];
  100. } else {
  101. html = this.messages['page.info'].call(this, this.config.totalCount, start, end);
  102. }
  103. $(expr).html(html);
  104. },
  105. configPageSize: function(expr) {
  106. var self = this;
  107. $(expr).val(this.config.pageSize);
  108. $(expr).bind('change', function() {
  109. self.changePageSize($(expr).val());
  110. });
  111. },
  112. removeAll: function() {
  113. var len = $('.' + this.config.selectedItemClass + ':checked').length;
  114. if (len == 0) {
  115. $.showMessage(this.messages['select.record'], {
  116. position: 'top',
  117. size: '36',
  118. fontSize: '20px'
  119. });
  120. return false;
  121. }
  122. if (confirm(this.messages['confirm.delete'])) {
  123. $('#' + this.config.gridFormId).submit();
  124. return true;
  125. } else {
  126. return false;
  127. }
  128. },
  129. exportExcel: function() {
  130. var url = this.config.exportUrl;
  131. var params = {
  132. 'pageNo': this.config.pageNo,
  133. 'pageSize': this.config.pageSize,
  134. 'orderBy': this.config.orderBy,
  135. 'order': this.config.asc ? 'ASC' : 'DESC'
  136. };
  137. var targetParams = {};
  138. $.extend(targetParams, this.config.params, params);
  139. var separator = url.indexOf('?') == -1 ? '?' : '&';
  140. for (var key in params) {
  141. var value = params[key];
  142. if (typeof value == 'undefined' || value == null || value == '') {
  143. continue;
  144. }
  145. url += separator + key + '=' + encodeURIComponent(value);
  146. if (separator == '?') {
  147. separator = '&';
  148. }
  149. }
  150. location.href = url;
  151. },
  152. messages: {
  153. 'page.info': function() {
  154. return arguments[0] + " records, display: " + arguments[1] + " to " + arguments[2];
  155. },
  156. 'page.empty': 'No data to display',
  157. 'select.record': 'please select record to delete',
  158. 'confirm.delete': 'are you sure to delete these records?'
  159. }
  160. }