jquery.bootstrap.table.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * @author zhixin wen <wenzhixin2010@gmail.com>
  3. * @version 0.0.1
  4. * @github https://github.com/wenzhixin/bootstrap-table
  5. * @blog http://wenzhixin.net.cn
  6. */
  7. (function($) {
  8. 'use strict';
  9. function Table($el) {
  10. this.$el = $el;
  11. }
  12. Table.prototype = {
  13. constructor: Table,
  14. init: function(options, first) {
  15. if (first) {
  16. this.$div = $([
  17. '<div class="fixed-table-container">',
  18. '<div class="fixed-table-header"></div>',
  19. '<div class="fixed-table-body"></div>',
  20. '</div>'].join(''));
  21. this.$div.insertAfter(this.$el);
  22. this.$div.find('.fixed-table-body').append(this.$el);
  23. if (this.$el.height()) {
  24. this.$div.css('height', this.$el.height() + 'px');
  25. }
  26. this.$el.addClass('table table-hover');
  27. }
  28. this.options = options;
  29. this.$el.html('');
  30. this.$header = $('<thead></thead>');
  31. this.$el.append(this.$header);
  32. this.$body = $('<tbody></tbody>');
  33. this.$el.append(this.$body);
  34. this.data = [];
  35. this.initHeader();
  36. this.initData();
  37. this.initBody();
  38. },
  39. initHeader: function() {
  40. var that = this,
  41. html = ['<tr>'];
  42. this.header = {
  43. fields: [],
  44. styles: [],
  45. formatters: [],
  46. sorters: []
  47. };
  48. $.each(this.options.columns, function(i, column) {
  49. var style = column.align ? 'text-align: ' + column.align + '; ': '',
  50. order = that.options.sortOrder || column.order || 'asc';
  51. style += column.valign ? 'vertical-align: ' + column.valign + '; ' : '';
  52. that.header.fields.push(column.field);
  53. that.header.styles.push(style);
  54. that.header.formatters.push(column.formatter);
  55. that.header.sorters.push(column.sorter);
  56. style += column.width ? 'width: ' + column.width + 'px; ': '';
  57. style += column.sortable ? 'cursor: pointer;' : '';
  58. html.push('<th' +
  59. (column.sortable ? ' data-sortable="' + column.field + '"' : '') +
  60. (order ? ' data-order="' + order + '"' : '') +
  61. ' style="' + style + '">');
  62. html.push('<div class="th-inner">');
  63. html.push(column.title);
  64. if (that.options.sortName === column.field && column.sortable) {
  65. html.push(that.getCaretHtml());
  66. }
  67. html.push('</div>');
  68. html.push('</th>');
  69. });
  70. html.push('</tr>');
  71. this.$header.html(html.join(''));
  72. this.$header.find('th[data-sortable]').click(function() {
  73. that.onSort($(this));
  74. });
  75. },
  76. initData: function(data, append) {
  77. if (append) {
  78. this.data = this.data.concat(data);
  79. } else {
  80. this.data = data || this.options.data;
  81. }
  82. this.initSort();
  83. },
  84. initSort: function() {
  85. var name = this.options.sortName,
  86. order = this.options.sortOrder === 'desc' ? -1 : 1,
  87. index = $.inArray(this.options.sortName, this.header.fields);
  88. if (index !== -1) {
  89. var sorter = this.header.sorters[index];
  90. this.data.sort(function(a, b) {
  91. if (typeof sorter === 'function') {
  92. return order * sorter(a[name], b[name]);
  93. }
  94. if (a[name] === b[name]) {
  95. return 0;
  96. }
  97. if (a[name] < b[name]) {
  98. return order * -1;
  99. }
  100. return order;
  101. });
  102. }
  103. },
  104. initBody: function() {
  105. var that = this,
  106. html = [];
  107. $.each(this.data, function(i, item) {
  108. html.push('<tr>');
  109. $.each(that.header.fields, function(j, field) {
  110. var value = item[field];
  111. if (typeof that.header.formatters[j] === 'function') {
  112. value = that.header.formatters[j](value, item);
  113. }
  114. html.push('<td style="' + that.header.styles[j] + '">' + value + '</td>');
  115. });
  116. html.push('</tr>');
  117. });
  118. this.$body.html(html.join(''));
  119. this.$body.find('tr').click(function() {
  120. that.options.onClickRow(that.data[$(this).index()]);
  121. });
  122. },
  123. onSort: function($this) {
  124. this.$header.find('span.order').remove();
  125. this.options.sortName = $this.attr('data-sortable');
  126. this.options.sortOrder = $this.attr('data-order') === 'asc' ? 'desc' : 'asc';
  127. this.options.onSort(this.options.sortName, this.options.sortOrder);
  128. $this.attr('data-order', this.options.sortOrder);
  129. $this.find('.th-inner').append(this.getCaretHtml());
  130. this.initSort();
  131. this.initBody();
  132. },
  133. getCaretHtml: function() {
  134. return ['<span class="order' + (this.options.sortOrder === 'desc' ? '' : ' dropup') + '">',
  135. '<span class="caret" style="margin: 10px 5px;"></span>',
  136. '</span>'].join('');
  137. },
  138. /** public function **/
  139. load: function(data) {
  140. this.initData(data);
  141. this.initBody();
  142. },
  143. append: function(data) {
  144. this.initData(data, true);
  145. this.initBody();
  146. },
  147. mergeCells: function(options) {
  148. var row = options.index,
  149. col = $.inArray(options.field, this.header.fields),
  150. rowspan = options.rowspan || 1,
  151. colspan = options.colspan || 1,
  152. i, j,
  153. $tr = this.$body.find('tr'),
  154. $td = $tr.eq(row).find('td').eq(col);
  155. if (row < 0 || col < 0 || row >= this.data.length) {
  156. return;
  157. }
  158. for (i = row; i < row + rowspan; i++) {
  159. for (j = col; j < col + colspan; j++) {
  160. $tr.eq(i).find('td').eq(j).hide();
  161. }
  162. }
  163. $td.attr('rowspan', rowspan).attr('colspan', colspan).show();
  164. }
  165. };
  166. $.fn.bootstrapTable = function() {
  167. var option = arguments[0],
  168. args = arguments,
  169. value,
  170. allowedMethods = ['load', 'append', 'mergeCells'];
  171. this.each(function() {
  172. var $this = $(this),
  173. data = $this.data('bootstrapTable'),
  174. options = $.extend({}, $.fn.bootstrapTable.defaults, typeof option === 'object' && option);
  175. if (typeof option === 'string') {
  176. if ($.inArray(option, allowedMethods) < 0) {
  177. throw "Unknown method: " + option;
  178. }
  179. value = data[option](args[1]);
  180. } else {
  181. if (!data) {
  182. data = new Table($this);
  183. data.init(options, true);
  184. $this.data('bootstrapTable', data);
  185. } else {
  186. data.init(options, false);
  187. }
  188. }
  189. });
  190. return value ? value : this;
  191. };
  192. $.fn.bootstrapTable.defaults = {
  193. columns: [],
  194. data: [],
  195. onClickRow: function(value, row) {return false;},
  196. onSort: function(name, order) {return false;}
  197. };
  198. })(jQuery);