jquery.bootstrap.table.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. this.options = options;
  16. if (first) {
  17. this.$div = $([
  18. '<div class="fixed-table-container">',
  19. '<div class="fixed-table-header"></div>',
  20. '<div class="fixed-table-body"></div>',
  21. '</div>'].join(''));
  22. this.$div.insertAfter(this.$el);
  23. this.$div.find('.fixed-table-body').append(this.$el);
  24. if (this.options.height) {
  25. this.$div.css('height', this.options.height + 'px');
  26. }
  27. this.$el.addClass('table table-hover');
  28. }
  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 || that.options.undefinedText) + '</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. this.resetView();
  123. },
  124. onSort: function($this) {
  125. this.$header.find('span.order').remove();
  126. this.options.sortName = $this.attr('data-sortable');
  127. this.options.sortOrder = $this.attr('data-order') === 'asc' ? 'desc' : 'asc';
  128. this.options.onSort(this.options.sortName, this.options.sortOrder);
  129. $this.attr('data-order', this.options.sortOrder);
  130. $this.find('.th-inner').append(this.getCaretHtml());
  131. this.initSort();
  132. this.initBody();
  133. },
  134. getCaretHtml: function() {
  135. return ['<span class="order' + (this.options.sortOrder === 'desc' ? '' : ' dropup') + '">',
  136. '<span class="caret" style="margin: 10px 5px;"></span>',
  137. '</span>'].join('');
  138. },
  139. resetView: function() {
  140. var that = this;
  141. this.$header.find('.th-inner').each(function(i) {
  142. $(this).attr('style', that.header.styles[i])
  143. .css('width', ($(this).parent().width() - 16) + 'px'); // padding: 8px
  144. });
  145. },
  146. /** public function **/
  147. load: function(data) {
  148. this.initData(data);
  149. this.initBody();
  150. },
  151. append: function(data) {
  152. this.initData(data, true);
  153. this.initBody();
  154. },
  155. mergeCells: function(options) {
  156. var row = options.index,
  157. col = $.inArray(options.field, this.header.fields),
  158. rowspan = options.rowspan || 1,
  159. colspan = options.colspan || 1,
  160. i, j,
  161. $tr = this.$body.find('tr'),
  162. $td = $tr.eq(row).find('td').eq(col);
  163. if (row < 0 || col < 0 || row >= this.data.length) {
  164. return;
  165. }
  166. for (i = row; i < row + rowspan; i++) {
  167. for (j = col; j < col + colspan; j++) {
  168. $tr.eq(i).find('td').eq(j).hide();
  169. }
  170. }
  171. $td.attr('rowspan', rowspan).attr('colspan', colspan).show();
  172. }
  173. };
  174. $.fn.bootstrapTable = function() {
  175. var option = arguments[0],
  176. args = arguments,
  177. value,
  178. allowedMethods = ['load', 'append', 'mergeCells'];
  179. this.each(function() {
  180. var $this = $(this),
  181. data = $this.data('bootstrapTable'),
  182. options = $.extend({}, $.fn.bootstrapTable.defaults, typeof option === 'object' && option);
  183. if (typeof option === 'string') {
  184. if ($.inArray(option, allowedMethods) < 0) {
  185. throw "Unknown method: " + option;
  186. }
  187. value = data[option](args[1]);
  188. } else {
  189. if (!data) {
  190. data = new Table($this);
  191. data.init(options, true);
  192. $this.data('bootstrapTable', data);
  193. } else {
  194. data.init(options, false);
  195. }
  196. }
  197. });
  198. return value ? value : this;
  199. };
  200. $.fn.bootstrapTable.defaults = {
  201. undefinedText: '-',
  202. columns: [],
  203. data: [],
  204. onClickRow: function(value, row) {return false;},
  205. onSort: function(name, order) {return false;}
  206. };
  207. })(jQuery);