bootstrap-table.js 55 KB

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