bootstrap-table.js 49 KB

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