bootstrap-table.js 53 KB

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