bootstrap-table.js 55 KB

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