jquery.bootstrap.table.js 5.4 KB

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