jquery.bootstrap.table.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. that.header.fields.push(column.field);
  39. that.header.styles.push(style);
  40. that.header.formatters.push(column.formatter);
  41. that.header.sorters.push(column.sorter);
  42. style += column.width ? 'width: ' + column.width + 'px; ': '';
  43. style += column.sortable ? 'cursor: pointer;' : '';
  44. html.push('<th' +
  45. (column.sortable ? ' data-sortable="' + column.field + '"' : '') +
  46. (order ? ' data-order="' + order + '"' : '') +
  47. ' style="' + style + '">');
  48. html.push(column.title);
  49. if (that.options.sortName === column.field && column.sortable) {
  50. html.push(that.getCaretHtml());
  51. }
  52. html.push('</th>');
  53. });
  54. html.push('</tr>');
  55. this.$header.html(html.join(''));
  56. this.$header.find('th[data-sortable]').click(function() {
  57. that.onSort($(this));
  58. });
  59. },
  60. initData: function(data, append) {
  61. if (append) {
  62. this.data = this.data.concat(data);
  63. } else {
  64. this.data = data || this.options.data;
  65. }
  66. this.initSort();
  67. },
  68. initSort: function() {
  69. var name = this.options.sortName,
  70. order = this.options.sortOrder === 'desc' ? -1 : 1,
  71. index = $.inArray(this.options.sortName, this.header.fields);
  72. if (index !== -1) {
  73. var sorter = this.header.sorters[index];
  74. this.data.sort(function(a, b) {
  75. if (typeof sorter === 'function') {
  76. return order * sorter(a[name], b[name]);
  77. }
  78. if (a[name] === b[name]) {
  79. return 0;
  80. }
  81. if (a[name] < b[name]) {
  82. return order * -1;
  83. }
  84. return order;
  85. });
  86. }
  87. },
  88. initBody: function() {
  89. var that = this,
  90. html = [];
  91. $.each(this.data, function(i, item) {
  92. html.push('<tr>');
  93. $.each(that.header.fields, function(j, field) {
  94. var value = item[field];
  95. if (typeof that.header.formatters[j] === 'function') {
  96. value = that.header.formatters[j](value, item);
  97. }
  98. html.push('<td style="' + that.header.styles[j] + '">' + value + '</td>');
  99. });
  100. html.push('</tr>');
  101. });
  102. this.$body.html(html.join(''));
  103. this.$body.find('tr').click(function() {
  104. that.options.onClickRow(that.data[$(this).index()]);
  105. });
  106. },
  107. onSort: function($this) {
  108. this.$header.find('span.order').remove();
  109. this.options.sortName = $this.attr('data-sortable');
  110. this.options.sortOrder = $this.attr('data-order') === 'asc' ? 'desc' : 'asc';
  111. this.options.onSort(this.options.sortName, this.options.sortOrder);
  112. $this.attr('data-order', this.options.sortOrder);
  113. $this.append(this.getCaretHtml());
  114. this.initSort();
  115. this.initBody();
  116. },
  117. getCaretHtml: function() {
  118. return ['<span class="order' + (this.options.sortOrder === 'desc' ? '' : ' dropup') + '">',
  119. '<span class="caret" style="margin: 8px;"></span>',
  120. '</span>'].join('');
  121. },
  122. /** public function **/
  123. load: function(data) {
  124. this.initData(data);
  125. this.initBody();
  126. },
  127. append: function(data) {
  128. this.initData(data, true);
  129. this.initBody();
  130. }
  131. };
  132. $.fn.bootstrapTable = function() {
  133. var option = arguments[0],
  134. args = arguments,
  135. value,
  136. allowedMethods = ['load', 'append'];
  137. this.each(function() {
  138. var $this = $(this),
  139. data = $this.data('bootstrapTable'),
  140. options = $.extend({}, $.fn.bootstrapTable.defaults, typeof option === 'object' && option);
  141. if (!data) {
  142. data = new Table($this, options);
  143. $this.data('bootstrapTable', data);
  144. }
  145. if (typeof option === 'string') {
  146. if ($.inArray(option, allowedMethods) < 0) {
  147. throw "Unknown method: " + option;
  148. }
  149. value = data[option](args[1]);
  150. } else {
  151. data.init();
  152. }
  153. });
  154. return value ? value : this;
  155. };
  156. $.fn.bootstrapTable.defaults = {
  157. className: 'table table-bordered table-hover',
  158. columns: [],
  159. data: [],
  160. onClickRow: function(value, row) {return false;},
  161. onSort: function(name, order) {return false;}
  162. };
  163. })(jQuery);