bootstrap-table.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.class);
  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.initBody();
  24. },
  25. initHeader: function() {
  26. var that = this,
  27. html = ['<tr>'];
  28. this.header = {
  29. fields: [],
  30. styles: [],
  31. formatters: []
  32. };
  33. $.each(this.options.columns, function(i, column) {
  34. var style = column.align ? 'text-align: ' + column.align + '; ': '';
  35. style += column.width ? 'width: ' + column.width + 'px; ': '';
  36. that.header.fields.push(column.field);
  37. that.header.styles.push(style);
  38. that.header.formatters.push(column.formatter);
  39. html.push('<th style="' + style + '">' + column.title + '</th>');
  40. });
  41. html.push('</tr>');
  42. this.$header.html(html.join(''));
  43. },
  44. initBody: function(data, append) {
  45. var that = this,
  46. html = [];
  47. $.each(data || this.options.data, function(i, item) {
  48. html.push('<tr>');
  49. $.each(that.header.fields, function(j, field) {
  50. var value = item[field];
  51. if (typeof that.header.formatters[j] === 'function') {
  52. value = that.header.formatters[j](value, item);
  53. }
  54. html.push('<td style="' + that.header.styles[j] + '">' + value + '</td>');
  55. });
  56. html.push('</tr>');
  57. });
  58. this.$body[append ? 'append' : 'html'](html.join(''));
  59. if (append) {
  60. this.data = this.data.concat(data);
  61. } else {
  62. this.data = data || this.options.data;
  63. }
  64. this.$body.find('tr').click(function() {
  65. that.options.onClickRow(that.data[$(this).index()]);
  66. });
  67. },
  68. load: function(data) {
  69. this.initBody(data);
  70. },
  71. append: function(data) {
  72. this.initBody(data, true);
  73. }
  74. };
  75. $.fn.bootstrapTable = function() {
  76. var option = arguments[0],
  77. args = arguments,
  78. value,
  79. allowedMethods = ['load', 'append'];
  80. this.each(function() {
  81. var $this = $(this),
  82. data = $this.data('bootstrapTable'),
  83. options = $.extend({}, $.fn.bootstrapTable.defaults, typeof option === 'object' && option);
  84. if (!data) {
  85. data = new Table($this, options);
  86. $this.data('bootstrapTable', data);
  87. }
  88. if (typeof option === 'string') {
  89. if ($.inArray(option, allowedMethods) < 0) {
  90. throw "Unknown method: " + option;
  91. }
  92. value = data[option](args[1]);
  93. } else {
  94. data.init();
  95. }
  96. });
  97. return value ? value : this;
  98. };
  99. $.fn.bootstrapTable.defaults = {
  100. class: 'table table-bordered table-hover',
  101. columns: [],
  102. data: [],
  103. onClickRow: function() {return false;}
  104. };
  105. })(jQuery);