bootstrap-table.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /**
  2. * @author zhixin wen <wenzhixin2010@gmail.com>
  3. * version: 1.0.3
  4. */
  5. !function ($) {
  6. 'use strict';
  7. // TOOLS DEFINITION
  8. // ======================
  9. // it only does '%s', and return '' when arguments are undefined
  10. var sprintf = function(str) {
  11. var args = arguments,
  12. flag = true,
  13. i = 1;
  14. str = str.replace(/%s/g, function() {
  15. var arg = args[i++];
  16. if (typeof arg === 'undefined') {
  17. flag = false;
  18. return '';
  19. }
  20. return arg;
  21. });
  22. if (flag) {
  23. return str;
  24. }
  25. return '';
  26. };
  27. var getPropertyFromOther = function(list, from, to, value) {
  28. var result = '';
  29. $.each(list, function(i, item) {
  30. if (item[from] === value) {
  31. result = item[to];
  32. return false;
  33. }
  34. return true;
  35. });
  36. return result;
  37. };
  38. // BOOTSTRAP TABLE CLASS DEFINITION
  39. // ======================
  40. var BootstrapTable = function(el, options) {
  41. this.options = options;
  42. this.$el = $(el);
  43. this.$el_ = this.$el.clone();
  44. this.init();
  45. };
  46. BootstrapTable.DEFAULTS = {
  47. classes: 'table table-hover',
  48. height: undefined,
  49. undefinedText: '-',
  50. sortName: undefined,
  51. sortOrder: 'asc',
  52. striped: false,
  53. columns: [],
  54. data: [],
  55. method: 'get',
  56. url: undefined,
  57. queryParams: {},
  58. pagination: false,
  59. sidePagination: 'client', // client or server
  60. totalRows: 0, // server side need to set
  61. pageNumber: 1,
  62. pageSize: 10,
  63. pageList: [10, 25, 50, 100],
  64. search: false,
  65. selectItemName: 'btSelectItem',
  66. showHeader: true,
  67. showColumns: false,
  68. idField: undefined,
  69. cardView: false,
  70. clickToSelect: false,
  71. formatLoadingMessage: function() {
  72. return 'Loading, please wait…';
  73. },
  74. formatRecordsPerPage: function(pageNumber) {
  75. return sprintf('%s records per page', pageNumber);
  76. },
  77. formatShowingRows: function(pageFrom, pageTo, totalRows) {
  78. return sprintf('Showing %s to %s of %s rows', pageFrom, pageTo, totalRows);
  79. },
  80. formatSearch: function() {
  81. return 'Search';
  82. },
  83. formatNoMatches: function() {
  84. return 'No matching records found';
  85. },
  86. onClickRow: function(item) {return false;},
  87. onSort: function(name, order) {return false;},
  88. onCheck: function(row) {return false;},
  89. onUncheck: function(row) {return false;},
  90. onCheckAll: function() {return false;},
  91. onUncheckAll: function() {return false;},
  92. onLoadSuccess: function(data) {return false;},
  93. onLoadError: function(status) {return false;}
  94. };
  95. BootstrapTable.prototype.init = function() {
  96. this.initContainer();
  97. this.initHeader();
  98. this.initData();
  99. this.initToolbar();
  100. this.initPagination();
  101. this.initBody();
  102. this.initServer();
  103. };
  104. BootstrapTable.prototype.initContainer = function() {
  105. this.$container = $([
  106. '<div class="bootstrap-table">',
  107. '<div class="fixed-table-toolbar"></div>',
  108. '<div class="fixed-table-container">',
  109. '<div class="fixed-table-header"></div>',
  110. '<div class="fixed-table-body">',
  111. '<div class="fixed-table-loading">',
  112. this.options.formatLoadingMessage(),
  113. '</div>',
  114. '</div>',
  115. '<div class="fixed-table-pagination"></div>',
  116. '</div>',
  117. '</div>'].join(''));
  118. this.$container.insertAfter(this.$el);
  119. this.$container.find('.fixed-table-body').append(this.$el);
  120. this.$container.after('<div class="clearfix"></div>');
  121. this.$loading = this.$container.find('.fixed-table-loading');
  122. this.$el.addClass(this.options.classes);
  123. if (this.options.striped) {
  124. this.$el.addClass('table-striped');
  125. }
  126. };
  127. BootstrapTable.prototype.initHeader = function() {
  128. var that = this,
  129. columns = [],
  130. visibleColumns = [],
  131. html = [];
  132. this.$header = this.$el.find('thead');
  133. if (!this.$header.length) {
  134. this.$header = $('<thead></thead>').appendTo(this.$el);
  135. }
  136. if (!this.$header.find('tr').length) {
  137. this.$header.append('<tr></tr>');
  138. }
  139. this.$header.find('th').each(function() {
  140. var column = $.extend({}, {
  141. title: $(this).text()
  142. }, $(this).data());
  143. columns.push(column);
  144. });
  145. this.options.columns = $.extend(columns, this.options.columns);
  146. this.header = {
  147. fields: [],
  148. styles: [],
  149. formatters: [],
  150. sorters: []
  151. };
  152. $.each(this.options.columns, function(i, column) {
  153. var text = '',
  154. style = sprintf('text-align: %s; ', column.align) + sprintf('vertical-align: %s; ', column.valign),
  155. order = that.options.sortOrder || column.order || 'asc';
  156. column.visible = typeof column.visible === 'undefined' ? true : column.visible;
  157. if (!column.visible) {
  158. return;
  159. }
  160. visibleColumns.push(column);
  161. that.header.fields.push(column.field);
  162. that.header.styles.push(style);
  163. that.header.formatters.push(column.formatter);
  164. that.header.sorters.push(column.sorter);
  165. style = sprintf('width: %spx; ', column.checkbox || column.radio ? 36 : column.width);
  166. style += column.sortable ? 'cursor: pointer; ' : '';
  167. html.push('<th' + sprintf(' style="%s"', style) + '>');
  168. html.push('<div class="th-inner">');
  169. text = column.title;
  170. if (that.options.sortName === column.field && column.sortable) {
  171. text += that.getCaretHtml();
  172. }
  173. if (column.checkbox) {
  174. text = '<input name="btSelectAll" type="checkbox" class="checkbox" />';
  175. that.header.stateField = column.field;
  176. }
  177. if (column.radio) {
  178. text = '';
  179. that.header.stateField = column.field;
  180. }
  181. html.push(text);
  182. html.push('</div>');
  183. html.push('</th>');
  184. });
  185. this.$header.find('tr').html(html.join(''));
  186. this.$header.find('th').each(function(i) {
  187. $(this).data(visibleColumns[i]);
  188. if (visibleColumns[i].sortable) {
  189. $(this).off('click').on('click', $.proxy(that.onSort, that));
  190. }
  191. });
  192. if (!this.options.showHeader || this.options.cardView) {
  193. this.$header.hide();
  194. this.$container.find('.fixed-table-header').css('border-bottom', 'none');
  195. this.$container.find('.fixed-table-container').css('padding-top', 0);
  196. this.$loading.css('top', 0);
  197. }
  198. this.$selectAll = this.$header.find('[name="btSelectAll"]');
  199. this.$selectAll.off('click').on('click', function() {
  200. var checked = $(this).prop('checked');
  201. that[checked ? 'checkAll' : 'uncheckAll']();
  202. });
  203. };
  204. BootstrapTable.prototype.initData = function(data, append) {
  205. if (append) {
  206. this.data = this.data.concat(data);
  207. } else {
  208. this.data = data || this.options.data;
  209. }
  210. this.initSort();
  211. };
  212. BootstrapTable.prototype.initSort = function() {
  213. var name = this.options.sortName,
  214. order = this.options.sortOrder === 'desc' ? -1 : 1,
  215. index = $.inArray(this.options.sortName, this.header.fields);
  216. if (index !== -1) {
  217. var sorter = this.header.sorters[index];
  218. this.data.sort(function(a, b) {
  219. if (typeof sorter === 'function') {
  220. return order * sorter(a[name], b[name]);
  221. }
  222. if (typeof sorter === 'string') {
  223. return order * eval(sorter + '(a[name], b[name])'); // eval ?
  224. }
  225. if (a[name] === b[name]) {
  226. return 0;
  227. }
  228. if (a[name] < b[name]) {
  229. return order * -1;
  230. }
  231. return order;
  232. });
  233. }
  234. };
  235. BootstrapTable.prototype.onSort = function(event) {
  236. var $this = $(event.currentTarget);
  237. this.$header.find('span.order').remove();
  238. this.options.sortName = $this.data('field');
  239. this.options.sortOrder = $this.data('order') === 'asc' ? 'desc' : 'asc';
  240. this.options.onSort(this.options.sortName, this.options.sortOrder);
  241. $this.data('order', this.options.sortOrder);
  242. $this.find('.th-inner').append(this.getCaretHtml());
  243. this.initSort();
  244. this.initBody();
  245. };
  246. BootstrapTable.prototype.initToolbar = function() {
  247. var that = this,
  248. html = [],
  249. $pageList,
  250. $keepOpen,
  251. $search;
  252. this.$toolbar = this.$container.find('.fixed-table-toolbar');
  253. if (this.options.pagination) {
  254. html = [];
  255. html.push('<div class="page-list">');
  256. var pageNumber = [
  257. '<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">',
  258. '<span class="page-size">',
  259. this.options.pageSize,
  260. '</span>',
  261. ' <span class="caret"></span>',
  262. '</button>',
  263. '<ul class="dropdown-menu" role="menu">'];
  264. $.each(this.options.pageList, function(i, page) {
  265. var active = page === that.options.pageSize ? ' class="active"' : '';
  266. pageNumber.push(sprintf('<li%s><a href="javascript:void(0)">%s</a></li>', active, page));
  267. });
  268. pageNumber.push('</ul>');
  269. html.push(this.options.formatRecordsPerPage(pageNumber.join('')));
  270. html.push('</div>');
  271. this.$toolbar.append(html.join(''));
  272. $pageList = this.$toolbar.find('.page-list a');
  273. $pageList.off('click').on('click', $.proxy(this.onPageListChange, this));
  274. }
  275. if (this.options.showColumns) {
  276. html = [];
  277. html.push('<div class="columns pull-right keep-open">',
  278. '<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">',
  279. '<i class="glyphicon glyphicon-th icon-th"></i>',
  280. ' <span class="caret"></span>',
  281. '</button>',
  282. '<ul class="dropdown-menu" role="menu">');
  283. $.each(this.options.columns, function(i, column) {
  284. if (column.radio || column.checkbox) {
  285. return;
  286. }
  287. var checked = column.visible ? ' checked="checked"' : '';
  288. html.push(sprintf('<li>' +
  289. '<label><input type="checkbox" value="%s"%s> %s</label>' +
  290. '</li>', i, checked, column.title));
  291. });
  292. html.push('</ul>',
  293. '</div>');
  294. this.$toolbar.append(html.join(''));
  295. $keepOpen = this.$toolbar.find('.keep-open label');
  296. $keepOpen.off('click').on('click', function(event) {
  297. event.stopPropagation();
  298. var $this = $(this).find('input');
  299. that.options.columns[$this.val()].visible = $this.prop('checked');
  300. that.initHeader();
  301. that.initBody();
  302. });
  303. }
  304. if (this.options.search) {
  305. html = [];
  306. html.push(
  307. '<div class="pull-right search">',
  308. sprintf('<input class="form-control" type="text" placeholder="%s">',
  309. this.options.formatSearch()),
  310. '</div>');
  311. this.$toolbar.append(html.join(''));
  312. $search = this.$toolbar.find('.search input');
  313. $search.off('keyup').on('keyup', $.proxy(this.onSearch, this));
  314. }
  315. };
  316. BootstrapTable.prototype.onSearch = function(event) {
  317. var that = this;
  318. this.searchText = $(event.currentTarget).val();
  319. this.searchData = $.grep(this.data, function(item) {
  320. for (var key in item) {
  321. if (typeof item[key] === 'string' && item[key].indexOf(that.searchText) !== -1) {
  322. return true;
  323. }
  324. }
  325. return false;
  326. });
  327. this.resetRows();
  328. this.initPagination();
  329. this.initBody();
  330. };
  331. BootstrapTable.prototype.initPagination = function() {
  332. this.$pagination = this.$container.find('.fixed-table-pagination');
  333. if (!this.options.pagination) {
  334. return;
  335. }
  336. var that = this,
  337. html = [],
  338. i, from, to,
  339. $first, $pre,
  340. $next, $last,
  341. $number,
  342. data = this.searchText ? this.searchData : this.data;
  343. if (this.options.sidePagination === 'client') {
  344. this.options.totalRows = data.length;
  345. }
  346. this.totalPages = 0;
  347. if (this.options.totalRows) {
  348. this.totalPages = ~~((this.options.totalRows - 1) / this.options.pageSize) + 1;
  349. }
  350. if (this.totalPages > 0 && this.options.pageNumber > this.totalPages) {
  351. this.options.pageNumber = this.totalPages;
  352. }
  353. this.pageFrom = (this.options.pageNumber - 1) * this.options.pageSize + 1;
  354. this.pageTo = this.options.pageNumber * this.options.pageSize;
  355. if (this.pageTo > this.options.totalRows) {
  356. this.pageTo = this.options.totalRows;
  357. }
  358. html.push(
  359. '<div class="pull-left pagination">',
  360. '<div class="pagination-info">',
  361. this.options.formatShowingRows(this.pageFrom, this.pageTo, this.options.totalRows),
  362. '</div>',
  363. '</div>',
  364. '<div class="pull-right">',
  365. '<ul class="pagination">',
  366. '<li class="page-first"><a href="javascript:void(0)">&lt;&lt;</a></li>',
  367. '<li class="page-pre"><a href="javascript:void(0)">&lt;</a></li>');
  368. if (this.totalPages < 5) {
  369. from = 1;
  370. to = this.totalPages;
  371. } else {
  372. from = this.options.pageNumber - 2;
  373. to = from + 4;
  374. if (from < 1) {
  375. from = 1;
  376. to = 5;
  377. }
  378. if (to > this.totalPages) {
  379. to = this.totalPages;
  380. from = to - 4;
  381. }
  382. }
  383. for (i = from; i <= to; i++) {
  384. html.push('<li class="page-number' + (i === this.options.pageNumber ? ' active' : '') + '">',
  385. '<a href="javascript:void(0)">', i ,'</a>',
  386. '</li>');
  387. }
  388. html.push(
  389. '<li class="page-next"><a href="javascript:void(0)">&gt;</a></li>',
  390. '<li class="page-last"><a href="javascript:void(0)">&gt;&gt;</a></li>',
  391. '</ul>',
  392. '</div>');
  393. this.$pagination.html(html.join(''));
  394. $first = this.$pagination.find('.page-first');
  395. $pre = this.$pagination.find('.page-pre');
  396. $next = this.$pagination.find('.page-next');
  397. $last = this.$pagination.find('.page-last');
  398. $number = this.$pagination.find('.page-number');
  399. if (this.options.pageNumber <= 1) {
  400. $first.addClass('disabled');
  401. $pre.addClass('disabled');
  402. }
  403. if (this.options.pageNumber >= this.totalPages) {
  404. $next.addClass('disabled');
  405. $last.addClass('disabled');
  406. }
  407. $first.off('click').on('click', $.proxy(this.onPageFirst, this));
  408. $pre.off('click').on('click', $.proxy(this.onPagePre, this));
  409. $next.off('click').on('click', $.proxy(this.onPageNext, this));
  410. $last.off('click').on('click', $.proxy(this.onPageLast, this));
  411. $number.off('click').on('click', $.proxy(this.onPageNumber, this));
  412. };
  413. BootstrapTable.prototype.onPageListChange = function(event) {
  414. var $this = $(event.currentTarget);
  415. $this.parent().addClass('active').siblings().removeClass('active');
  416. this.options.pageSize = +$this.text();
  417. this.$toolbar.find('.page-size').text(this.options.pageSize);
  418. this.resetRows();
  419. this.initPagination();
  420. this.initBody();
  421. };
  422. BootstrapTable.prototype.onPageFirst = function() {
  423. this.options.pageNumber = 1;
  424. this.resetRows();
  425. this.initPagination();
  426. this.initBody();
  427. };
  428. BootstrapTable.prototype.onPagePre = function() {
  429. this.options.pageNumber--;
  430. this.resetRows();
  431. this.initPagination();
  432. this.initBody();
  433. };
  434. BootstrapTable.prototype.onPageNext = function() {
  435. this.options.pageNumber++;
  436. this.resetRows();
  437. this.initPagination();
  438. this.initBody();
  439. };
  440. BootstrapTable.prototype.onPageLast = function() {
  441. this.options.pageNumber = this.totalPages;
  442. this.resetRows();
  443. this.initPagination();
  444. this.initBody();
  445. };
  446. BootstrapTable.prototype.onPageNumber = function(event) {
  447. this.options.pageNumber = +$(event.currentTarget).text();
  448. this.resetRows();
  449. this.initPagination();
  450. this.initBody();
  451. };
  452. BootstrapTable.prototype.initBody = function() {
  453. var that = this,
  454. html = [],
  455. data = this.searchText ? this.searchData : this.data;
  456. this.$body = this.$el.find('tbody');
  457. if (!this.$body.length) {
  458. this.$body = $('<tbody></tbody>').appendTo(this.$el);
  459. }
  460. if (!this.options.pagination) {
  461. this.pageFrom = 1;
  462. this.pageTo = data.length;
  463. }
  464. for (var i = this.pageFrom - 1; i < this.pageTo; i++) {
  465. var item = data[i];
  466. html.push('<tr' + ' data-index="' + i + '">');
  467. if (this.options.cardView) {
  468. html.push(sprintf('<td colspan="%s">', this.header.fields.length));
  469. }
  470. $.each(this.header.fields, function(j, field) {
  471. var text = '',
  472. value = item[field],
  473. type = '',
  474. style = sprintf('style="%s"', that.header.styles[j]);
  475. if (typeof that.header.formatters[j] === 'function') {
  476. value = that.header.formatters[j](value, item);
  477. } else if (typeof that.header.formatters[j] === 'string') {
  478. value = eval(that.header.formatters[j] + '(value, item)'); // eval ?
  479. }
  480. if (that.options.columns[j].checkbox || that.options.columns[j].radio) {
  481. type = that.options.columns[j].checkbox ? 'checkbox' : type;
  482. type = that.options.columns[j].radio ? 'radio' : type;
  483. text = ['<td>',
  484. '<input class="checkbox"' +
  485. sprintf(' data-index="%s"', i) +
  486. sprintf(' name="%s"', that.options.selectItemName) +
  487. sprintf(' type="%s"', type) +
  488. sprintf(' value="%s"', item[that.options.idField]) +
  489. sprintf(' checked="%s"', value ? 'checked' : undefined) + ' />',
  490. '</td>'].join('');
  491. } else {
  492. value = typeof value === 'undefined' ? that.options.undefinedText : value;
  493. text = that.options.cardView ?
  494. ['<div class="card-view">',
  495. sprintf('<div class="title" %s>%s</div>', style,
  496. getPropertyFromOther(that.options.columns, 'field', 'title', field)),
  497. sprintf('<div class="value">%s</div>', value),
  498. '</div>'].join('') :
  499. [sprintf('<td %s>', style),
  500. value,
  501. '</td>'].join('');
  502. }
  503. html.push(text);
  504. });
  505. if (this.options.cardView) {
  506. html.push('</td>');
  507. }
  508. html.push('</tr>');
  509. }
  510. // show no records
  511. if (!html.length) {
  512. html.push('<tr class="no-records-found">',
  513. sprintf('<td colspan="%s">%s</td>', this.header.fields.length, this.options.formatNoMatches()),
  514. '</tr>');
  515. }
  516. this.$body.html(html.join(''));
  517. this.$body.find('tr').off('click').on('click', function() {
  518. that.options.onClickRow(that.data[$(this).data('index')]);
  519. if (that.options.clickToSelect) {
  520. $(this).find(sprintf('[name="%s"]', that.options.selectItemName)).trigger('click');
  521. }
  522. });
  523. this.$selectItem = this.$body.find(sprintf('[name="%s"]', this.options.selectItemName));
  524. this.$selectItem.off('click').on('click', function(event) {
  525. event.stopImmediatePropagation();
  526. var checkAll = that.$selectItem.length === that.$selectItem.filter(':checked').length,
  527. checked = $(this).prop('checked'),
  528. row = that.data[$(this).data('index')];
  529. that.$selectAll.prop('checked', checkAll);
  530. row[that.header.stateField] = checked;
  531. that.options[checked ? 'onCheck' : 'onUncheck'](row);
  532. });
  533. this.resetView();
  534. };
  535. BootstrapTable.prototype.initServer = function() {
  536. var that = this;
  537. if (!this.options.url) {
  538. return;
  539. }
  540. this.$loading.show();
  541. $.ajax({
  542. type: this.options.method,
  543. url: this.options.url,
  544. data: this.options.queryParams,
  545. contentType: 'application/json',
  546. dataType: 'json',
  547. success: function(data) {
  548. that.load(data);
  549. that.onLoadSuccess(data);
  550. },
  551. error: function(res) {
  552. that.onLoadError(res.status);
  553. },
  554. complete: function() {
  555. that.$loading.hide();
  556. }
  557. });
  558. };
  559. BootstrapTable.prototype.getCaretHtml = function() {
  560. return ['<span class="order' + (this.options.sortOrder === 'desc' ? '' : ' dropup') + '">',
  561. '<span class="caret" style="margin: 10px 5px;"></span>',
  562. '</span>'].join('');
  563. };
  564. BootstrapTable.prototype.updateRows = function(checked) {
  565. var that = this;
  566. this.$selectItem.each(function() {
  567. that.data[$(this).data('index')][that.header.stateField] = checked;
  568. });
  569. };
  570. BootstrapTable.prototype.resetRows = function() {
  571. var that = this;
  572. $.each(this.data, function(i, row) {
  573. that.$selectAll.prop('checked', false);
  574. that.$selectItem.prop('checked', false);
  575. row[that.header.stateField] = false;
  576. });
  577. };
  578. // PUBLIC FUNCTION DEFINITION
  579. // =======================
  580. BootstrapTable.prototype.resetView = function() {
  581. var header = this.header;
  582. this.$selectAll.prop('checked', this.$selectItem.length > 0 &&
  583. this.$selectItem.length === this.$selectItem.filter(':checked').length);
  584. if (this.options.height) {
  585. var toolbarHeight = +this.$toolbar.children().outerHeight(true),
  586. paginationHeight = +this.$pagination.children().outerHeight(true),
  587. height = this.options.height - toolbarHeight - paginationHeight;
  588. this.$container.find('.fixed-table-container').css('height', height + 'px');
  589. }
  590. this.$header.find('.th-inner').each(function(i) {
  591. var width = $(this).parent().width();
  592. $(this).attr('style', header.styles[i])
  593. .css('position', 'absolute')
  594. .css('width', width + 'px'); // padding: 8px
  595. });
  596. };
  597. BootstrapTable.prototype.load = function(data) {
  598. this.initData(data);
  599. this.initPagination();
  600. this.initBody();
  601. };
  602. BootstrapTable.prototype.append = function(data) {
  603. this.initData(data, true);
  604. this.initBody();
  605. };
  606. BootstrapTable.prototype.mergeCells = function(options) {
  607. var row = options.index,
  608. col = $.inArray(options.field, this.header.fields),
  609. rowspan = options.rowspan || 1,
  610. colspan = options.colspan || 1,
  611. i, j,
  612. $tr = this.$body.find('tr'),
  613. $td = $tr.eq(row).find('td').eq(col);
  614. if (row < 0 || col < 0 || row >= this.data.length) {
  615. return;
  616. }
  617. for (i = row; i < row + rowspan; i++) {
  618. for (j = col; j < col + colspan; j++) {
  619. $tr.eq(i).find('td').eq(j).hide();
  620. }
  621. }
  622. $td.attr('rowspan', rowspan).attr('colspan', colspan).show();
  623. };
  624. BootstrapTable.prototype.getSelections = function() {
  625. var that = this;
  626. return $.grep(this.data, function(row) {
  627. return row[that.header.stateField];
  628. });
  629. };
  630. BootstrapTable.prototype.checkAll = function() {
  631. this.$selectAll.prop('checked', true);
  632. this.$selectItem.prop('checked', true);
  633. this.updateRows(true);
  634. this.options.onCheckAll();
  635. };
  636. BootstrapTable.prototype.uncheckAll = function() {
  637. this.$selectAll.prop('checked', false);
  638. this.$selectItem.prop('checked', false);
  639. this.updateRows(false);
  640. this.options.onUncheckAll();
  641. };
  642. BootstrapTable.prototype.destroy = function() {
  643. this.$container.next().remove();
  644. this.$container.replaceWith(this.$el_);
  645. return this.$el_;
  646. };
  647. BootstrapTable.prototype.showLoading = function() {
  648. this.$loading.show();
  649. };
  650. BootstrapTable.prototype.hideLoading = function() {
  651. this.$loading.hide();
  652. };
  653. // BOOTSTRAP TABLE PLUGIN DEFINITION
  654. // =======================
  655. $.fn.bootstrapTable = function(option, _relatedTarget) {
  656. var allowedMethods = [
  657. 'getSelections',
  658. 'load', 'append', 'mergeCells',
  659. 'checkAll', 'uncheckAll',
  660. 'destroy', 'resetView',
  661. 'showLoading', 'hideLoading'
  662. ],
  663. value;
  664. this.each(function() {
  665. var $this = $(this),
  666. data = $this.data('bootstrap.table'),
  667. options = $.extend({}, BootstrapTable.DEFAULTS, $this.data(), typeof option === 'object' && option);
  668. if (!data) {
  669. $this.data('bootstrap.table', (data = new BootstrapTable(this, options)));
  670. }
  671. if (typeof option === 'string') {
  672. if ($.inArray(option, allowedMethods) < 0) {
  673. throw "Unknown method: " + option;
  674. }
  675. value = data[option](_relatedTarget);
  676. }
  677. });
  678. return value ? value : this;
  679. };
  680. $.fn.bootstrapTable.Constructor = BootstrapTable;
  681. $.fn.bootstrapTable.defaults = BootstrapTable.DEFAULTS;
  682. // BOOTSTRAP TABLE INIT
  683. // =======================
  684. $(function() {
  685. $('[data-toggle="table"]').bootstrapTable();
  686. });
  687. }(jQuery);