bootstrap-table.js 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  1. /**
  2. * @author zhixin wen <wenzhixin2010@gmail.com>
  3. * version: 1.3.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. this.data = s ? $.grep(this.options.data, function (item, i) {
  567. for (var key in item) {
  568. key = $.isNumeric(key) ? parseInt(key, 10) : key;
  569. var value = item[key];
  570. // Fix #142: search use formated data
  571. value = calculateObjectValue(that.header,
  572. that.header.formatters[$.inArray(key, that.header.fields)],
  573. [value, item, i], value);
  574. if ($.inArray(key, that.header.fields) !== -1 &&
  575. (typeof value === 'string' ||
  576. typeof value === 'number') &&
  577. (value + '').toLowerCase().indexOf(s) !== -1) {
  578. return true;
  579. }
  580. }
  581. return false;
  582. }) : this.options.data;
  583. }
  584. };
  585. BootstrapTable.prototype.initPagination = function () {
  586. this.$pagination = this.$container.find('.fixed-table-pagination');
  587. if (!this.options.pagination) {
  588. return;
  589. }
  590. var that = this,
  591. html = [],
  592. i, from, to,
  593. $pageList,
  594. $first, $pre,
  595. $next, $last,
  596. $number,
  597. data = this.getData();
  598. if (this.options.sidePagination !== 'server') {
  599. this.options.totalRows = data.length;
  600. }
  601. this.totalPages = 0;
  602. if (this.options.totalRows) {
  603. this.totalPages = ~~((this.options.totalRows - 1) / this.options.pageSize) + 1;
  604. }
  605. if (this.totalPages > 0 && this.options.pageNumber > this.totalPages) {
  606. this.options.pageNumber = this.totalPages;
  607. }
  608. this.pageFrom = (this.options.pageNumber - 1) * this.options.pageSize + 1;
  609. this.pageTo = this.options.pageNumber * this.options.pageSize;
  610. if (this.pageTo > this.options.totalRows) {
  611. this.pageTo = this.options.totalRows;
  612. }
  613. html.push(
  614. '<div class="pull-left pagination-detail">',
  615. '<span class="pagination-info">',
  616. this.options.formatShowingRows(this.pageFrom, this.pageTo, this.options.totalRows),
  617. '</span>');
  618. html.push('<span class="page-list">');
  619. var pageNumber = [
  620. '<span class="btn-group dropup">',
  621. '<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">',
  622. '<span class="page-size">',
  623. this.options.pageSize,
  624. '</span>',
  625. ' <span class="caret"></span>',
  626. '</button>',
  627. '<ul class="dropdown-menu" role="menu">'],
  628. pageList = this.options.pageList;
  629. if (typeof this.options.pageList === 'string') {
  630. var list = this.options.pageList.slice(1, -1).replace(/ /g, '').split(',');
  631. pageList = [];
  632. $.each(list, function (i, value) {
  633. pageList.push(+value);
  634. });
  635. }
  636. $.each(pageList, function (i, page) {
  637. if (!that.options.smartDisplay || that.options.totalRows >= page || i === 0) {
  638. var active = page === that.options.pageSize ? ' class="active"' : '';
  639. pageNumber.push(sprintf('<li%s><a href="javascript:void(0)">%s</a></li>', active, page));
  640. }
  641. });
  642. pageNumber.push('</ul></span>');
  643. html.push(this.options.formatRecordsPerPage(pageNumber.join('')));
  644. html.push('</span>');
  645. html.push('</div>',
  646. '<div class="pull-right pagination">',
  647. '<ul class="pagination">',
  648. '<li class="page-first"><a href="javascript:void(0)">&lt;&lt;</a></li>',
  649. '<li class="page-pre"><a href="javascript:void(0)">&lt;</a></li>');
  650. if (this.totalPages < 5) {
  651. from = 1;
  652. to = this.totalPages;
  653. } else {
  654. from = this.options.pageNumber - 2;
  655. to = from + 4;
  656. if (from < 1) {
  657. from = 1;
  658. to = 5;
  659. }
  660. if (to > this.totalPages) {
  661. to = this.totalPages;
  662. from = to - 4;
  663. }
  664. }
  665. for (i = from; i <= to; i++) {
  666. html.push('<li class="page-number' + (i === this.options.pageNumber ? ' active disabled' : '') + '">',
  667. '<a href="javascript:void(0)">', i ,'</a>',
  668. '</li>');
  669. }
  670. html.push(
  671. '<li class="page-next"><a href="javascript:void(0)">&gt;</a></li>',
  672. '<li class="page-last"><a href="javascript:void(0)">&gt;&gt;</a></li>',
  673. '</ul>',
  674. '</div>');
  675. this.$pagination.html(html.join(''));
  676. $pageList = this.$pagination.find('.page-list a');
  677. $first = this.$pagination.find('.page-first');
  678. $pre = this.$pagination.find('.page-pre');
  679. $next = this.$pagination.find('.page-next');
  680. $last = this.$pagination.find('.page-last');
  681. $number = this.$pagination.find('.page-number');
  682. if (this.options.pageNumber <= 1) {
  683. $first.addClass('disabled');
  684. $pre.addClass('disabled');
  685. }
  686. if (this.options.pageNumber >= this.totalPages) {
  687. $next.addClass('disabled');
  688. $last.addClass('disabled');
  689. }
  690. if (this.options.smartDisplay) {
  691. if (this.totalPages <= 1) {
  692. this.$pagination.find('div.pagination').hide();
  693. }
  694. if (this.options.pageList.length < 2 || this.options.totalRows <= this.options.pageList[1]) {
  695. this.$pagination.find('span.page-list').hide();
  696. }
  697. }
  698. $pageList.off('click').on('click', $.proxy(this.onPageListChange, this));
  699. $first.off('click').on('click', $.proxy(this.onPageFirst, this));
  700. $pre.off('click').on('click', $.proxy(this.onPagePre, this));
  701. $next.off('click').on('click', $.proxy(this.onPageNext, this));
  702. $last.off('click').on('click', $.proxy(this.onPageLast, this));
  703. $number.off('click').on('click', $.proxy(this.onPageNumber, this));
  704. };
  705. BootstrapTable.prototype.updatePagination = function (event) {
  706. // Fix #171: IE disabled button can be clicked bug.
  707. if (event && $(event.currentTarget).hasClass('disabled')) {
  708. return;
  709. }
  710. if (!this.options.maintainSelected) {
  711. this.resetRows();
  712. }
  713. this.initPagination();
  714. if (this.options.sidePagination === 'server') {
  715. this.initServer();
  716. } else {
  717. this.initBody();
  718. }
  719. this.trigger('page-change', this.options.pageSize, this.options.pageNumber);
  720. };
  721. BootstrapTable.prototype.onPageListChange = function (event) {
  722. var $this = $(event.currentTarget);
  723. $this.parent().addClass('active').siblings().removeClass('active');
  724. this.options.pageSize = +$this.text();
  725. this.$toolbar.find('.page-size').text(this.options.pageSize);
  726. this.updatePagination(event);
  727. };
  728. BootstrapTable.prototype.onPageFirst = function (event) {
  729. this.options.pageNumber = 1;
  730. this.updatePagination(event);
  731. };
  732. BootstrapTable.prototype.onPagePre = function (event) {
  733. this.options.pageNumber--;
  734. this.updatePagination(event);
  735. };
  736. BootstrapTable.prototype.onPageNext = function (event) {
  737. this.options.pageNumber++;
  738. this.updatePagination(event);
  739. };
  740. BootstrapTable.prototype.onPageLast = function (event) {
  741. this.options.pageNumber = this.totalPages;
  742. this.updatePagination(event);
  743. };
  744. BootstrapTable.prototype.onPageNumber = function (event) {
  745. if (this.options.pageNumber === +$(event.currentTarget).text()) {
  746. return;
  747. }
  748. this.options.pageNumber = +$(event.currentTarget).text();
  749. this.updatePagination(event);
  750. };
  751. BootstrapTable.prototype.initBody = function (fixedScroll) {
  752. var that = this,
  753. html = [],
  754. data = this.getData();
  755. this.$body = this.$el.find('tbody');
  756. if (!this.$body.length) {
  757. this.$body = $('<tbody></tbody>').appendTo(this.$el);
  758. }
  759. if (this.options.sidePagination === 'server') {
  760. data = this.data;
  761. }
  762. if (!this.options.pagination || this.options.sidePagination === 'server') {
  763. this.pageFrom = 1;
  764. this.pageTo = data.length;
  765. }
  766. for (var i = this.pageFrom - 1; i < this.pageTo; i++) {
  767. var item = data[i],
  768. style = {},
  769. csses = [],
  770. attributes = {},
  771. htmlAttributes = [];
  772. style = calculateObjectValue(this.options, this.options.rowStyle, [item, i], style);
  773. if (style && style.css) {
  774. for (var key in style.css) {
  775. csses.push(key + ': ' + style.css[key]);
  776. }
  777. }
  778. attributes = calculateObjectValue(this.options,
  779. this.options.rowAttributes, [item, i], attributes);
  780. if (attributes) {
  781. for (var key in attributes) {
  782. htmlAttributes.push(sprintf('%s="%s"', key, escapeHTML(attributes[key])));
  783. }
  784. }
  785. html.push('<tr',
  786. sprintf(' %s', htmlAttributes.join(' ')),
  787. sprintf(' id="%s"', item._id),
  788. sprintf(' class="%s"', style.classes || item._class),
  789. sprintf(' data-index="%s"', i),
  790. '>'
  791. );
  792. if (this.options.cardView) {
  793. html.push(sprintf('<td colspan="%s">', this.header.fields.length));
  794. }
  795. $.each(this.header.fields, function (j, field) {
  796. var text = '',
  797. value = item[field],
  798. type = '',
  799. cellStyle = {},
  800. id_ = '',
  801. class_ = that.header.classes[j];
  802. style = sprintf('style="%s"', csses.concat(that.header.styles[j]).join('; '));
  803. value = calculateObjectValue(that.header,
  804. that.header.formatters[j], [value, item, i], value);
  805. // handle td's id and class
  806. if (item['_' + field + '_id']) {
  807. id_ = sprintf(' id="%s"', item['_' + field + '_id']);
  808. }
  809. if (item['_' + field + '_class']) {
  810. class_ = sprintf(' class="%s"', item['_' + field + '_class']);
  811. }
  812. cellStyle = calculateObjectValue(that.header,
  813. that.header.cellStyles[j], [value, item, i], cellStyle);
  814. if (cellStyle.classes) {
  815. class_ = sprintf(' class="%s"', cellStyle.classes);
  816. }
  817. if (cellStyle.css) {
  818. csses = [];
  819. for (var key in cellStyle.css) {
  820. csses.push(key + ': ' + cellStyle.css[key]);
  821. }
  822. style = sprintf('style="%s"', csses.concat(that.header.styles[j]).join('; '));
  823. }
  824. if (that.options.columns[j].checkbox || that.options.columns[j].radio) {
  825. //if card view mode bypass
  826. if (that.options.cardView) {
  827. return true;
  828. }
  829. type = that.options.columns[j].checkbox ? 'checkbox' : type;
  830. type = that.options.columns[j].radio ? 'radio' : type;
  831. text = ['<td class="bs-checkbox">',
  832. '<input' +
  833. sprintf(' data-index="%s"', i) +
  834. sprintf(' name="%s"', that.options.selectItemName) +
  835. sprintf(' type="%s"', type) +
  836. sprintf(' value="%s"', item[that.options.idField]) +
  837. sprintf(' checked="%s"', value === true ||
  838. (value && value.checked) ? 'checked' : undefined) +
  839. sprintf(' disabled="%s"', !that.options.columns[j].checkboxEnabled ||
  840. (value && value.disabled) ? 'disabled' : undefined) +
  841. ' />',
  842. '</td>'].join('');
  843. } else {
  844. value = typeof value === 'undefined' || value === null ?
  845. that.options.undefinedText : value;
  846. text = that.options.cardView ?
  847. ['<div class="card-view">',
  848. that.options.showHeader ? sprintf('<span class="title" %s>%s</span>', style,
  849. getPropertyFromOther(that.options.columns, 'field', 'title', field)) : '',
  850. sprintf('<span class="value">%s</span>', value),
  851. '</div>'].join('') :
  852. [sprintf('<td%s %s %s>', id_, class_, style),
  853. value,
  854. '</td>'].join('');
  855. // Hide empty data on Card view when smartDisplay is set to true.
  856. if (that.options.cardView && that.options.smartDisplay && value === '') {
  857. text = '';
  858. }
  859. }
  860. html.push(text);
  861. });
  862. if (this.options.cardView) {
  863. html.push('</td>');
  864. }
  865. html.push('</tr>');
  866. }
  867. // show no records
  868. if (!html.length) {
  869. html.push('<tr class="no-records-found">',
  870. sprintf('<td colspan="%s">%s</td>', this.header.fields.length, this.options.formatNoMatches()),
  871. '</tr>');
  872. }
  873. this.$body.html(html.join(''));
  874. if (!fixedScroll) {
  875. this.$container.find('.fixed-table-body').scrollTop(0);
  876. }
  877. // click to select by column
  878. this.$body.find('> tr > td').off('click').on('click', function () {
  879. var $tr = $(this).parent();
  880. that.trigger('click-row', that.data[$tr.data('index')], $tr);
  881. // if click to select - then trigger the checkbox/radio click
  882. if (that.options.clickToSelect) {
  883. if (that.header.clickToSelects[$tr.children().index($(this))]) {
  884. $tr.find(sprintf('[name="%s"]',
  885. that.options.selectItemName)).trigger('click');
  886. }
  887. }
  888. });
  889. this.$body.find('tr').off('dblclick').on('dblclick', function () {
  890. that.trigger('dbl-click-row', that.data[$(this).data('index')], $(this));
  891. });
  892. this.$selectItem = this.$body.find(sprintf('[name="%s"]', this.options.selectItemName));
  893. this.$selectItem.off('click').on('click', function (event) {
  894. event.stopImmediatePropagation();
  895. // radio trigger click event bug!
  896. if ($(this).is(':radio')) {
  897. $(this).prop('checked', true);
  898. }
  899. var checkAll = that.$selectItem.filter(':enabled').length ===
  900. that.$selectItem.filter(':enabled').filter(':checked').length,
  901. checked = $(this).prop('checked'),
  902. row = that.data[$(this).data('index')];
  903. that.$selectAll.add(that.$selectAll_).prop('checked', checkAll);
  904. row[that.header.stateField] = checked;
  905. that.trigger(checked ? 'check' : 'uncheck', row);
  906. if (that.options.singleSelect) {
  907. that.$selectItem.not(this).each(function () {
  908. that.data[$(this).data('index')][that.header.stateField] = false;
  909. });
  910. that.$selectItem.filter(':checked').not(this).prop('checked', false);
  911. }
  912. that.updateSelected();
  913. });
  914. $.each(this.header.events, function (i, events) {
  915. if (!events) {
  916. return;
  917. }
  918. // fix bug, if events is defined with namespace
  919. if (typeof events === 'string') {
  920. events = calculateObjectValue(null, events);
  921. }
  922. for (var key in events) {
  923. that.$body.find('tr').each(function () {
  924. var $tr = $(this),
  925. $td = $tr.find('td').eq(i),
  926. index = key.indexOf(' '),
  927. name = key.substring(0, index),
  928. el = key.substring(index + 1),
  929. func = events[key];
  930. $td.find(el).off(name).on(name, function (e) {
  931. var index = $tr.data('index'),
  932. row = that.data[index],
  933. value = row[that.header.fields[i]];
  934. func(e, value, row, index);
  935. });
  936. });
  937. }
  938. });
  939. this.updateSelected();
  940. this.resetView();
  941. };
  942. BootstrapTable.prototype.initServer = function (silent) {
  943. var that = this,
  944. data = {},
  945. params = {
  946. pageSize: this.options.pageSize,
  947. pageNumber: this.options.pageNumber,
  948. searchText: this.searchText,
  949. sortName: this.options.sortName,
  950. sortOrder: this.options.sortOrder
  951. };
  952. if (!this.options.url) {
  953. return;
  954. }
  955. if (this.options.queryParamsType === 'limit') {
  956. params = {
  957. limit: params.pageSize,
  958. offset: params.pageSize * (params.pageNumber - 1),
  959. search: params.searchText,
  960. sort: params.sortName,
  961. order: params.sortOrder
  962. };
  963. }
  964. data = calculateObjectValue(this.options, this.options.queryParams, [params], data);
  965. // false to stop request
  966. if (data === false) {
  967. return;
  968. }
  969. if (!silent) {
  970. this.$loading.show();
  971. }
  972. $.ajax({
  973. type: this.options.method,
  974. url: this.options.url,
  975. data: data,
  976. cache: this.options.cache,
  977. contentType: this.options.contentType,
  978. dataType: this.options.dataType,
  979. success: function (res) {
  980. res = calculateObjectValue(that.options, that.options.responseHandler, [res], res);
  981. var data = res;
  982. if (that.options.sidePagination === 'server') {
  983. that.options.totalRows = res.total;
  984. data = res.rows;
  985. }
  986. that.load(data);
  987. that.trigger('load-success', data);
  988. },
  989. error: function (res) {
  990. that.trigger('load-error', res.status);
  991. },
  992. complete: function () {
  993. if (!silent) {
  994. that.$loading.hide();
  995. }
  996. }
  997. });
  998. };
  999. BootstrapTable.prototype.getCaretHtml = function () {
  1000. return ['<span class="order' + (this.options.sortOrder === 'desc' ? '' : ' dropup') + '">',
  1001. '<span class="caret" style="margin: 10px 5px;"></span>',
  1002. '</span>'].join('');
  1003. };
  1004. BootstrapTable.prototype.updateSelected = function () {
  1005. this.$selectItem.each(function () {
  1006. $(this).parents('tr')[$(this).prop('checked') ? 'addClass' : 'removeClass']('selected');
  1007. });
  1008. };
  1009. BootstrapTable.prototype.updateRows = function (checked) {
  1010. var that = this;
  1011. this.$selectItem.each(function () {
  1012. that.data[$(this).data('index')][that.header.stateField] = checked;
  1013. });
  1014. };
  1015. BootstrapTable.prototype.resetRows = function () {
  1016. var that = this;
  1017. $.each(this.data, function (i, row) {
  1018. that.$selectAll.prop('checked', false);
  1019. that.$selectItem.prop('checked', false);
  1020. row[that.header.stateField] = false;
  1021. });
  1022. };
  1023. BootstrapTable.prototype.trigger = function (name) {
  1024. var args = Array.prototype.slice.call(arguments, 1);
  1025. name += '.bs.table';
  1026. this.options[BootstrapTable.EVENTS[name]].apply(this.options, args);
  1027. this.$el.trigger($.Event(name), args);
  1028. this.options.onAll(name, args);
  1029. this.$el.trigger($.Event('all.bs.table'), [name, args]);
  1030. };
  1031. BootstrapTable.prototype.resetHeader = function () {
  1032. var that = this,
  1033. $fixedHeader = this.$container.find('.fixed-table-header'),
  1034. $fixedBody = this.$container.find('.fixed-table-body'),
  1035. scrollWidth = this.$el.width() > $fixedBody.width() ? getScrollBarWidth() : 0;
  1036. // fix #61: the hidden table reset header bug.
  1037. if (this.$el.is(':hidden')) {
  1038. clearTimeout(this.timeoutId_); // doesn't matter if it's 0
  1039. this.timeoutId_ = setTimeout($.proxy(this.resetHeader, this), 100); // 100ms
  1040. return;
  1041. }
  1042. this.$header_ = this.$header.clone(true, true);
  1043. this.$selectAll_ = this.$header_.find('[name="btSelectAll"]');
  1044. // fix bug: get $el.css('width') error sometime (height = 500)
  1045. setTimeout(function () {
  1046. $fixedHeader.css({
  1047. 'height': '37px',
  1048. 'border-bottom': '1px solid #dddddd',
  1049. 'margin-right': scrollWidth
  1050. }).find('table').css('width', that.$el.css('width'))
  1051. .html('').attr('class', that.$el.attr('class'))
  1052. .append(that.$header_);
  1053. // fix bug: $.data() is not working as expected after $.append()
  1054. that.$header.find('th').each(function (i) {
  1055. that.$header_.find('th').eq(i).data($(this).data());
  1056. });
  1057. that.$body.find('tr:first-child:not(.no-records-found) > *').each(function(i) {
  1058. that.$header_.find('div.fht-cell').eq(i).width($(this).innerWidth());
  1059. });
  1060. that.$el.css('margin-top', -that.$header.height());
  1061. // horizontal scroll event
  1062. $fixedBody.off('scroll').on('scroll', function () {
  1063. $fixedHeader.scrollLeft($(this).scrollLeft());
  1064. });
  1065. });
  1066. };
  1067. BootstrapTable.prototype.toggleColumn = function (index, checked, needUpdate) {
  1068. if (index === -1) {
  1069. return;
  1070. }
  1071. this.options.columns[index].visible = checked;
  1072. this.initHeader();
  1073. this.initSearch();
  1074. this.initPagination();
  1075. this.initBody();
  1076. if (this.options.showColumns) {
  1077. var $items = this.$toolbar.find('.keep-open input').prop('disabled', false);
  1078. if (needUpdate) {
  1079. $items.filter(sprintf('[value="%s"]', index)).prop('checked', checked);
  1080. }
  1081. if ($items.filter(':checked').length <= this.options.minimumCountColumns) {
  1082. $items.filter(':checked').prop('disabled', true);
  1083. }
  1084. }
  1085. };
  1086. // PUBLIC FUNCTION DEFINITION
  1087. // =======================
  1088. BootstrapTable.prototype.resetView = function (params) {
  1089. var that = this,
  1090. header = this.header;
  1091. if (params && params.height) {
  1092. this.options.height = params.height;
  1093. }
  1094. this.$selectAll.prop('checked', this.$selectItem.length > 0 &&
  1095. this.$selectItem.length === this.$selectItem.filter(':checked').length);
  1096. if (this.options.height) {
  1097. var toolbarHeight = +this.$toolbar.children().outerHeight(true),
  1098. paginationHeight = +this.$pagination.children().outerHeight(true),
  1099. height = this.options.height - toolbarHeight - paginationHeight;
  1100. this.$container.find('.fixed-table-container').css('height', height + 'px');
  1101. }
  1102. if (this.options.cardView) {
  1103. // remove the element css
  1104. that.$el.css('margin-top', '0');
  1105. that.$container.find('.fixed-table-container').css('padding-bottom', '0');
  1106. return;
  1107. }
  1108. if (this.options.showHeader && this.options.height) {
  1109. this.resetHeader();
  1110. }
  1111. if (this.options.height && this.options.showHeader) {
  1112. this.$container.find('.fixed-table-container').css('padding-bottom', '37px');
  1113. }
  1114. };
  1115. BootstrapTable.prototype.getData = function () {
  1116. return this.searchText ? this.data : this.options.data;
  1117. };
  1118. BootstrapTable.prototype.load = function (data) {
  1119. this.initData(data);
  1120. this.initSearch();
  1121. this.initPagination();
  1122. this.initBody();
  1123. };
  1124. BootstrapTable.prototype.append = function (data) {
  1125. this.initData(data, true);
  1126. this.initSearch();
  1127. this.initPagination();
  1128. this.initBody(true);
  1129. };
  1130. BootstrapTable.prototype.remove = function (params) {
  1131. var len = this.options.data.length,
  1132. i, row;
  1133. if (!params.hasOwnProperty('field') || !params.hasOwnProperty('values')) {
  1134. return;
  1135. }
  1136. for (i = len - 1; i >= 0; i--) {
  1137. row = this.options.data[i];
  1138. if (!row.hasOwnProperty(params.field)) {
  1139. return;
  1140. }
  1141. if ($.inArray(row[params.field], params.values) !== -1) {
  1142. this.options.data.splice(i, 1);
  1143. }
  1144. }
  1145. if (len === this.options.data.length) {
  1146. return;
  1147. }
  1148. this.initSearch();
  1149. this.initPagination();
  1150. this.initBody(true);
  1151. };
  1152. BootstrapTable.prototype.updateRow = function (params) {
  1153. if (!params.hasOwnProperty('index') || !params.hasOwnProperty('row')) {
  1154. return;
  1155. }
  1156. $.extend(this.data[params.index], params.row);
  1157. this.initBody(true);
  1158. };
  1159. BootstrapTable.prototype.mergeCells = function (options) {
  1160. var row = options.index,
  1161. col = $.inArray(options.field, this.header.fields),
  1162. rowspan = options.rowspan || 1,
  1163. colspan = options.colspan || 1,
  1164. i, j,
  1165. $tr = this.$body.find('tr'),
  1166. $td = $tr.eq(row).find('td').eq(col);
  1167. if (row < 0 || col < 0 || row >= this.data.length) {
  1168. return;
  1169. }
  1170. for (i = row; i < row + rowspan; i++) {
  1171. for (j = col; j < col + colspan; j++) {
  1172. $tr.eq(i).find('td').eq(j).hide();
  1173. }
  1174. }
  1175. $td.attr('rowspan', rowspan).attr('colspan', colspan)
  1176. .show(10, $.proxy(this.resetView, this));
  1177. };
  1178. BootstrapTable.prototype.getSelections = function () {
  1179. var that = this;
  1180. return $.grep(this.data, function (row) {
  1181. return row[that.header.stateField];
  1182. });
  1183. };
  1184. BootstrapTable.prototype.checkAll = function () {
  1185. this.$selectAll.add(this.$selectAll_).prop('checked', true);
  1186. this.$selectItem.filter(':enabled').prop('checked', true);
  1187. this.updateRows(true);
  1188. this.updateSelected();
  1189. this.trigger('check-all');
  1190. };
  1191. BootstrapTable.prototype.uncheckAll = function () {
  1192. this.$selectAll.add(this.$selectAll_).prop('checked', false);
  1193. this.$selectItem.filter(':enabled').prop('checked', false);
  1194. this.updateRows(false);
  1195. this.updateSelected();
  1196. this.trigger('uncheck-all');
  1197. };
  1198. BootstrapTable.prototype.destroy = function () {
  1199. this.$el.insertBefore(this.$container);
  1200. $(this.options.toolbar).insertBefore(this.$el);
  1201. this.$container.next().remove();
  1202. this.$container.remove();
  1203. this.$el.html(this.$el_.html())
  1204. .attr('class', this.$el_.attr('class') || ''); // reset the class
  1205. };
  1206. BootstrapTable.prototype.showLoading = function () {
  1207. this.$loading.show();
  1208. };
  1209. BootstrapTable.prototype.hideLoading = function () {
  1210. this.$loading.hide();
  1211. };
  1212. BootstrapTable.prototype.refresh = function (params) {
  1213. if (params && params.url) {
  1214. this.options.url = params.url;
  1215. this.options.pageNumber = 1;
  1216. }
  1217. this.initServer(params && params.silent);
  1218. };
  1219. BootstrapTable.prototype.showColumn = function (field) {
  1220. this.toggleColumn(getFieldIndex(this.options.columns, field), true, true);
  1221. };
  1222. BootstrapTable.prototype.hideColumn = function (field) {
  1223. this.toggleColumn(getFieldIndex(this.options.columns, field), false, true);
  1224. };
  1225. // BOOTSTRAP TABLE PLUGIN DEFINITION
  1226. // =======================
  1227. var allowedMethods = [
  1228. 'getSelections', 'getData',
  1229. 'load', 'append', 'remove',
  1230. 'updateRow',
  1231. 'mergeCells',
  1232. 'checkAll', 'uncheckAll',
  1233. 'refresh',
  1234. 'resetView',
  1235. 'destroy',
  1236. 'showLoading', 'hideLoading',
  1237. 'showColumn', 'hideColumn'
  1238. ];
  1239. $.fn.bootstrapTable = function (option, _relatedTarget) {
  1240. var value;
  1241. this.each(function () {
  1242. var $this = $(this),
  1243. data = $this.data('bootstrap.table'),
  1244. options = $.extend({}, BootstrapTable.DEFAULTS, $this.data(),
  1245. typeof option === 'object' && option);
  1246. if (typeof option === 'string') {
  1247. if ($.inArray(option, allowedMethods) < 0) {
  1248. throw "Unknown method: " + option;
  1249. }
  1250. if (!data) {
  1251. return;
  1252. }
  1253. value = data[option](_relatedTarget);
  1254. if (option === 'destroy') {
  1255. $this.removeData('bootstrap.table');
  1256. }
  1257. }
  1258. if (!data) {
  1259. $this.data('bootstrap.table', (data = new BootstrapTable(this, options)));
  1260. }
  1261. });
  1262. return typeof value === 'undefined' ? this : value;
  1263. };
  1264. $.fn.bootstrapTable.Constructor = BootstrapTable;
  1265. $.fn.bootstrapTable.defaults = BootstrapTable.DEFAULTS;
  1266. $.fn.bootstrapTable.columnDefaults = BootstrapTable.COLUMN_DEFAULTS;
  1267. $.fn.bootstrapTable.methods = allowedMethods;
  1268. // BOOTSTRAP TABLE INIT
  1269. // =======================
  1270. $(function () {
  1271. $('[data-toggle="table"]').bootstrapTable();
  1272. });
  1273. }(jQuery);