bootstrap-table.js 56 KB

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