bootstrap-table.js 54 KB

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