bootstrap-table.js 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. /**
  2. * @author zhixin wen <wenzhixin2010@gmail.com>
  3. * version: 1.4.0
  4. * https://github.com/wenzhixin/bootstrap-table/
  5. */
  6. !function ($) {
  7. 'use strict';
  8. // TOOLS DEFINITION
  9. // ======================
  10. // it only does '%s', and return '' when arguments are undefined
  11. var sprintf = function(str) {
  12. var args = arguments,
  13. flag = true,
  14. i = 1;
  15. str = str.replace(/%s/g, function () {
  16. var arg = args[i++];
  17. if (typeof arg === 'undefined') {
  18. flag = false;
  19. return '';
  20. }
  21. return arg;
  22. });
  23. if (flag) {
  24. return str;
  25. }
  26. return '';
  27. };
  28. var getPropertyFromOther = function (list, from, to, value) {
  29. var result = '';
  30. $.each(list, function (i, item) {
  31. if (item[from] === value) {
  32. result = item[to];
  33. return false;
  34. }
  35. return true;
  36. });
  37. return result;
  38. };
  39. var getFieldIndex = function (columns, field) {
  40. var index = -1;
  41. $.each(columns, function (i, column) {
  42. if (column.field === field) {
  43. index = i;
  44. return false;
  45. }
  46. return true;
  47. });
  48. return index;
  49. };
  50. var getScrollBarWidth = function () {
  51. var inner = $('<p/>').addClass('fixed-table-scroll-inner'),
  52. outer = $('<div/>').addClass('fixed-table-scroll-outer'),
  53. w1, w2;
  54. outer.append(inner);
  55. $('body').append(outer);
  56. w1 = inner[0].offsetWidth;
  57. outer.css('overflow', 'scroll');
  58. w2 = inner[0].offsetWidth;
  59. if (w1 == w2) {
  60. w2 = outer[0].clientWidth;
  61. }
  62. outer.remove();
  63. return w1 - w2;
  64. };
  65. var calculateObjectValue = function (self, name, args, defaultValue) {
  66. if (typeof name === 'string') {
  67. // support obj.func1.func2
  68. var names = name.split('.');
  69. if (names.length > 1) {
  70. name = window;
  71. $.each(names, function (i, f) {
  72. name = name[f];
  73. });
  74. } else {
  75. name = window[name];
  76. }
  77. }
  78. if (typeof name === 'object') {
  79. return name;
  80. }
  81. if (typeof name === 'function') {
  82. return name.apply(self, args);
  83. }
  84. return defaultValue;
  85. };
  86. var escapeHTML = function (text) {
  87. if (typeof text == 'string') {
  88. return text
  89. .replace(/&/g, "&amp;")
  90. .replace(/</g, "&lt;")
  91. .replace(/>/g, "&gt;")
  92. .replace(/"/g, "&quot;")
  93. .replace(/'/g, "&#039;");
  94. }
  95. return text;
  96. };
  97. // BOOTSTRAP TABLE CLASS DEFINITION
  98. // ======================
  99. var BootstrapTable = function (el, options) {
  100. this.options = options;
  101. this.$el = $(el);
  102. this.$el_ = this.$el.clone();
  103. this.timeoutId_ = 0;
  104. this.init();
  105. };
  106. BootstrapTable.DEFAULTS = {
  107. classes: 'table table-hover',
  108. height: undefined,
  109. undefinedText: '-',
  110. sortName: undefined,
  111. sortOrder: 'asc',
  112. striped: false,
  113. columns: [],
  114. data: [],
  115. method: 'get',
  116. url: undefined,
  117. cache: true,
  118. contentType: 'application/json',
  119. dataType: 'json',
  120. queryParams: function (params) {return params;},
  121. queryParamsType: 'limit', // undefined
  122. responseHandler: function (res) {return res;},
  123. pagination: false,
  124. sidePagination: 'client', // client or server
  125. totalRows: 0, // server side need to set
  126. pageNumber: 1,
  127. pageSize: 10,
  128. pageList: [10, 25, 50, 100],
  129. search: false,
  130. searchAlign: 'right',
  131. selectItemName: 'btSelectItem',
  132. showHeader: true,
  133. showColumns: false,
  134. showRefresh: false,
  135. showToggle: false,
  136. smartDisplay: true,
  137. minimumCountColumns: 1,
  138. idField: undefined,
  139. cardView: false,
  140. clickToSelect: false,
  141. singleSelect: false,
  142. toolbar: undefined,
  143. toolbarAlign: 'right',
  144. checkboxHeader: true,
  145. sortable: true,
  146. maintainSelected: false,
  147. rowStyle: function (row, index) {return {};},
  148. rowAttributes: function (row, index) {return {};},
  149. formatLoadingMessage: function () {
  150. return 'Loading, please wait…';
  151. },
  152. formatRecordsPerPage: function (pageNumber) {
  153. return sprintf('%s records per page', pageNumber);
  154. },
  155. formatShowingRows: function (pageFrom, pageTo, totalRows) {
  156. return sprintf('Showing %s to %s of %s rows', pageFrom, pageTo, totalRows);
  157. },
  158. formatSearch: function () {
  159. return 'Search';
  160. },
  161. formatNoMatches: function () {
  162. return 'No matching records found';
  163. },
  164. formatRefresh: function () {
  165. return 'Refresh';
  166. },
  167. formatToggle: function () {
  168. return 'Toggle';
  169. },
  170. formatColumns: function () {
  171. return 'Columns';
  172. },
  173. onAll: function (name, args) {return false;},
  174. onClickRow: function (item, $element) {return false;},
  175. onDblClickRow: function (item, $element) {return false;},
  176. onSort: function (name, order) {return false;},
  177. onCheck: function (row) {return false;},
  178. onUncheck: function (row) {return false;},
  179. onCheckAll: function () {return false;},
  180. onUncheckAll: function () {return false;},
  181. onLoadSuccess: function (data) {return false;},
  182. onLoadError: function (status) {return false;},
  183. onColumnSwitch: function (field, checked) {return false;},
  184. onPageChange: function (number, size) {return false;},
  185. onSearch: function (text) {return false;}
  186. };
  187. BootstrapTable.COLUMN_DEFAULTS = {
  188. radio: false,
  189. checkbox: false,
  190. checkboxEnabled: true,
  191. field: undefined,
  192. title: undefined,
  193. 'class': undefined,
  194. align: undefined, // left, right, center
  195. halign: undefined, // left, right, center
  196. valign: undefined, // top, middle, bottom
  197. width: undefined,
  198. sortable: false,
  199. order: 'asc', // asc, desc
  200. visible: true,
  201. switchable: true,
  202. clickToSelect: true,
  203. formatter: undefined,
  204. events: undefined,
  205. sorter: undefined,
  206. cellStyle: undefined
  207. };
  208. BootstrapTable.EVENTS = {
  209. 'all.bs.table': 'onAll',
  210. 'click-row.bs.table': 'onClickRow',
  211. 'dbl-click-row.bs.table': 'onDblClickRow',
  212. 'sort.bs.table': 'onSort',
  213. 'check.bs.table': 'onCheck',
  214. 'uncheck.bs.table': 'onUncheck',
  215. 'check-all.bs.table': 'onCheckAll',
  216. 'uncheck-all.bs.table': 'onUncheckAll',
  217. 'load-success.bs.table': 'onLoadSuccess',
  218. 'load-error.bs.table': 'onLoadError',
  219. 'column-switch.bs.table': 'onColumnSwitch',
  220. 'page-change.bs.table': 'onPageChange',
  221. 'search.bs.table': 'onSearch'
  222. };
  223. BootstrapTable.prototype.init = function () {
  224. this.initContainer();
  225. this.initTable();
  226. this.initHeader();
  227. this.initData();
  228. this.initToolbar();
  229. this.initPagination();
  230. this.initBody();
  231. this.initServer();
  232. };
  233. BootstrapTable.prototype.initContainer = function () {
  234. this.$container = $([
  235. '<div class="bootstrap-table">',
  236. '<div class="fixed-table-toolbar"></div>',
  237. '<div class="fixed-table-container">',
  238. '<div class="fixed-table-header"><table></table></div>',
  239. '<div class="fixed-table-body">',
  240. '<div class="fixed-table-loading">',
  241. this.options.formatLoadingMessage(),
  242. '</div>',
  243. '</div>',
  244. '<div class="fixed-table-pagination"></div>',
  245. '</div>',
  246. '</div>'].join(''));
  247. this.$container.insertAfter(this.$el);
  248. this.$container.find('.fixed-table-body').append(this.$el);
  249. this.$container.after('<div class="clearfix"></div>');
  250. this.$loading = this.$container.find('.fixed-table-loading');
  251. this.$el.addClass(this.options.classes);
  252. if (this.options.striped) {
  253. this.$el.addClass('table-striped');
  254. }
  255. };
  256. BootstrapTable.prototype.initTable = function () {
  257. var that = this,
  258. columns = [],
  259. data = [];
  260. this.$header = this.$el.find('thead');
  261. if (!this.$header.length) {
  262. this.$header = $('<thead></thead>').appendTo(this.$el);
  263. }
  264. if (!this.$header.find('tr').length) {
  265. this.$header.append('<tr></tr>');
  266. }
  267. this.$header.find('th').each(function () {
  268. var column = $.extend({}, {
  269. title: $(this).html(),
  270. 'class': $(this).attr('class')
  271. }, $(this).data());
  272. columns.push(column);
  273. });
  274. this.options.columns = $.extend([], columns, this.options.columns);
  275. $.each(this.options.columns, function (i, column) {
  276. that.options.columns[i] = $.extend({}, BootstrapTable.COLUMN_DEFAULTS,
  277. {field: i}, column); // when field is undefined, use index instead
  278. });
  279. // if options.data is setting, do not process tbody data
  280. if (this.options.data.length) {
  281. return;
  282. }
  283. this.$el.find('tbody tr').each(function () {
  284. var row = {};
  285. // save tr's id and class
  286. row._id = $(this).attr('id');
  287. row._class = $(this).attr('class');
  288. $(this).find('td').each(function (i) {
  289. var field = that.options.columns[i].field;
  290. row[field] = $(this).html();
  291. // save td's id and class
  292. row['_' + field + '_id'] = $(this).attr('id');
  293. row['_' + field + '_class'] = $(this).attr('class');
  294. });
  295. data.push(row);
  296. });
  297. this.options.data = data;
  298. };
  299. BootstrapTable.prototype.initHeader = function () {
  300. var that = this,
  301. visibleColumns = [],
  302. html = [];
  303. this.header = {
  304. fields: [],
  305. styles: [],
  306. classes: [],
  307. formatters: [],
  308. events: [],
  309. sorters: [],
  310. cellStyles: [],
  311. clickToSelects: []
  312. };
  313. $.each(this.options.columns, function (i, column) {
  314. var text = '',
  315. halign = '', // header align style
  316. align = '', // body align style
  317. style = '',
  318. class_ = sprintf(' class="%s"', column['class']),
  319. order = that.options.sortOrder || column.order;
  320. if (!column.visible) {
  321. return;
  322. }
  323. halign = sprintf('text-align: %s; ', column.halign ? column.halign : column.align)
  324. align = sprintf('text-align: %s; ', column.align);
  325. style = sprintf('vertical-align: %s; ', column.valign);
  326. style += sprintf('width: %spx; ', column.checkbox || column.radio ? 36 : column.width);
  327. visibleColumns.push(column);
  328. that.header.fields.push(column.field);
  329. that.header.styles.push(align + style);
  330. that.header.classes.push(class_);
  331. that.header.formatters.push(column.formatter);
  332. that.header.events.push(column.events);
  333. that.header.sorters.push(column.sorter);
  334. that.header.cellStyles.push(column.cellStyle);
  335. that.header.clickToSelects.push(column.clickToSelect);
  336. html.push('<th',
  337. column.checkbox || column.radio ?
  338. sprintf(' class="bs-checkbox %s"', column['class'] || '') :
  339. class_,
  340. sprintf(' style="%s"', halign + style),
  341. '>');
  342. html.push(sprintf('<div class="th-inner %s">', that.options.sortable && column.sortable ?
  343. 'sortable' : ''));
  344. text = column.title;
  345. if (that.options.sortName === column.field && that.options.sortable && column.sortable) {
  346. text += that.getCaretHtml();
  347. }
  348. if (column.checkbox) {
  349. if (!that.options.singleSelect && that.options.checkboxHeader) {
  350. text = '<input name="btSelectAll" type="checkbox" />';
  351. }
  352. that.header.stateField = column.field;
  353. }
  354. if (column.radio) {
  355. text = '';
  356. that.header.stateField = column.field;
  357. that.options.singleSelect = true;
  358. }
  359. html.push(text);
  360. html.push('</div>');
  361. html.push('<div class="fht-cell"></div>');
  362. html.push('</th>');
  363. });
  364. this.$header.find('tr').html(html.join(''));
  365. this.$header.find('th').each(function (i) {
  366. $(this).data(visibleColumns[i]);
  367. });
  368. this.$container.off('click', 'th').on('click', 'th', function (event) {
  369. if (that.options.sortable && $(this).data().sortable) {
  370. that.onSort(event);
  371. }
  372. });
  373. if (!this.options.showHeader || this.options.cardView) {
  374. this.$header.hide();
  375. this.$container.find('.fixed-table-header').hide();
  376. this.$loading.css('top', 0);
  377. } else {
  378. this.$header.show();
  379. this.$container.find('.fixed-table-header').show();
  380. this.$loading.css('top', '37px');
  381. }
  382. this.$selectAll = this.$header.find('[name="btSelectAll"]');
  383. this.$container.off('click', '[name="btSelectAll"]')
  384. .on('click', '[name="btSelectAll"]', function () {
  385. var checked = $(this).prop('checked');
  386. that[checked ? 'checkAll' : 'uncheckAll']();
  387. });
  388. };
  389. BootstrapTable.prototype.initData = function (data, append) {
  390. if (append) {
  391. this.data = this.data.concat(data);
  392. } else {
  393. this.data = data || this.options.data;
  394. }
  395. this.options.data = this.data;
  396. if (this.options.sidePagination === 'server') {
  397. return;
  398. }
  399. this.initSort();
  400. };
  401. BootstrapTable.prototype.initSort = function () {
  402. var that = this,
  403. name = this.options.sortName,
  404. order = this.options.sortOrder === 'desc' ? -1 : 1,
  405. index = $.inArray(this.options.sortName, this.header.fields);
  406. if (index !== -1) {
  407. this.data.sort(function (a, b) {
  408. var aa = a[name],
  409. bb = b[name],
  410. value = calculateObjectValue(that.header, that.header.sorters[index], [aa, bb]);
  411. if (value !== undefined) {
  412. return order * value;
  413. }
  414. // Fix #161: undefined or null string sort bug.
  415. if (aa === undefined || aa === null) {
  416. aa = '';
  417. }
  418. if (aa === undefined || bb === null) {
  419. bb = '';
  420. }
  421. if (aa === bb) {
  422. return 0;
  423. }
  424. if (aa < bb) {
  425. return order * -1;
  426. }
  427. return order;
  428. });
  429. }
  430. };
  431. BootstrapTable.prototype.onSort = function (event) {
  432. var $this = $(event.currentTarget),
  433. $this_ = this.$header.find('th').eq($this.index());
  434. this.$header.add(this.$header_).find('span.order').remove();
  435. if (this.options.sortName === $this.data('field')) {
  436. this.options.sortOrder = this.options.sortOrder === 'asc' ? 'desc' : 'asc';
  437. } else {
  438. this.options.sortName = $this.data('field');
  439. this.options.sortOrder = $this.data('order') === 'asc' ? 'desc' : 'asc';
  440. }
  441. this.trigger('sort', this.options.sortName, this.options.sortOrder);
  442. $this.add($this_).data('order', this.options.sortOrder)
  443. .find('.th-inner').append(this.getCaretHtml());
  444. if (this.options.sidePagination === 'server') {
  445. this.initServer();
  446. return;
  447. }
  448. this.initSort();
  449. this.initBody();
  450. };
  451. BootstrapTable.prototype.initToolbar = function () {
  452. var that = this,
  453. html = [],
  454. timeoutId = 0,
  455. $keepOpen,
  456. $search,
  457. switchableCount = 0;
  458. this.$toolbar = this.$container.find('.fixed-table-toolbar').html('');
  459. if (typeof this.options.toolbar === 'string') {
  460. $('<div class="bars pull-left"></div>')
  461. .appendTo(this.$toolbar)
  462. .append($(this.options.toolbar));
  463. }
  464. // showColumns, showToggle, showRefresh
  465. html = ['<div class="columns columns-' + this.options.toolbarAlign + ' btn-group pull-' + this.options.toolbarAlign + '">'];
  466. if (this.options.showRefresh) {
  467. html.push(sprintf('<button class="btn btn-default" type="button" name="refresh" title="%s">',
  468. this.options.formatRefresh()),
  469. '<i class="glyphicon glyphicon-refresh icon-refresh"></i>',
  470. '</button>');
  471. }
  472. if (this.options.showToggle) {
  473. html.push(sprintf('<button class="btn btn-default" type="button" name="toggle" title="%s">',
  474. this.options.formatToggle()),
  475. '<i class="glyphicon glyphicon glyphicon-list-alt icon-list-alt"></i>',
  476. '</button>');
  477. }
  478. if (this.options.showColumns) {
  479. html.push(sprintf('<div class="keep-open %s" title="%s">',
  480. this.options.showRefresh || this.options.showToggle ? 'btn-group' : '',
  481. this.options.formatColumns()),
  482. '<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">',
  483. '<i class="glyphicon glyphicon-th icon-th"></i>',
  484. ' <span class="caret"></span>',
  485. '</button>',
  486. '<ul class="dropdown-menu" role="menu">');
  487. $.each(this.options.columns, function (i, column) {
  488. if (column.radio || column.checkbox) {
  489. return;
  490. }
  491. var checked = column.visible ? ' checked="checked"' : '';
  492. if (column.switchable) {
  493. html.push(sprintf('<li>' +
  494. '<label><input type="checkbox" data-field="%s" value="%s"%s> %s</label>' +
  495. '</li>', column.field, i, checked, column.title));
  496. switchableCount++;
  497. }
  498. });
  499. html.push('</ul>',
  500. '</div>');
  501. }
  502. html.push('</div>');
  503. if (html.length > 2) {
  504. this.$toolbar.append(html.join(''));
  505. }
  506. if (this.options.showRefresh) {
  507. this.$toolbar.find('button[name="refresh"]')
  508. .off('click').on('click', $.proxy(this.refresh, this));
  509. }
  510. if (this.options.showToggle) {
  511. this.$toolbar.find('button[name="toggle"]')
  512. .off('click').on('click', function () {
  513. that.options.cardView = !that.options.cardView;
  514. that.initHeader();
  515. that.initBody();
  516. });
  517. }
  518. if (this.options.showColumns) {
  519. $keepOpen = this.$toolbar.find('.keep-open');
  520. if (switchableCount <= this.options.minimumCountColumns) {
  521. $keepOpen.find('input').prop('disabled', true);
  522. }
  523. $keepOpen.find('li').off('click').on('click', function (event) {
  524. event.stopImmediatePropagation();
  525. });
  526. $keepOpen.find('input').off('click').on('click', function () {
  527. var $this = $(this);
  528. that.toggleColumn($this.val(), $this.prop('checked'), false);
  529. that.trigger('column-switch', $(this).data('field'), $this.prop('checked'));
  530. });
  531. }
  532. if (this.options.search) {
  533. html = [];
  534. html.push(
  535. '<div class="pull-' + this.options.searchAlign + ' search">',
  536. sprintf('<input class="form-control" type="text" placeholder="%s">',
  537. this.options.formatSearch()),
  538. '</div>');
  539. this.$toolbar.append(html.join(''));
  540. $search = this.$toolbar.find('.search input');
  541. $search.off('keyup').on('keyup', function (event) {
  542. clearTimeout(timeoutId); // doesn't matter if it's 0
  543. timeoutId = setTimeout(function () {
  544. that.onSearch(event);
  545. }, 500); // 500ms
  546. });
  547. }
  548. };
  549. BootstrapTable.prototype.onSearch = function (event) {
  550. var text = $.trim($(event.currentTarget).val());
  551. // trim search input
  552. $(event.currentTarget).val(text);
  553. if (text === this.searchText) {
  554. return;
  555. }
  556. this.searchText = text;
  557. this.options.pageNumber = 1;
  558. this.initSearch();
  559. this.updatePagination();
  560. this.trigger('search', text);
  561. };
  562. BootstrapTable.prototype.initSearch = function () {
  563. var that = this;
  564. if (this.options.sidePagination !== 'server') {
  565. var s = this.searchText && this.searchText.toLowerCase();
  566. var f = $.isEmptyObject(this.filterColumns) ? null: this.filterColumns;
  567. // Check filter
  568. this.data = f ? $.grep(this.options.data, function (item, i) {
  569. for (var key in f) {
  570. if (item[key] !== f[key]) {
  571. return false;
  572. }
  573. }
  574. return true;
  575. }) : this.options.data;
  576. this.data = s ? $.grep(this.data, function (item, i) {
  577. for (var key in item) {
  578. key = $.isNumeric(key) ? parseInt(key, 10) : key;
  579. var value = item[key];
  580. // Fix #142: search use formated data
  581. value = calculateObjectValue(that.header,
  582. that.header.formatters[$.inArray(key, that.header.fields)],
  583. [value, item, i], value);
  584. if ($.inArray(key, that.header.fields) !== -1 &&
  585. (typeof value === 'string' ||
  586. typeof value === 'number') &&
  587. (value + '').toLowerCase().indexOf(s) !== -1) {
  588. return true;
  589. }
  590. }
  591. return false;
  592. }) : this.data;
  593. }
  594. };
  595. BootstrapTable.prototype.initPagination = function () {
  596. this.$pagination = this.$container.find('.fixed-table-pagination');
  597. if (!this.options.pagination) {
  598. return;
  599. }
  600. var that = this,
  601. html = [],
  602. i, from, to,
  603. $pageList,
  604. $first, $pre,
  605. $next, $last,
  606. $number,
  607. data = this.getData();
  608. if (this.options.sidePagination !== 'server') {
  609. this.options.totalRows = data.length;
  610. }
  611. this.totalPages = 0;
  612. if (this.options.totalRows) {
  613. this.totalPages = ~~((this.options.totalRows - 1) / this.options.pageSize) + 1;
  614. }
  615. if (this.totalPages > 0 && this.options.pageNumber > this.totalPages) {
  616. this.options.pageNumber = this.totalPages;
  617. }
  618. this.pageFrom = (this.options.pageNumber - 1) * this.options.pageSize + 1;
  619. this.pageTo = this.options.pageNumber * this.options.pageSize;
  620. if (this.pageTo > this.options.totalRows) {
  621. this.pageTo = this.options.totalRows;
  622. }
  623. html.push(
  624. '<div class="pull-left pagination-detail">',
  625. '<span class="pagination-info">',
  626. this.options.formatShowingRows(this.pageFrom, this.pageTo, this.options.totalRows),
  627. '</span>');
  628. html.push('<span class="page-list">');
  629. var pageNumber = [
  630. '<span class="btn-group dropup">',
  631. '<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">',
  632. '<span class="page-size">',
  633. this.options.pageSize,
  634. '</span>',
  635. ' <span class="caret"></span>',
  636. '</button>',
  637. '<ul class="dropdown-menu" role="menu">'],
  638. pageList = this.options.pageList;
  639. if (typeof this.options.pageList === 'string') {
  640. var list = this.options.pageList.slice(1, -1).replace(/ /g, '').split(',');
  641. pageList = [];
  642. $.each(list, function (i, value) {
  643. pageList.push(+value);
  644. });
  645. }
  646. $.each(pageList, function (i, page) {
  647. if (!that.options.smartDisplay || that.options.totalRows >= page || i === 0) {
  648. var active = page === that.options.pageSize ? ' class="active"' : '';
  649. pageNumber.push(sprintf('<li%s><a href="javascript:void(0)">%s</a></li>', active, page));
  650. }
  651. });
  652. pageNumber.push('</ul></span>');
  653. html.push(this.options.formatRecordsPerPage(pageNumber.join('')));
  654. html.push('</span>');
  655. html.push('</div>',
  656. '<div class="pull-right pagination">',
  657. '<ul class="pagination">',
  658. '<li class="page-first"><a href="javascript:void(0)">&lt;&lt;</a></li>',
  659. '<li class="page-pre"><a href="javascript:void(0)">&lt;</a></li>');
  660. if (this.totalPages < 5) {
  661. from = 1;
  662. to = this.totalPages;
  663. } else {
  664. from = this.options.pageNumber - 2;
  665. to = from + 4;
  666. if (from < 1) {
  667. from = 1;
  668. to = 5;
  669. }
  670. if (to > this.totalPages) {
  671. to = this.totalPages;
  672. from = to - 4;
  673. }
  674. }
  675. for (i = from; i <= to; i++) {
  676. html.push('<li class="page-number' + (i === this.options.pageNumber ? ' active disabled' : '') + '">',
  677. '<a href="javascript:void(0)">', i ,'</a>',
  678. '</li>');
  679. }
  680. html.push(
  681. '<li class="page-next"><a href="javascript:void(0)">&gt;</a></li>',
  682. '<li class="page-last"><a href="javascript:void(0)">&gt;&gt;</a></li>',
  683. '</ul>',
  684. '</div>');
  685. this.$pagination.html(html.join(''));
  686. $pageList = this.$pagination.find('.page-list a');
  687. $first = this.$pagination.find('.page-first');
  688. $pre = this.$pagination.find('.page-pre');
  689. $next = this.$pagination.find('.page-next');
  690. $last = this.$pagination.find('.page-last');
  691. $number = this.$pagination.find('.page-number');
  692. if (this.options.pageNumber <= 1) {
  693. $first.addClass('disabled');
  694. $pre.addClass('disabled');
  695. }
  696. if (this.options.pageNumber >= this.totalPages) {
  697. $next.addClass('disabled');
  698. $last.addClass('disabled');
  699. }
  700. if (this.options.smartDisplay) {
  701. if (this.totalPages <= 1) {
  702. this.$pagination.find('div.pagination').hide();
  703. }
  704. if (this.options.pageList.length < 2 || this.options.totalRows <= this.options.pageList[1]) {
  705. this.$pagination.find('span.page-list').hide();
  706. }
  707. // when data is empty, hide the pagination
  708. this.$pagination[this.getData().length ? 'show' : 'hide']();
  709. }
  710. $pageList.off('click').on('click', $.proxy(this.onPageListChange, this));
  711. $first.off('click').on('click', $.proxy(this.onPageFirst, this));
  712. $pre.off('click').on('click', $.proxy(this.onPagePre, this));
  713. $next.off('click').on('click', $.proxy(this.onPageNext, this));
  714. $last.off('click').on('click', $.proxy(this.onPageLast, this));
  715. $number.off('click').on('click', $.proxy(this.onPageNumber, this));
  716. };
  717. BootstrapTable.prototype.updatePagination = function (event) {
  718. // Fix #171: IE disabled button can be clicked bug.
  719. if (event && $(event.currentTarget).hasClass('disabled')) {
  720. return;
  721. }
  722. if (!this.options.maintainSelected) {
  723. this.resetRows();
  724. }
  725. this.initPagination();
  726. if (this.options.sidePagination === 'server') {
  727. this.initServer();
  728. } else {
  729. this.initBody();
  730. }
  731. this.trigger('page-change', this.options.pageSize, this.options.pageNumber);
  732. };
  733. BootstrapTable.prototype.onPageListChange = function (event) {
  734. var $this = $(event.currentTarget);
  735. $this.parent().addClass('active').siblings().removeClass('active');
  736. this.options.pageSize = +$this.text();
  737. this.$toolbar.find('.page-size').text(this.options.pageSize);
  738. this.updatePagination(event);
  739. };
  740. BootstrapTable.prototype.onPageFirst = function (event) {
  741. this.options.pageNumber = 1;
  742. this.updatePagination(event);
  743. };
  744. BootstrapTable.prototype.onPagePre = function (event) {
  745. this.options.pageNumber--;
  746. this.updatePagination(event);
  747. };
  748. BootstrapTable.prototype.onPageNext = function (event) {
  749. this.options.pageNumber++;
  750. this.updatePagination(event);
  751. };
  752. BootstrapTable.prototype.onPageLast = function (event) {
  753. this.options.pageNumber = this.totalPages;
  754. this.updatePagination(event);
  755. };
  756. BootstrapTable.prototype.onPageNumber = function (event) {
  757. if (this.options.pageNumber === +$(event.currentTarget).text()) {
  758. return;
  759. }
  760. this.options.pageNumber = +$(event.currentTarget).text();
  761. this.updatePagination(event);
  762. };
  763. BootstrapTable.prototype.initBody = function (fixedScroll) {
  764. var that = this,
  765. html = [],
  766. data = this.getData();
  767. this.$body = this.$el.find('tbody');
  768. if (!this.$body.length) {
  769. this.$body = $('<tbody></tbody>').appendTo(this.$el);
  770. }
  771. if (this.options.sidePagination === 'server') {
  772. data = this.data;
  773. }
  774. if (!this.options.pagination || this.options.sidePagination === 'server') {
  775. this.pageFrom = 1;
  776. this.pageTo = data.length;
  777. }
  778. for (var i = this.pageFrom - 1; i < this.pageTo; i++) {
  779. var item = data[i],
  780. style = {},
  781. csses = [],
  782. attributes = {},
  783. htmlAttributes = [];
  784. style = calculateObjectValue(this.options, this.options.rowStyle, [item, i], style);
  785. if (style && style.css) {
  786. for (var key in style.css) {
  787. csses.push(key + ': ' + style.css[key]);
  788. }
  789. }
  790. attributes = calculateObjectValue(this.options,
  791. this.options.rowAttributes, [item, i], attributes);
  792. if (attributes) {
  793. for (var key in attributes) {
  794. htmlAttributes.push(sprintf('%s="%s"', key, escapeHTML(attributes[key])));
  795. }
  796. }
  797. html.push('<tr',
  798. sprintf(' %s', htmlAttributes.join(' ')),
  799. sprintf(' id="%s"', $.isArray(item) ? undefined : item._id),
  800. sprintf(' class="%s"', style.classes || $.isArray(item) ? undefined : item._class),
  801. sprintf(' data-index="%s"', i),
  802. '>'
  803. );
  804. if (this.options.cardView) {
  805. html.push(sprintf('<td colspan="%s">', this.header.fields.length));
  806. }
  807. $.each(this.header.fields, function (j, field) {
  808. var text = '',
  809. value = item[field],
  810. type = '',
  811. cellStyle = {},
  812. id_ = '',
  813. class_ = that.header.classes[j];
  814. style = sprintf('style="%s"', csses.concat(that.header.styles[j]).join('; '));
  815. value = calculateObjectValue(that.header,
  816. that.header.formatters[j], [value, item, i], value);
  817. // handle td's id and class
  818. if (item['_' + field + '_id']) {
  819. id_ = sprintf(' id="%s"', item['_' + field + '_id']);
  820. }
  821. if (item['_' + field + '_class']) {
  822. class_ = sprintf(' class="%s"', item['_' + field + '_class']);
  823. }
  824. cellStyle = calculateObjectValue(that.header,
  825. that.header.cellStyles[j], [value, item, i], cellStyle);
  826. if (cellStyle.classes) {
  827. class_ = sprintf(' class="%s"', cellStyle.classes);
  828. }
  829. if (cellStyle.css) {
  830. csses = [];
  831. for (var key in cellStyle.css) {
  832. csses.push(key + ': ' + cellStyle.css[key]);
  833. }
  834. style = sprintf('style="%s"', csses.concat(that.header.styles[j]).join('; '));
  835. }
  836. if (that.options.columns[j].checkbox || that.options.columns[j].radio) {
  837. //if card view mode bypass
  838. if (that.options.cardView) {
  839. return true;
  840. }
  841. type = that.options.columns[j].checkbox ? 'checkbox' : type;
  842. type = that.options.columns[j].radio ? 'radio' : type;
  843. text = ['<td class="bs-checkbox">',
  844. '<input' +
  845. sprintf(' data-index="%s"', i) +
  846. sprintf(' name="%s"', that.options.selectItemName) +
  847. sprintf(' type="%s"', type) +
  848. sprintf(' value="%s"', item[that.options.idField]) +
  849. sprintf(' checked="%s"', value === true ||
  850. (value && value.checked) ? 'checked' : undefined) +
  851. sprintf(' disabled="%s"', !that.options.columns[j].checkboxEnabled ||
  852. (value && value.disabled) ? 'disabled' : undefined) +
  853. ' />',
  854. '</td>'].join('');
  855. } else {
  856. value = typeof value === 'undefined' || value === null ?
  857. that.options.undefinedText : value;
  858. text = that.options.cardView ?
  859. ['<div class="card-view">',
  860. that.options.showHeader ? sprintf('<span class="title" %s>%s</span>', style,
  861. getPropertyFromOther(that.options.columns, 'field', 'title', field)) : '',
  862. sprintf('<span class="value">%s</span>', value),
  863. '</div>'].join('') :
  864. [sprintf('<td%s %s %s>', id_, class_, style),
  865. value,
  866. '</td>'].join('');
  867. // Hide empty data on Card view when smartDisplay is set to true.
  868. if (that.options.cardView && that.options.smartDisplay && value === '') {
  869. text = '';
  870. }
  871. }
  872. html.push(text);
  873. });
  874. if (this.options.cardView) {
  875. html.push('</td>');
  876. }
  877. html.push('</tr>');
  878. }
  879. // show no records
  880. if (!html.length) {
  881. html.push('<tr class="no-records-found">',
  882. sprintf('<td colspan="%s">%s</td>', this.header.fields.length, this.options.formatNoMatches()),
  883. '</tr>');
  884. }
  885. this.$body.html(html.join(''));
  886. if (!fixedScroll) {
  887. this.$container.find('.fixed-table-body').scrollTop(0);
  888. }
  889. // click to select by column
  890. this.$body.find('> tr > td').off('click').on('click', function () {
  891. var $tr = $(this).parent();
  892. that.trigger('click-row', that.data[$tr.data('index')], $tr);
  893. // if click to select - then trigger the checkbox/radio click
  894. if (that.options.clickToSelect) {
  895. if (that.header.clickToSelects[$tr.children().index($(this))]) {
  896. $tr.find(sprintf('[name="%s"]',
  897. that.options.selectItemName))[0].click(); // #144: .trigger('click') bug
  898. }
  899. }
  900. });
  901. this.$body.find('tr').off('dblclick').on('dblclick', function () {
  902. that.trigger('dbl-click-row', that.data[$(this).data('index')], $(this));
  903. });
  904. this.$selectItem = this.$body.find(sprintf('[name="%s"]', this.options.selectItemName));
  905. this.$selectItem.off('click').on('click', function (event) {
  906. event.stopImmediatePropagation();
  907. var checkAll = that.$selectItem.filter(':enabled').length ===
  908. that.$selectItem.filter(':enabled').filter(':checked').length,
  909. checked = $(this).prop('checked'),
  910. row = that.data[$(this).data('index')];
  911. that.$selectAll.add(that.$selectAll_).prop('checked', checkAll);
  912. row[that.header.stateField] = checked;
  913. that.trigger(checked ? 'check' : 'uncheck', row);
  914. if (that.options.singleSelect) {
  915. that.$selectItem.not(this).each(function () {
  916. that.data[$(this).data('index')][that.header.stateField] = false;
  917. });
  918. that.$selectItem.filter(':checked').not(this).prop('checked', false);
  919. }
  920. that.updateSelected();
  921. });
  922. $.each(this.header.events, function (i, events) {
  923. if (!events) {
  924. return;
  925. }
  926. // fix bug, if events is defined with namespace
  927. if (typeof events === 'string') {
  928. events = calculateObjectValue(null, events);
  929. }
  930. for (var key in events) {
  931. that.$body.find('tr').each(function () {
  932. var $tr = $(this),
  933. $td = $tr.find('td').eq(i),
  934. index = key.indexOf(' '),
  935. name = key.substring(0, index),
  936. el = key.substring(index + 1),
  937. func = events[key];
  938. $td.find(el).off(name).on(name, function (e) {
  939. var index = $tr.data('index'),
  940. row = that.data[index],
  941. value = row[that.header.fields[i]];
  942. func(e, value, row, index);
  943. });
  944. });
  945. }
  946. });
  947. this.updateSelected();
  948. this.resetView();
  949. };
  950. BootstrapTable.prototype.initServer = function (silent) {
  951. var that = this,
  952. data = {},
  953. params = {
  954. pageSize: this.options.pageSize,
  955. pageNumber: this.options.pageNumber,
  956. searchText: this.searchText,
  957. sortName: this.options.sortName,
  958. sortOrder: this.options.sortOrder
  959. };
  960. if (!this.options.url) {
  961. return;
  962. }
  963. if (this.options.queryParamsType === 'limit') {
  964. params = {
  965. limit: params.pageSize,
  966. offset: params.pageSize * (params.pageNumber - 1),
  967. search: params.searchText,
  968. sort: params.sortName,
  969. order: params.sortOrder
  970. };
  971. }
  972. data = calculateObjectValue(this.options, this.options.queryParams, [params], data);
  973. // false to stop request
  974. if (data === false) {
  975. return;
  976. }
  977. if (!silent) {
  978. this.$loading.show();
  979. }
  980. $.ajax({
  981. type: this.options.method,
  982. url: this.options.url,
  983. data: data,
  984. cache: this.options.cache,
  985. contentType: this.options.contentType,
  986. dataType: this.options.dataType,
  987. success: function (res) {
  988. res = calculateObjectValue(that.options, that.options.responseHandler, [res], res);
  989. var data = res;
  990. if (that.options.sidePagination === 'server') {
  991. that.options.totalRows = res.total;
  992. data = res.rows;
  993. }
  994. that.load(data);
  995. that.trigger('load-success', data);
  996. },
  997. error: function (res) {
  998. that.trigger('load-error', res.status);
  999. },
  1000. complete: function () {
  1001. if (!silent) {
  1002. that.$loading.hide();
  1003. }
  1004. }
  1005. });
  1006. };
  1007. BootstrapTable.prototype.getCaretHtml = function () {
  1008. return ['<span class="order' + (this.options.sortOrder === 'desc' ? '' : ' dropup') + '">',
  1009. '<span class="caret" style="margin: 10px 5px;"></span>',
  1010. '</span>'].join('');
  1011. };
  1012. BootstrapTable.prototype.updateSelected = function () {
  1013. this.$selectItem.each(function () {
  1014. $(this).parents('tr')[$(this).prop('checked') ? 'addClass' : 'removeClass']('selected');
  1015. });
  1016. };
  1017. BootstrapTable.prototype.updateRows = function (checked) {
  1018. var that = this;
  1019. this.$selectItem.each(function () {
  1020. that.data[$(this).data('index')][that.header.stateField] = checked;
  1021. });
  1022. };
  1023. BootstrapTable.prototype.resetRows = function () {
  1024. var that = this;
  1025. $.each(this.data, function (i, row) {
  1026. that.$selectAll.prop('checked', false);
  1027. that.$selectItem.prop('checked', false);
  1028. row[that.header.stateField] = false;
  1029. });
  1030. };
  1031. BootstrapTable.prototype.trigger = function (name) {
  1032. var args = Array.prototype.slice.call(arguments, 1);
  1033. name += '.bs.table';
  1034. this.options[BootstrapTable.EVENTS[name]].apply(this.options, args);
  1035. this.$el.trigger($.Event(name), args);
  1036. this.options.onAll(name, args);
  1037. this.$el.trigger($.Event('all.bs.table'), [name, args]);
  1038. };
  1039. BootstrapTable.prototype.resetHeader = function () {
  1040. var that = this,
  1041. $fixedHeader = this.$container.find('.fixed-table-header'),
  1042. $fixedBody = this.$container.find('.fixed-table-body'),
  1043. scrollWidth = this.$el.width() > $fixedBody.width() ? getScrollBarWidth() : 0;
  1044. // fix #61: the hidden table reset header bug.
  1045. if (this.$el.is(':hidden')) {
  1046. clearTimeout(this.timeoutId_); // doesn't matter if it's 0
  1047. this.timeoutId_ = setTimeout($.proxy(this.resetHeader, this), 100); // 100ms
  1048. return;
  1049. }
  1050. this.$header_ = this.$header.clone(true, true);
  1051. this.$selectAll_ = this.$header_.find('[name="btSelectAll"]');
  1052. // fix bug: get $el.css('width') error sometime (height = 500)
  1053. setTimeout(function () {
  1054. $fixedHeader.css({
  1055. 'height': '37px',
  1056. 'border-bottom': '1px solid #dddddd',
  1057. 'margin-right': scrollWidth
  1058. }).find('table').css('width', that.$el.css('width'))
  1059. .html('').attr('class', that.$el.attr('class'))
  1060. .append(that.$header_);
  1061. // fix bug: $.data() is not working as expected after $.append()
  1062. that.$header.find('th').each(function (i) {
  1063. that.$header_.find('th').eq(i).data($(this).data());
  1064. });
  1065. that.$body.find('tr:first-child:not(.no-records-found) > *').each(function(i) {
  1066. that.$header_.find('div.fht-cell').eq(i).width($(this).innerWidth());
  1067. });
  1068. that.$el.css('margin-top', -that.$header.height());
  1069. // horizontal scroll event
  1070. $fixedBody.off('scroll').on('scroll', function () {
  1071. $fixedHeader.scrollLeft($(this).scrollLeft());
  1072. });
  1073. });
  1074. };
  1075. BootstrapTable.prototype.toggleColumn = function (index, checked, needUpdate) {
  1076. if (index === -1) {
  1077. return;
  1078. }
  1079. this.options.columns[index].visible = checked;
  1080. this.initHeader();
  1081. this.initSearch();
  1082. this.initPagination();
  1083. this.initBody();
  1084. if (this.options.showColumns) {
  1085. var $items = this.$toolbar.find('.keep-open input').prop('disabled', false);
  1086. if (needUpdate) {
  1087. $items.filter(sprintf('[value="%s"]', index)).prop('checked', checked);
  1088. }
  1089. if ($items.filter(':checked').length <= this.options.minimumCountColumns) {
  1090. $items.filter(':checked').prop('disabled', true);
  1091. }
  1092. }
  1093. };
  1094. // PUBLIC FUNCTION DEFINITION
  1095. // =======================
  1096. BootstrapTable.prototype.resetView = function (params) {
  1097. var that = this,
  1098. header = this.header;
  1099. if (params && params.height) {
  1100. this.options.height = params.height;
  1101. }
  1102. this.$selectAll.prop('checked', this.$selectItem.length > 0 &&
  1103. this.$selectItem.length === this.$selectItem.filter(':checked').length);
  1104. if (this.options.height) {
  1105. var toolbarHeight = +this.$toolbar.children().outerHeight(true),
  1106. paginationHeight = +this.$pagination.children().outerHeight(true),
  1107. height = this.options.height - toolbarHeight - paginationHeight;
  1108. this.$container.find('.fixed-table-container').css('height', height + 'px');
  1109. }
  1110. if (this.options.cardView) {
  1111. // remove the element css
  1112. that.$el.css('margin-top', '0');
  1113. that.$container.find('.fixed-table-container').css('padding-bottom', '0');
  1114. return;
  1115. }
  1116. if (this.options.showHeader && this.options.height) {
  1117. this.resetHeader();
  1118. }
  1119. if (this.options.height && this.options.showHeader) {
  1120. this.$container.find('.fixed-table-container').css('padding-bottom', '37px');
  1121. }
  1122. };
  1123. BootstrapTable.prototype.getData = function () {
  1124. return (this.searchText || !$.isEmptyObject(this.filterColumns)) ? this.data : this.options.data;
  1125. };
  1126. BootstrapTable.prototype.load = function (data) {
  1127. this.initData(data);
  1128. this.initSearch();
  1129. this.initPagination();
  1130. this.initBody();
  1131. };
  1132. BootstrapTable.prototype.append = function (data) {
  1133. this.initData(data, true);
  1134. this.initSearch();
  1135. this.initPagination();
  1136. this.initBody(true);
  1137. };
  1138. BootstrapTable.prototype.remove = function (params) {
  1139. var len = this.options.data.length,
  1140. i, row;
  1141. if (!params.hasOwnProperty('field') || !params.hasOwnProperty('values')) {
  1142. return;
  1143. }
  1144. for (i = len - 1; i >= 0; i--) {
  1145. row = this.options.data[i];
  1146. if (!row.hasOwnProperty(params.field)) {
  1147. return;
  1148. }
  1149. if ($.inArray(row[params.field], params.values) !== -1) {
  1150. this.options.data.splice(i, 1);
  1151. }
  1152. }
  1153. if (len === this.options.data.length) {
  1154. return;
  1155. }
  1156. this.initSearch();
  1157. this.initPagination();
  1158. this.initBody(true);
  1159. };
  1160. BootstrapTable.prototype.updateRow = function (params) {
  1161. if (!params.hasOwnProperty('index') || !params.hasOwnProperty('row')) {
  1162. return;
  1163. }
  1164. $.extend(this.data[params.index], params.row);
  1165. this.initBody(true);
  1166. };
  1167. BootstrapTable.prototype.mergeCells = function (options) {
  1168. var row = options.index,
  1169. col = $.inArray(options.field, this.header.fields),
  1170. rowspan = options.rowspan || 1,
  1171. colspan = options.colspan || 1,
  1172. i, j,
  1173. $tr = this.$body.find('tr'),
  1174. $td = $tr.eq(row).find('td').eq(col);
  1175. if (row < 0 || col < 0 || row >= this.data.length) {
  1176. return;
  1177. }
  1178. for (i = row; i < row + rowspan; i++) {
  1179. for (j = col; j < col + colspan; j++) {
  1180. $tr.eq(i).find('td').eq(j).hide();
  1181. }
  1182. }
  1183. $td.attr('rowspan', rowspan).attr('colspan', colspan).show();
  1184. };
  1185. BootstrapTable.prototype.getSelections = function () {
  1186. var that = this;
  1187. return $.grep(this.data, function (row) {
  1188. return row[that.header.stateField];
  1189. });
  1190. };
  1191. BootstrapTable.prototype.checkAll = function () {
  1192. this.$selectAll.add(this.$selectAll_).prop('checked', true);
  1193. this.$selectItem.filter(':enabled').prop('checked', true);
  1194. this.updateRows(true);
  1195. this.updateSelected();
  1196. this.trigger('check-all');
  1197. };
  1198. BootstrapTable.prototype.uncheckAll = function () {
  1199. this.$selectAll.add(this.$selectAll_).prop('checked', false);
  1200. this.$selectItem.filter(':enabled').prop('checked', false);
  1201. this.updateRows(false);
  1202. this.updateSelected();
  1203. this.trigger('uncheck-all');
  1204. };
  1205. BootstrapTable.prototype.destroy = function () {
  1206. this.$el.insertBefore(this.$container);
  1207. $(this.options.toolbar).insertBefore(this.$el);
  1208. this.$container.next().remove();
  1209. this.$container.remove();
  1210. this.$el.html(this.$el_.html())
  1211. .attr('class', this.$el_.attr('class') || ''); // reset the class
  1212. };
  1213. BootstrapTable.prototype.showLoading = function () {
  1214. this.$loading.show();
  1215. };
  1216. BootstrapTable.prototype.hideLoading = function () {
  1217. this.$loading.hide();
  1218. };
  1219. BootstrapTable.prototype.refresh = function (params) {
  1220. if (params && params.url) {
  1221. this.options.url = params.url;
  1222. this.options.pageNumber = 1;
  1223. }
  1224. this.initServer(params && params.silent);
  1225. };
  1226. BootstrapTable.prototype.showColumn = function (field) {
  1227. this.toggleColumn(getFieldIndex(this.options.columns, field), true, true);
  1228. };
  1229. BootstrapTable.prototype.hideColumn = function (field) {
  1230. this.toggleColumn(getFieldIndex(this.options.columns, field), false, true);
  1231. };
  1232. BootstrapTable.prototype.filterBy = function (columns) {
  1233. this.filterColumns = $.isEmptyObject(columns) ? {}: columns;
  1234. this.options.pageNumber = 1;
  1235. this.initSearch();
  1236. this.updatePagination();
  1237. };
  1238. // BOOTSTRAP TABLE PLUGIN DEFINITION
  1239. // =======================
  1240. var allowedMethods = [
  1241. 'getSelections', 'getData',
  1242. 'load', 'append', 'remove',
  1243. 'updateRow',
  1244. 'mergeCells',
  1245. 'checkAll', 'uncheckAll',
  1246. 'refresh',
  1247. 'resetView',
  1248. 'destroy',
  1249. 'showLoading', 'hideLoading',
  1250. 'showColumn', 'hideColumn',
  1251. 'filterBy'
  1252. ];
  1253. $.fn.bootstrapTable = function (option, _relatedTarget) {
  1254. var value;
  1255. this.each(function () {
  1256. var $this = $(this),
  1257. data = $this.data('bootstrap.table'),
  1258. options = $.extend({}, BootstrapTable.DEFAULTS, $this.data(),
  1259. typeof option === 'object' && option);
  1260. if (typeof option === 'string') {
  1261. if ($.inArray(option, allowedMethods) < 0) {
  1262. throw "Unknown method: " + option;
  1263. }
  1264. if (!data) {
  1265. return;
  1266. }
  1267. value = data[option](_relatedTarget);
  1268. if (option === 'destroy') {
  1269. $this.removeData('bootstrap.table');
  1270. }
  1271. }
  1272. if (!data) {
  1273. $this.data('bootstrap.table', (data = new BootstrapTable(this, options)));
  1274. }
  1275. });
  1276. return typeof value === 'undefined' ? this : value;
  1277. };
  1278. $.fn.bootstrapTable.Constructor = BootstrapTable;
  1279. $.fn.bootstrapTable.defaults = BootstrapTable.DEFAULTS;
  1280. $.fn.bootstrapTable.columnDefaults = BootstrapTable.COLUMN_DEFAULTS;
  1281. $.fn.bootstrapTable.methods = allowedMethods;
  1282. // BOOTSTRAP TABLE INIT
  1283. // =======================
  1284. $(function () {
  1285. $('[data-toggle="table"]').bootstrapTable();
  1286. });
  1287. }(jQuery);