bootstrap-table.js 50 KB

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