bootstrap-table.js 53 KB

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