bootstrap-table.js 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083
  1. /**
  2. * @author zhixin wen <wenzhixin2010@gmail.com>
  3. * version: 1.7.0
  4. * https://github.com/wenzhixin/bootstrap-table/
  5. */
  6. !function ($) {
  7. 'use strict';
  8. // TOOLS DEFINITION
  9. // ======================
  10. var cellHeight = 37; // update css if changed
  11. var cachedWidth = null;
  12. // it only does '%s', and return '' when arguments are undefined
  13. var sprintf = function (str) {
  14. var args = arguments,
  15. flag = true,
  16. i = 1;
  17. str = str.replace(/%s/g, function () {
  18. var arg = args[i++];
  19. if (typeof arg === 'undefined') {
  20. flag = false;
  21. return '';
  22. }
  23. return arg;
  24. });
  25. return flag ? str : '';
  26. };
  27. var getPropertyFromOther = function (list, from, to, value) {
  28. var result = '';
  29. $.each(list, function (i, item) {
  30. if (item[from] === value) {
  31. result = item[to];
  32. return false;
  33. }
  34. return true;
  35. });
  36. return result;
  37. };
  38. var getFieldIndex = function (columns, field) {
  39. var index = -1;
  40. $.each(columns, function (i, column) {
  41. if (column.field === field) {
  42. index = i;
  43. return false;
  44. }
  45. return true;
  46. });
  47. return index;
  48. };
  49. var getScrollBarWidth = function () {
  50. if (cachedWidth === null) {
  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. cachedWidth = w1 - w2;
  64. }
  65. return cachedWidth;
  66. };
  67. var calculateObjectValue = function (self, name, args, defaultValue) {
  68. if (typeof name === 'string') {
  69. // support obj.func1.func2
  70. var names = name.split('.');
  71. if (names.length > 1) {
  72. name = window;
  73. $.each(names, function (i, f) {
  74. name = name[f];
  75. });
  76. } else {
  77. name = window[name];
  78. }
  79. }
  80. if (typeof name === 'object') {
  81. return name;
  82. }
  83. if (typeof name === 'function') {
  84. return name.apply(self, args);
  85. }
  86. return defaultValue;
  87. };
  88. var escapeHTML = function (text) {
  89. if (typeof text === 'string') {
  90. return text
  91. .replace(/&/g, "&amp;")
  92. .replace(/</g, "&lt;")
  93. .replace(/>/g, "&gt;")
  94. .replace(/"/g, "&quot;")
  95. .replace(/'/g, "&#039;");
  96. }
  97. return text;
  98. };
  99. var getRealHeight = function ($el) {
  100. var height = 0;
  101. $el.children().each(function () {
  102. if (height < $(this).outerHeight(true)) {
  103. height = $(this).outerHeight(true);
  104. }
  105. });
  106. return height;
  107. };
  108. // BOOTSTRAP TABLE CLASS DEFINITION
  109. // ======================
  110. var BootstrapTable = function (el, options) {
  111. this.options = options;
  112. this.$el = $(el);
  113. this.$el_ = this.$el.clone();
  114. this.timeoutId_ = 0;
  115. this.timeoutFooter_ = 0;
  116. this.init();
  117. };
  118. BootstrapTable.DEFAULTS = {
  119. classes: 'table table-hover',
  120. height: undefined,
  121. undefinedText: '-',
  122. sortName: undefined,
  123. sortOrder: 'asc',
  124. striped: false,
  125. columns: [],
  126. data: [],
  127. method: 'get',
  128. url: undefined,
  129. ajax: undefined,
  130. cache: true,
  131. contentType: 'application/json',
  132. dataType: 'json',
  133. ajaxOptions: {},
  134. queryParams: function (params) {
  135. return params;
  136. },
  137. queryParamsType: 'limit', // undefined
  138. responseHandler: function (res) {
  139. return res;
  140. },
  141. pagination: false,
  142. sidePagination: 'client', // client or server
  143. totalRows: 0, // server side need to set
  144. pageNumber: 1,
  145. pageSize: 10,
  146. pageList: [10, 25, 50, 100],
  147. paginationHAlign: 'right', //right, left
  148. paginationVAlign: 'bottom', //bottom, top, both
  149. paginationDetailHAlign: 'left', //right, left
  150. paginationFirstText: '&laquo;',
  151. paginationPreText: '&lsaquo;',
  152. paginationNextText: '&rsaquo;',
  153. paginationLastText: '&raquo;',
  154. search: false,
  155. searchAlign: 'right',
  156. selectItemName: 'btSelectItem',
  157. showHeader: true,
  158. showFooter: false,
  159. showColumns: false,
  160. showPaginationSwitch: false,
  161. showRefresh: false,
  162. showToggle: false,
  163. buttonsAlign: 'right',
  164. smartDisplay: true,
  165. minimumCountColumns: 1,
  166. idField: undefined,
  167. uniqueId: undefined,
  168. cardView: false,
  169. trimOnSearch: true,
  170. clickToSelect: false,
  171. singleSelect: false,
  172. toolbar: undefined,
  173. toolbarAlign: 'left',
  174. checkboxHeader: true,
  175. sortable: true,
  176. maintainSelected: false,
  177. searchTimeOut: 500,
  178. searchText: '',
  179. iconSize: undefined,
  180. iconsPrefix: 'glyphicon', // glyphicon of fa (font awesome)
  181. icons: {
  182. paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
  183. paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
  184. refresh: 'glyphicon-refresh icon-refresh',
  185. toggle: 'glyphicon-list-alt icon-list-alt',
  186. columns: 'glyphicon-th icon-th'
  187. },
  188. rowStyle: function (row, index) {
  189. return {};
  190. },
  191. rowAttributes: function (row, index) {
  192. return {};
  193. },
  194. onAll: function (name, args) {
  195. return false;
  196. },
  197. onClickRow: function (item, $element) {
  198. return false;
  199. },
  200. onDblClickRow: function (item, $element) {
  201. return false;
  202. },
  203. onSort: function (name, order) {
  204. return false;
  205. },
  206. onCheck: function (row) {
  207. return false;
  208. },
  209. onUncheck: function (row) {
  210. return false;
  211. },
  212. onCheckAll: function () {
  213. return false;
  214. },
  215. onUncheckAll: function () {
  216. return false;
  217. },
  218. onLoadSuccess: function (data) {
  219. return false;
  220. },
  221. onLoadError: function (status) {
  222. return false;
  223. },
  224. onColumnSwitch: function (field, checked) {
  225. return false;
  226. },
  227. onPageChange: function (number, size) {
  228. return false;
  229. },
  230. onSearch: function (text) {
  231. return false;
  232. },
  233. onToggle: function (cardView) {
  234. return false;
  235. },
  236. onPreBody: function (data) {
  237. return false;
  238. },
  239. onPostBody: function () {
  240. return false;
  241. },
  242. onPostHeader: function () {
  243. return false;
  244. }
  245. };
  246. BootstrapTable.LOCALES = [];
  247. BootstrapTable.LOCALES['en-US'] = {
  248. formatLoadingMessage: function () {
  249. return 'Loading, please wait...';
  250. },
  251. formatRecordsPerPage: function (pageNumber) {
  252. return sprintf('%s records per page', pageNumber);
  253. },
  254. formatShowingRows: function (pageFrom, pageTo, totalRows) {
  255. return sprintf('Showing %s to %s of %s rows', pageFrom, pageTo, totalRows);
  256. },
  257. formatSearch: function () {
  258. return 'Search';
  259. },
  260. formatNoMatches: function () {
  261. return 'No matching records found';
  262. },
  263. formatPaginationSwitch: function () {
  264. return 'Hide/Show pagination';
  265. },
  266. formatRefresh: function () {
  267. return 'Refresh';
  268. },
  269. formatToggle: function () {
  270. return 'Toggle';
  271. },
  272. formatColumns: function () {
  273. return 'Columns';
  274. },
  275. formatAllRows: function () {
  276. return 'All';
  277. }
  278. };
  279. $.extend(BootstrapTable.DEFAULTS, BootstrapTable.LOCALES['en-US']);
  280. BootstrapTable.COLUMN_DEFAULTS = {
  281. radio: false,
  282. checkbox: false,
  283. checkboxEnabled: true,
  284. field: undefined,
  285. title: undefined,
  286. 'class': undefined,
  287. align: undefined, // left, right, center
  288. halign: undefined, // left, right, center
  289. falign: undefined, // left, right, center
  290. valign: undefined, // top, middle, bottom
  291. width: undefined,
  292. sortable: false,
  293. order: 'asc', // asc, desc
  294. visible: true,
  295. switchable: true,
  296. clickToSelect: true,
  297. formatter: undefined,
  298. footerFormatter: undefined,
  299. events: undefined,
  300. sorter: undefined,
  301. sortName: undefined,
  302. cellStyle: undefined,
  303. searchable: true,
  304. cardVisible: true
  305. };
  306. BootstrapTable.EVENTS = {
  307. 'all.bs.table': 'onAll',
  308. 'click-row.bs.table': 'onClickRow',
  309. 'dbl-click-row.bs.table': 'onDblClickRow',
  310. 'sort.bs.table': 'onSort',
  311. 'check.bs.table': 'onCheck',
  312. 'uncheck.bs.table': 'onUncheck',
  313. 'check-all.bs.table': 'onCheckAll',
  314. 'uncheck-all.bs.table': 'onUncheckAll',
  315. 'load-success.bs.table': 'onLoadSuccess',
  316. 'load-error.bs.table': 'onLoadError',
  317. 'column-switch.bs.table': 'onColumnSwitch',
  318. 'page-change.bs.table': 'onPageChange',
  319. 'search.bs.table': 'onSearch',
  320. 'toggle.bs.table': 'onToggle',
  321. 'pre-body.bs.table': 'onPreBody',
  322. 'post-body.bs.table': 'onPostBody',
  323. 'post-header.bs.table': 'onPostHeader'
  324. };
  325. BootstrapTable.prototype.init = function () {
  326. this.initContainer();
  327. this.initTable();
  328. this.initHeader();
  329. this.initData();
  330. this.initFooter();
  331. this.initToolbar();
  332. this.initPagination();
  333. this.initBody();
  334. this.initServer();
  335. };
  336. BootstrapTable.prototype.initContainer = function () {
  337. this.$container = $([
  338. '<div class="bootstrap-table">',
  339. '<div class="fixed-table-toolbar"></div>',
  340. this.options.paginationVAlign === 'top' || this.options.paginationVAlign === 'both' ?
  341. '<div class="fixed-table-pagination" style="clear: both;"></div>' :
  342. '',
  343. '<div class="fixed-table-container">',
  344. '<div class="fixed-table-header"><table></table></div>',
  345. '<div class="fixed-table-body">',
  346. '<div class="fixed-table-loading">',
  347. this.options.formatLoadingMessage(),
  348. '</div>',
  349. '</div>',
  350. '<div class="fixed-table-footer"><table><tr></tr></table></div>',
  351. this.options.paginationVAlign === 'bottom' || this.options.paginationVAlign === 'both' ?
  352. '<div class="fixed-table-pagination"></div>' :
  353. '',
  354. '</div>',
  355. '</div>'].join(''));
  356. this.$container.insertAfter(this.$el);
  357. this.$container.find('.fixed-table-body').append(this.$el);
  358. this.$container.after('<div class="clearfix"></div>');
  359. this.$loading = this.$container.find('.fixed-table-loading');
  360. this.$el.addClass(this.options.classes);
  361. if (this.options.striped) {
  362. this.$el.addClass('table-striped');
  363. }
  364. if ($.inArray('table-no-bordered', this.options.classes.split(' ')) !== -1) {
  365. this.$container.find('.fixed-table-container').addClass('table-no-bordered');
  366. }
  367. };
  368. BootstrapTable.prototype.initTable = function () {
  369. var that = this,
  370. columns = [],
  371. data = [];
  372. this.$header = this.$el.find('thead');
  373. if (!this.$header.length) {
  374. this.$header = $('<thead></thead>').appendTo(this.$el);
  375. }
  376. if (!this.$header.find('tr').length) {
  377. this.$header.append('<tr></tr>');
  378. }
  379. this.$header.find('th').each(function () {
  380. var column = $.extend({}, {
  381. title: $(this).html(),
  382. 'class': $(this).attr('class')
  383. }, $(this).data());
  384. columns.push(column);
  385. });
  386. this.options.columns = $.extend(true, [], columns, this.options.columns);
  387. $.each(this.options.columns, function (i, column) {
  388. that.options.columns[i] = $.extend({}, BootstrapTable.COLUMN_DEFAULTS,
  389. {field: i}, column); // when field is undefined, use index instead
  390. });
  391. // if options.data is setting, do not process tbody data
  392. if (this.options.data.length) {
  393. return;
  394. }
  395. this.$el.find('tbody tr').each(function () {
  396. var row = {};
  397. // save tr's id and class
  398. row._id = $(this).attr('id');
  399. row._class = $(this).attr('class');
  400. $(this).find('td').each(function (i) {
  401. var field = that.options.columns[i].field;
  402. row[field] = $(this).html();
  403. // save td's id and class
  404. row['_' + field + '_id'] = $(this).attr('id');
  405. row['_' + field + '_class'] = $(this).attr('class');
  406. row['_' + field + '_data'] = $(this).data();
  407. });
  408. data.push(row);
  409. });
  410. this.options.data = data;
  411. };
  412. BootstrapTable.prototype.initHeader = function () {
  413. var that = this,
  414. visibleColumns = [],
  415. html = [],
  416. timeoutId = 0;
  417. this.header = {
  418. fields: [],
  419. styles: [],
  420. classes: [],
  421. formatters: [],
  422. events: [],
  423. sorters: [],
  424. sortNames: [],
  425. cellStyles: [],
  426. clickToSelects: [],
  427. searchables: []
  428. };
  429. $.each(this.options.columns, function (i, column) {
  430. var text = '',
  431. halign = '', // header align style
  432. align = '', // body align style
  433. style = '',
  434. class_ = sprintf(' class="%s"', column['class']),
  435. order = that.options.sortOrder || column.order,
  436. searchable = true,
  437. unitWidth = 'px',
  438. width = column.width;
  439. if (!column.visible) {
  440. // Fix #229. Default Sort order is wrong if data-visible="false" is set on the field referenced by data-sort-name.
  441. if (column.field === that.options.sortName) {
  442. that.header.fields.push(column.field);
  443. }
  444. return;
  445. }
  446. if (that.options.cardView && (!column.cardVisible)) {
  447. return;
  448. }
  449. if (column.width !== undefined && (!that.options.cardView)) {
  450. if (typeof column.width === 'string') {
  451. if (column.width.indexOf('%') !== -1) {
  452. unitWidth = '%';
  453. }
  454. }
  455. }
  456. if (column.width && typeof column.width === 'string') {
  457. width = column.width.replace('%', '').replace('px', '');
  458. }
  459. halign = sprintf('text-align: %s; ', column.halign ? column.halign : column.align);
  460. align = sprintf('text-align: %s; ', column.align);
  461. style = sprintf('vertical-align: %s; ', column.valign);
  462. style += sprintf('width: %s%s; ', column.checkbox || column.radio ? 36 : width, unitWidth);
  463. visibleColumns.push(column);
  464. that.header.fields.push(column.field);
  465. that.header.styles.push(align + style);
  466. that.header.classes.push(class_);
  467. that.header.formatters.push(column.formatter);
  468. that.header.events.push(column.events);
  469. that.header.sorters.push(column.sorter);
  470. that.header.sortNames.push(column.sortName);
  471. that.header.cellStyles.push(column.cellStyle);
  472. that.header.clickToSelects.push(column.clickToSelect);
  473. that.header.searchables.push(column.searchable);
  474. html.push('<th',
  475. column.checkbox || column.radio ?
  476. sprintf(' class="bs-checkbox %s"', column['class'] || '') :
  477. class_,
  478. sprintf(' style="%s"', halign + style),
  479. '>');
  480. html.push(sprintf('<div class="th-inner %s">', that.options.sortable && column.sortable ?
  481. 'sortable' : ''));
  482. text = column.title;
  483. if (that.options.sortName === column.field && that.options.sortable && column.sortable) {
  484. text += that.getCaretHtml();
  485. }
  486. if (column.checkbox) {
  487. if (!that.options.singleSelect && that.options.checkboxHeader) {
  488. text = '<input name="btSelectAll" type="checkbox" />';
  489. }
  490. that.header.stateField = column.field;
  491. }
  492. if (column.radio) {
  493. text = '';
  494. that.header.stateField = column.field;
  495. that.options.singleSelect = true;
  496. }
  497. html.push(text);
  498. html.push('</div>');
  499. html.push('<div class="fht-cell"></div>');
  500. html.push('</div>');
  501. html.push('</th>');
  502. });
  503. this.$header.find('tr').html(html.join(''));
  504. this.$header.find('th').each(function (i) {
  505. $(this).data(visibleColumns[i]);
  506. });
  507. this.$container.off('click', '.th-inner').on('click', '.th-inner', function (event) {
  508. if (that.options.sortable && $(this).parent().data().sortable) {
  509. that.onSort(event);
  510. }
  511. });
  512. if (!this.options.showHeader || this.options.cardView) {
  513. this.$header.hide();
  514. this.$container.find('.fixed-table-header').hide();
  515. this.$loading.css('top', 0);
  516. } else {
  517. this.$header.show();
  518. this.$container.find('.fixed-table-header').show();
  519. this.$loading.css('top', cellHeight + 'px');
  520. }
  521. this.$selectAll = this.$header.find('[name="btSelectAll"]');
  522. this.$container.off('click', '[name="btSelectAll"]')
  523. .on('click', '[name="btSelectAll"]', function () {
  524. var checked = $(this).prop('checked');
  525. that[checked ? 'checkAll' : 'uncheckAll']();
  526. });
  527. };
  528. BootstrapTable.prototype.initFooter = function () {
  529. this.$footer = this.$container.find('.fixed-table-footer');
  530. if (!this.options.showFooter || this.options.cardView) {
  531. this.$footer.hide();
  532. } else {
  533. this.$footer.show();
  534. }
  535. };
  536. /**
  537. * @param data
  538. * @param type: append / prepend
  539. */
  540. BootstrapTable.prototype.initData = function (data, type) {
  541. if (type === 'append') {
  542. this.data = this.data.concat(data);
  543. } else if (type === 'prepend') {
  544. this.data = [].concat(data).concat(this.data);
  545. } else {
  546. this.data = data || this.options.data;
  547. }
  548. this.options.data = this.data;
  549. if (this.options.sidePagination === 'server') {
  550. return;
  551. }
  552. this.initSort();
  553. };
  554. BootstrapTable.prototype.initSort = function () {
  555. var that = this,
  556. name = this.options.sortName,
  557. order = this.options.sortOrder === 'desc' ? -1 : 1,
  558. index = $.inArray(this.options.sortName, this.header.fields);
  559. if (index !== -1) {
  560. this.data.sort(function (a, b) {
  561. if (that.header.sortNames[index]) {
  562. name = that.header.sortNames[index];
  563. }
  564. var aa = a[name],
  565. bb = b[name],
  566. value = calculateObjectValue(that.header, that.header.sorters[index], [aa, bb]);
  567. if (value !== undefined) {
  568. return order * value;
  569. }
  570. // Fix #161: undefined or null string sort bug.
  571. if (aa === undefined || aa === null) {
  572. aa = '';
  573. }
  574. if (bb === undefined || bb === null) {
  575. bb = '';
  576. }
  577. // IF both values are numeric, do a numeric comparison
  578. if ($.isNumeric(aa) && $.isNumeric(bb)) {
  579. // Convert numerical values form string to float.
  580. aa = parseFloat(aa);
  581. bb = parseFloat(bb);
  582. if (aa < bb) {
  583. return order * -1;
  584. }
  585. return order;
  586. }
  587. if (aa === bb) {
  588. return 0;
  589. }
  590. // If value is not a string, convert to string
  591. if (typeof aa !== 'string') {
  592. aa = aa.toString();
  593. }
  594. if (aa.localeCompare(bb) === -1) {
  595. return order * -1;
  596. }
  597. return order;
  598. });
  599. }
  600. };
  601. BootstrapTable.prototype.onSort = function (event) {
  602. var $this = $(event.currentTarget).parent(),
  603. $this_ = this.$header.find('th').eq($this.index());
  604. this.$header.add(this.$header_).find('span.order').remove();
  605. if (this.options.sortName === $this.data('field')) {
  606. this.options.sortOrder = this.options.sortOrder === 'asc' ? 'desc' : 'asc';
  607. } else {
  608. this.options.sortName = $this.data('field');
  609. this.options.sortOrder = $this.data('order') === 'asc' ? 'desc' : 'asc';
  610. }
  611. this.trigger('sort', this.options.sortName, this.options.sortOrder);
  612. $this.add($this_).data('order', this.options.sortOrder)
  613. .find('.th-inner').append(this.getCaretHtml());
  614. if (this.options.sidePagination === 'server') {
  615. this.initServer();
  616. return;
  617. }
  618. this.initSort();
  619. this.initBody();
  620. };
  621. BootstrapTable.prototype.initToolbar = function () {
  622. var that = this,
  623. html = [],
  624. timeoutId = 0,
  625. $keepOpen,
  626. $search,
  627. switchableCount = 0;
  628. this.$toolbar = this.$container.find('.fixed-table-toolbar').html('');
  629. if (typeof this.options.toolbar === 'string') {
  630. $(sprintf('<div class="bars pull-%s"></div>', this.options.toolbarAlign))
  631. .appendTo(this.$toolbar)
  632. .append($(this.options.toolbar));
  633. }
  634. // showColumns, showToggle, showRefresh
  635. html = [sprintf('<div class="columns columns-%s btn-group pull-%s">',
  636. this.options.buttonsAlign, this.options.buttonsAlign)];
  637. if (typeof this.options.icons === 'string') {
  638. this.options.icons = calculateObjectValue(null, this.options.icons);
  639. }
  640. if (this.options.showPaginationSwitch) {
  641. html.push(sprintf('<button class="btn btn-default" type="button" name="paginationSwitch" title="%s">',
  642. this.options.formatPaginationSwitch()),
  643. sprintf('<i class="%s %s"></i>', this.options.iconsPrefix, this.options.icons.paginationSwitchDown),
  644. '</button>');
  645. }
  646. if (this.options.showRefresh) {
  647. html.push(sprintf('<button class="btn btn-default' + (this.options.iconSize === undefined ? '' : ' btn-' + this.options.iconSize) + '" type="button" name="refresh" title="%s">',
  648. this.options.formatRefresh()),
  649. sprintf('<i class="%s %s"></i>', this.options.iconsPrefix, this.options.icons.refresh),
  650. '</button>');
  651. }
  652. if (this.options.showToggle) {
  653. html.push(sprintf('<button class="btn btn-default' + (this.options.iconSize === undefined ? '' : ' btn-' + this.options.iconSize) + '" type="button" name="toggle" title="%s">',
  654. this.options.formatToggle()),
  655. sprintf('<i class="%s %s"></i>', this.options.iconsPrefix, this.options.icons.toggle),
  656. '</button>');
  657. }
  658. if (this.options.showColumns) {
  659. html.push(sprintf('<div class="keep-open btn-group" title="%s">',
  660. this.options.formatColumns()),
  661. '<button type="button" class="btn btn-default' + (this.options.iconSize == undefined ? '' : ' btn-' + this.options.iconSize) + ' dropdown-toggle" data-toggle="dropdown">',
  662. sprintf('<i class="%s %s"></i>', this.options.iconsPrefix, this.options.icons.columns),
  663. ' <span class="caret"></span>',
  664. '</button>',
  665. '<ul class="dropdown-menu" role="menu">');
  666. $.each(this.options.columns, function (i, column) {
  667. if (column.radio || column.checkbox) {
  668. return;
  669. }
  670. if (that.options.cardView && (!column.cardVisible)) {
  671. return;
  672. }
  673. var checked = column.visible ? ' checked="checked"' : '';
  674. if (column.switchable) {
  675. html.push(sprintf('<li>' +
  676. '<label><input type="checkbox" data-field="%s" value="%s"%s> %s</label>' +
  677. '</li>', column.field, i, checked, column.title));
  678. switchableCount++;
  679. }
  680. });
  681. html.push('</ul>',
  682. '</div>');
  683. }
  684. html.push('</div>');
  685. // Fix #188: this.showToolbar is for extentions
  686. if (this.showToolbar || html.length > 2) {
  687. this.$toolbar.append(html.join(''));
  688. }
  689. if (this.options.showPaginationSwitch) {
  690. this.$toolbar.find('button[name="paginationSwitch"]')
  691. .off('click').on('click', $.proxy(this.togglePagination, this));
  692. }
  693. if (this.options.showRefresh) {
  694. this.$toolbar.find('button[name="refresh"]')
  695. .off('click').on('click', $.proxy(this.refresh, this));
  696. }
  697. if (this.options.showToggle) {
  698. this.$toolbar.find('button[name="toggle"]')
  699. .off('click').on('click', function () {
  700. that.toggleView();
  701. });
  702. }
  703. if (this.options.showColumns) {
  704. $keepOpen = this.$toolbar.find('.keep-open');
  705. if (switchableCount <= this.options.minimumCountColumns) {
  706. $keepOpen.find('input').prop('disabled', true);
  707. }
  708. $keepOpen.find('li').off('click').on('click', function (event) {
  709. event.stopImmediatePropagation();
  710. });
  711. $keepOpen.find('input').off('click').on('click', function () {
  712. var $this = $(this);
  713. that.toggleColumn(getFieldIndex(that.options.columns, $(this).data('field')), $this.prop('checked'), false);
  714. that.trigger('column-switch', $(this).data('field'), $this.prop('checked'));
  715. });
  716. }
  717. if (this.options.search) {
  718. html = [];
  719. html.push(
  720. '<div class="pull-' + this.options.searchAlign + ' search">',
  721. sprintf('<input class="form-control' + (this.options.iconSize === undefined ? '' : ' input-' + this.options.iconSize) + '" type="text" placeholder="%s">',
  722. this.options.formatSearch()),
  723. '</div>');
  724. this.$toolbar.append(html.join(''));
  725. $search = this.$toolbar.find('.search input');
  726. $search.off('keyup drop').on('keyup drop', function (event) {
  727. clearTimeout(timeoutId); // doesn't matter if it's 0
  728. timeoutId = setTimeout(function () {
  729. that.onSearch(event);
  730. }, that.options.searchTimeOut);
  731. });
  732. if (this.options.searchText !== '') {
  733. $search.val(this.options.searchText);
  734. clearTimeout(timeoutId); // doesn't matter if it's 0
  735. timeoutId = setTimeout(function () {
  736. $search.trigger('keyup');
  737. }, that.options.searchTimeOut);
  738. }
  739. }
  740. };
  741. BootstrapTable.prototype.onSearch = function (event) {
  742. var text = $.trim($(event.currentTarget).val());
  743. // trim search input
  744. if (this.options.trimOnSearch && $(event.currentTarget).val() !== text) {
  745. $(event.currentTarget).val(text);
  746. }
  747. if (text === this.searchText) {
  748. return;
  749. }
  750. this.searchText = text;
  751. this.options.pageNumber = 1;
  752. this.initSearch();
  753. this.updatePagination();
  754. this.trigger('search', text);
  755. };
  756. BootstrapTable.prototype.initSearch = function () {
  757. var that = this;
  758. if (this.options.sidePagination !== 'server') {
  759. var s = this.searchText && this.searchText.toLowerCase();
  760. var f = $.isEmptyObject(this.filterColumns) ? null : this.filterColumns;
  761. // Check filter
  762. this.data = f ? $.grep(this.options.data, function (item, i) {
  763. for (var key in f) {
  764. if (item[key] !== f[key]) {
  765. return false;
  766. }
  767. }
  768. return true;
  769. }) : this.options.data;
  770. this.data = s ? $.grep(this.data, function (item, i) {
  771. for (var key in item) {
  772. key = $.isNumeric(key) ? parseInt(key, 10) : key;
  773. var value = item[key];
  774. // Fix #142: search use formated data
  775. value = calculateObjectValue(that.header,
  776. that.header.formatters[$.inArray(key, that.header.fields)],
  777. [value, item, i], value);
  778. var index = $.inArray(key, that.header.fields);
  779. if (index !== -1 && that.header.searchables[index] &&
  780. (typeof value === 'string' ||
  781. typeof value === 'number') &&
  782. (value + '').toLowerCase().indexOf(s) !== -1) {
  783. return true;
  784. }
  785. }
  786. return false;
  787. }) : this.data;
  788. }
  789. };
  790. BootstrapTable.prototype.initPagination = function () {
  791. this.$pagination = this.$container.find('.fixed-table-pagination');
  792. if (!this.options.pagination) {
  793. this.$pagination.hide();
  794. return;
  795. } else {
  796. this.$pagination.show();
  797. }
  798. var that = this,
  799. html = [],
  800. $allSelected = false,
  801. i, from, to,
  802. $pageList,
  803. $first, $pre,
  804. $next, $last,
  805. $number,
  806. data = this.getData();
  807. if (this.options.sidePagination !== 'server') {
  808. this.options.totalRows = data.length;
  809. }
  810. this.totalPages = 0;
  811. if (this.options.totalRows) {
  812. if (this.options.pageSize === this.options.formatAllRows()) {
  813. this.options.pageSize = this.options.totalRows;
  814. $allSelected = true;
  815. } else if (this.options.pageSize === this.options.totalRows) {
  816. // Fix #667 Table with pagination, multiple pages and a search that matches to one page throws exception
  817. var pageLst = typeof this.options.pageList === 'string' ?
  818. this.options.pageList.replace('[', '').replace(']', '').replace(/ /g, '').toLowerCase().split(',') :
  819. this.options.pageList;
  820. if (pageLst.indexOf(this.options.formatAllRows().toLowerCase()) > -1) {
  821. $allSelected = true;
  822. }
  823. }
  824. this.totalPages = ~~((this.options.totalRows - 1) / this.options.pageSize) + 1;
  825. this.options.totalPages = this.totalPages;
  826. }
  827. if (this.totalPages > 0 && this.options.pageNumber > this.totalPages) {
  828. this.options.pageNumber = this.totalPages;
  829. }
  830. this.pageFrom = (this.options.pageNumber - 1) * this.options.pageSize + 1;
  831. this.pageTo = this.options.pageNumber * this.options.pageSize;
  832. if (this.pageTo > this.options.totalRows) {
  833. this.pageTo = this.options.totalRows;
  834. }
  835. html.push(
  836. '<div class="pull-' + this.options.paginationDetailHAlign + ' pagination-detail">',
  837. '<span class="pagination-info">',
  838. this.options.formatShowingRows(this.pageFrom, this.pageTo, this.options.totalRows),
  839. '</span>');
  840. html.push('<span class="page-list">');
  841. var pageNumber = [
  842. sprintf('<span class="btn-group %s">', this.options.paginationVAlign === 'top' || this.options.paginationVAlign === 'both' ?
  843. 'dropdown' : 'dropup'),
  844. '<button type="button" class="btn btn-default ' + (this.options.iconSize === undefined ? '' : ' btn-' + this.options.iconSize) + ' dropdown-toggle" data-toggle="dropdown">',
  845. '<span class="page-size">',
  846. $allSelected ? this.options.formatAllRows() : this.options.pageSize,
  847. '</span>',
  848. ' <span class="caret"></span>',
  849. '</button>',
  850. '<ul class="dropdown-menu" role="menu">'],
  851. pageList = this.options.pageList;
  852. if (typeof this.options.pageList === 'string') {
  853. var list = this.options.pageList.replace('[', '').replace(']', '').replace(/ /g, '').split(',');
  854. pageList = [];
  855. $.each(list, function (i, value) {
  856. pageList.push(value.toUpperCase() === that.options.formatAllRows().toUpperCase() ?
  857. that.options.formatAllRows() : +value);
  858. });
  859. }
  860. $.each(pageList, function (i, page) {
  861. if (!that.options.smartDisplay || i === 0 || pageList[i - 1] <= that.options.totalRows) {
  862. var active;
  863. if ($allSelected) {
  864. active = page === that.options.formatAllRows() ? ' class="active"' : '';
  865. } else {
  866. active = page === that.options.pageSize ? ' class="active"' : '';
  867. }
  868. pageNumber.push(sprintf('<li%s><a href="javascript:void(0)">%s</a></li>', active, page));
  869. }
  870. });
  871. pageNumber.push('</ul></span>');
  872. html.push(this.options.formatRecordsPerPage(pageNumber.join('')));
  873. html.push('</span>');
  874. // Fixed #611 vertical-align between pagination block and pagination-detail block. Remove class pagination.
  875. html.push('</div>',
  876. '<div class="pull-' + this.options.paginationHAlign + '">',
  877. '<ul class="pagination' + (this.options.iconSize === undefined ? '' : ' pagination-' + this.options.iconSize) + '">',
  878. '<li class="page-first"><a href="javascript:void(0)">' + this.options.paginationFirstText + '</a></li>',
  879. '<li class="page-pre"><a href="javascript:void(0)">' + this.options.paginationPreText + '</a></li>');
  880. if (this.totalPages < 5) {
  881. from = 1;
  882. to = this.totalPages;
  883. } else {
  884. from = this.options.pageNumber - 2;
  885. to = from + 4;
  886. if (from < 1) {
  887. from = 1;
  888. to = 5;
  889. }
  890. if (to > this.totalPages) {
  891. to = this.totalPages;
  892. from = to - 4;
  893. }
  894. }
  895. for (i = from; i <= to; i++) {
  896. html.push('<li class="page-number' + (i === this.options.pageNumber ? ' active' : '') + '">',
  897. '<a href="javascript:void(0)">', i, '</a>',
  898. '</li>');
  899. }
  900. html.push(
  901. '<li class="page-next"><a href="javascript:void(0)">' + this.options.paginationNextText + '</a></li>',
  902. '<li class="page-last"><a href="javascript:void(0)">' + this.options.paginationLastText + '</a></li>',
  903. '</ul>',
  904. '</div>');
  905. this.$pagination.html(html.join(''));
  906. $pageList = this.$pagination.find('.page-list a');
  907. $first = this.$pagination.find('.page-first');
  908. $pre = this.$pagination.find('.page-pre');
  909. $next = this.$pagination.find('.page-next');
  910. $last = this.$pagination.find('.page-last');
  911. $number = this.$pagination.find('.page-number');
  912. if (this.options.pageNumber <= 1) {
  913. $first.addClass('disabled');
  914. $pre.addClass('disabled');
  915. }
  916. if (this.options.pageNumber >= this.totalPages) {
  917. $next.addClass('disabled');
  918. $last.addClass('disabled');
  919. }
  920. if (this.options.smartDisplay) {
  921. if (this.totalPages <= 1) {
  922. this.$pagination.find('div.pagination').hide();
  923. }
  924. if (pageList.length < 2 || this.options.totalRows <= pageList[0]) {
  925. this.$pagination.find('span.page-list').hide();
  926. }
  927. // when data is empty, hide the pagination
  928. this.$pagination[this.getData().length ? 'show' : 'hide']();
  929. }
  930. if ($allSelected) {
  931. this.options.pageSize = this.options.formatAllRows();
  932. }
  933. $pageList.off('click').on('click', $.proxy(this.onPageListChange, this));
  934. $first.off('click').on('click', $.proxy(this.onPageFirst, this));
  935. $pre.off('click').on('click', $.proxy(this.onPagePre, this));
  936. $next.off('click').on('click', $.proxy(this.onPageNext, this));
  937. $last.off('click').on('click', $.proxy(this.onPageLast, this));
  938. $number.off('click').on('click', $.proxy(this.onPageNumber, this));
  939. };
  940. BootstrapTable.prototype.updatePagination = function (event) {
  941. // Fix #171: IE disabled button can be clicked bug.
  942. if (event && $(event.currentTarget).hasClass('disabled')) {
  943. return;
  944. }
  945. if (!this.options.maintainSelected) {
  946. this.resetRows();
  947. }
  948. this.initPagination();
  949. if (this.options.sidePagination === 'server') {
  950. this.initServer();
  951. } else {
  952. this.initBody();
  953. }
  954. this.trigger('page-change', this.options.pageNumber, this.options.pageSize);
  955. };
  956. BootstrapTable.prototype.onPageListChange = function (event) {
  957. var $this = $(event.currentTarget);
  958. $this.parent().addClass('active').siblings().removeClass('active');
  959. this.options.pageSize = $this.text().toUpperCase() === this.options.formatAllRows().toUpperCase() ?
  960. this.options.formatAllRows() : +$this.text();
  961. this.$toolbar.find('.page-size').text(this.options.pageSize);
  962. this.updatePagination(event);
  963. };
  964. BootstrapTable.prototype.onPageFirst = function (event) {
  965. this.options.pageNumber = 1;
  966. this.updatePagination(event);
  967. };
  968. BootstrapTable.prototype.onPagePre = function (event) {
  969. this.options.pageNumber--;
  970. this.updatePagination(event);
  971. };
  972. BootstrapTable.prototype.onPageNext = function (event) {
  973. this.options.pageNumber++;
  974. this.updatePagination(event);
  975. };
  976. BootstrapTable.prototype.onPageLast = function (event) {
  977. this.options.pageNumber = this.totalPages;
  978. this.updatePagination(event);
  979. };
  980. BootstrapTable.prototype.onPageNumber = function (event) {
  981. if (this.options.pageNumber === +$(event.currentTarget).text()) {
  982. return;
  983. }
  984. this.options.pageNumber = +$(event.currentTarget).text();
  985. this.updatePagination(event);
  986. };
  987. BootstrapTable.prototype.initBody = function (fixedScroll) {
  988. var that = this,
  989. html = [],
  990. data = this.getData();
  991. this.trigger('pre-body', data);
  992. this.$body = this.$el.find('tbody');
  993. if (!this.$body.length) {
  994. this.$body = $('<tbody></tbody>').appendTo(this.$el);
  995. }
  996. //Fix #389 Bootstrap-table-flatJSON is not working
  997. if (!this.options.pagination || this.options.sidePagination === 'server') {
  998. this.pageFrom = 1;
  999. this.pageTo = data.length;
  1000. }
  1001. for (var i = this.pageFrom - 1; i < this.pageTo; i++) {
  1002. var key,
  1003. item = data[i],
  1004. style = {},
  1005. csses = [],
  1006. attributes = {},
  1007. htmlAttributes = [];
  1008. style = calculateObjectValue(this.options, this.options.rowStyle, [item, i], style);
  1009. if (style && style.css) {
  1010. for (key in style.css) {
  1011. csses.push(key + ': ' + style.css[key]);
  1012. }
  1013. }
  1014. attributes = calculateObjectValue(this.options,
  1015. this.options.rowAttributes, [item, i], attributes);
  1016. if (attributes) {
  1017. for (key in attributes) {
  1018. htmlAttributes.push(sprintf('%s="%s"', key, escapeHTML(attributes[key])));
  1019. }
  1020. }
  1021. html.push('<tr',
  1022. sprintf(' %s', htmlAttributes.join(' ')),
  1023. sprintf(' id="%s"', $.isArray(item) ? undefined : item._id),
  1024. sprintf(' class="%s"', style.classes || ($.isArray(item) ? undefined : item._class)),
  1025. sprintf(' data-index="%s"', i),
  1026. sprintf(' data-unique-id="%s"', item[this.options.uniqueId]),
  1027. '>'
  1028. );
  1029. if (this.options.cardView) {
  1030. html.push(sprintf('<td colspan="%s">', this.header.fields.length));
  1031. }
  1032. $.each(this.header.fields, function (j, field) {
  1033. var text = '',
  1034. value = item[field],
  1035. type = '',
  1036. cellStyle = {},
  1037. id_ = '',
  1038. class_ = that.header.classes[j],
  1039. data_ = '',
  1040. column = that.options.columns[getFieldIndex(that.options.columns, field)];
  1041. style = sprintf('style="%s"', csses.concat(that.header.styles[j]).join('; '));
  1042. value = calculateObjectValue(that.header,
  1043. that.header.formatters[j], [value, item, i], value);
  1044. // handle td's id and class
  1045. if (item['_' + field + '_id']) {
  1046. id_ = sprintf(' id="%s"', item['_' + field + '_id']);
  1047. }
  1048. if (item['_' + field + '_class']) {
  1049. class_ = sprintf(' class="%s"', item['_' + field + '_class']);
  1050. }
  1051. cellStyle = calculateObjectValue(that.header,
  1052. that.header.cellStyles[j], [value, item, i], cellStyle);
  1053. if (cellStyle.classes) {
  1054. class_ = sprintf(' class="%s"', cellStyle.classes);
  1055. }
  1056. if (cellStyle.css) {
  1057. var csses_ = [];
  1058. for (var key in cellStyle.css) {
  1059. csses_.push(key + ': ' + cellStyle.css[key]);
  1060. }
  1061. style = sprintf('style="%s"', csses_.concat(that.header.styles[j]).join('; '));
  1062. }
  1063. if (item['_' + field + '_data'] && !$.isEmptyObject(item['_' + field + '_data'])) {
  1064. $.each(item['_' + field + '_data'], function (k, v) {
  1065. // ignore data-index
  1066. if (k === 'index') {
  1067. return;
  1068. }
  1069. data_ += sprintf(' data-%s="%s"', k, v);
  1070. });
  1071. }
  1072. if (column.checkbox || column.radio) {
  1073. type = column.checkbox ? 'checkbox' : type;
  1074. type = column.radio ? 'radio' : type;
  1075. text = [that.options.cardView ?
  1076. '<div class="card-view">' : '<td class="bs-checkbox">',
  1077. '<input' +
  1078. sprintf(' data-index="%s"', i) +
  1079. sprintf(' name="%s"', that.options.selectItemName) +
  1080. sprintf(' type="%s"', type) +
  1081. sprintf(' value="%s"', item[that.options.idField]) +
  1082. sprintf(' checked="%s"', value === true ||
  1083. (value && value.checked) ? 'checked' : undefined) +
  1084. sprintf(' disabled="%s"', !column.checkboxEnabled ||
  1085. (value && value.disabled) ? 'disabled' : undefined) +
  1086. ' />',
  1087. that.options.cardView ? '</div>' : '</td>'].join('');
  1088. } else {
  1089. value = typeof value === 'undefined' || value === null ?
  1090. that.options.undefinedText : value;
  1091. text = that.options.cardView ?
  1092. ['<div class="card-view">',
  1093. that.options.showHeader ? sprintf('<span class="title" %s>%s</span>', style,
  1094. getPropertyFromOther(that.options.columns, 'field', 'title', field)) : '',
  1095. sprintf('<span class="value">%s</span>', value),
  1096. '</div>'].join('') :
  1097. [sprintf('<td%s %s %s %s>', id_, class_, style, data_),
  1098. value,
  1099. '</td>'].join('');
  1100. // Hide empty data on Card view when smartDisplay is set to true.
  1101. if (that.options.cardView && that.options.smartDisplay && value === '') {
  1102. text = '';
  1103. }
  1104. }
  1105. html.push(text);
  1106. });
  1107. if (this.options.cardView) {
  1108. html.push('</td>');
  1109. }
  1110. html.push('</tr>');
  1111. }
  1112. // show no records
  1113. if (!html.length) {
  1114. html.push('<tr class="no-records-found">',
  1115. sprintf('<td colspan="%s">%s</td>', this.header.fields.length, this.options.formatNoMatches()),
  1116. '</tr>');
  1117. }
  1118. this.$body.html(html.join(''));
  1119. if (!fixedScroll) {
  1120. this.scrollTo(0);
  1121. }
  1122. // click to select by column
  1123. this.$body.find('> tr > td').off('click').on('click', function () {
  1124. var $tr = $(this).parent();
  1125. that.trigger('click-row', that.data[$tr.data('index')], $tr);
  1126. // if click to select - then trigger the checkbox/radio click
  1127. if (that.options.clickToSelect) {
  1128. if (that.header.clickToSelects[$tr.children().index($(this))]) {
  1129. $tr.find(sprintf('[name="%s"]',
  1130. that.options.selectItemName))[0].click(); // #144: .trigger('click') bug
  1131. }
  1132. }
  1133. });
  1134. this.$body.find('tr').off('dblclick').on('dblclick', function () {
  1135. that.trigger('dbl-click-row', that.data[$(this).data('index')], $(this));
  1136. });
  1137. this.$selectItem = this.$body.find(sprintf('[name="%s"]', this.options.selectItemName));
  1138. this.$selectItem.off('click').on('click', function (event) {
  1139. event.stopImmediatePropagation();
  1140. var checked = $(this).prop('checked'),
  1141. row = that.data[$(this).data('index')];
  1142. row[that.header.stateField] = checked;
  1143. that.trigger(checked ? 'check' : 'uncheck', row);
  1144. if (that.options.singleSelect) {
  1145. that.$selectItem.not(this).each(function () {
  1146. that.data[$(this).data('index')][that.header.stateField] = false;
  1147. });
  1148. that.$selectItem.filter(':checked').not(this).prop('checked', false);
  1149. }
  1150. that.updateSelected();
  1151. });
  1152. $.each(this.header.events, function (i, events) {
  1153. if (!events) {
  1154. return;
  1155. }
  1156. // fix bug, if events is defined with namespace
  1157. if (typeof events === 'string') {
  1158. events = calculateObjectValue(null, events);
  1159. }
  1160. for (var key in events) {
  1161. that.$body.find('tr').each(function () {
  1162. var $tr = $(this),
  1163. $td = $tr.find(that.options.cardView ? '.card-view' : 'td').eq(i),
  1164. index = key.indexOf(' '),
  1165. name = key.substring(0, index),
  1166. el = key.substring(index + 1),
  1167. func = events[key];
  1168. $td.find(el).off(name).on(name, function (e) {
  1169. var index = $tr.data('index'),
  1170. row = that.data[index],
  1171. value = row[that.header.fields[i]];
  1172. func.apply(this, [e, value, row, index]);
  1173. });
  1174. });
  1175. }
  1176. });
  1177. this.updateSelected();
  1178. this.resetView();
  1179. this.trigger('post-body');
  1180. };
  1181. BootstrapTable.prototype.initServer = function (silent, query) {
  1182. var that = this,
  1183. data = {},
  1184. params = {
  1185. pageSize: this.options.pageSize === this.options.formatAllRows() ?
  1186. this.options.totalRows : this.options.pageSize,
  1187. pageNumber: this.options.pageNumber,
  1188. searchText: this.searchText,
  1189. sortName: this.options.sortName,
  1190. sortOrder: this.options.sortOrder
  1191. },
  1192. request;
  1193. if (!this.options.url && !this.options.ajax) {
  1194. return;
  1195. }
  1196. if (this.options.queryParamsType === 'limit') {
  1197. params = {
  1198. search: params.searchText,
  1199. sort: params.sortName,
  1200. order: params.sortOrder
  1201. };
  1202. if (this.options.pagination) {
  1203. params.limit = this.options.pageSize === this.options.formatAllRows() ?
  1204. this.options.totalRows : this.options.pageSize;
  1205. params.offset = this.options.pageSize === this.options.formatAllRows() ?
  1206. 0 : this.options.pageSize * (this.options.pageNumber - 1);
  1207. }
  1208. }
  1209. if (!($.isEmptyObject(this.filterColumnsPartial))) {
  1210. params['filter'] = JSON.stringify(this.filterColumnsPartial, null);
  1211. }
  1212. data = calculateObjectValue(this.options, this.options.queryParams, [params], data);
  1213. $.extend(data, query || {});
  1214. // false to stop request
  1215. if (data === false) {
  1216. return;
  1217. }
  1218. if (!silent) {
  1219. this.$loading.show();
  1220. }
  1221. request = $.extend({}, calculateObjectValue(null, this.options.ajaxOptions), {
  1222. type: this.options.method,
  1223. url: this.options.url,
  1224. data: this.options.contentType === 'application/json' && this.options.method === 'post' ?
  1225. JSON.stringify(data) : data,
  1226. cache: this.options.cache,
  1227. contentType: this.options.contentType,
  1228. dataType: this.options.dataType,
  1229. success: function (res) {
  1230. res = calculateObjectValue(that.options, that.options.responseHandler, [res], res);
  1231. that.load(res);
  1232. that.trigger('load-success', res);
  1233. },
  1234. error: function (res) {
  1235. that.trigger('load-error', res.status);
  1236. },
  1237. complete: function () {
  1238. if (!silent) {
  1239. that.$loading.hide();
  1240. }
  1241. }
  1242. });
  1243. if (this.options.ajax) {
  1244. calculateObjectValue(this, this.options.ajax, [request], null);
  1245. } else {
  1246. $.ajax(request);
  1247. }
  1248. };
  1249. BootstrapTable.prototype.getCaretHtml = function () {
  1250. return ['<span class="order' + (this.options.sortOrder === 'desc' ? '' : ' dropup') + '">',
  1251. '<span class="caret" style="margin: 10px 5px;"></span>',
  1252. '</span>'].join('');
  1253. };
  1254. BootstrapTable.prototype.updateSelected = function () {
  1255. var checkAll = this.$selectItem.filter(':enabled').length ===
  1256. this.$selectItem.filter(':enabled').filter(':checked').length;
  1257. this.$selectAll.add(this.$selectAll_).prop('checked', checkAll);
  1258. this.$selectItem.each(function () {
  1259. $(this).parents('tr')[$(this).prop('checked') ? 'addClass' : 'removeClass']('selected');
  1260. });
  1261. };
  1262. BootstrapTable.prototype.updateRows = function (checked) {
  1263. var that = this;
  1264. this.$selectItem.each(function () {
  1265. that.data[$(this).data('index')][that.header.stateField] = checked;
  1266. });
  1267. };
  1268. BootstrapTable.prototype.resetRows = function () {
  1269. var that = this;
  1270. $.each(this.data, function (i, row) {
  1271. that.$selectAll.prop('checked', false);
  1272. that.$selectItem.prop('checked', false);
  1273. row[that.header.stateField] = false;
  1274. });
  1275. };
  1276. BootstrapTable.prototype.trigger = function (name) {
  1277. var args = Array.prototype.slice.call(arguments, 1);
  1278. name += '.bs.table';
  1279. this.options[BootstrapTable.EVENTS[name]].apply(this.options, args);
  1280. this.$el.trigger($.Event(name), args);
  1281. this.options.onAll(name, args);
  1282. this.$el.trigger($.Event('all.bs.table'), [name, args]);
  1283. };
  1284. BootstrapTable.prototype.resetHeader = function () {
  1285. // fix #61: the hidden table reset header bug.
  1286. // fix bug: get $el.css('width') error sometime (height = 500)
  1287. clearTimeout(this.timeoutId_);
  1288. this.timeoutId_ = setTimeout($.proxy(this.fitHeader, this), this.$el.is(':hidden') ? 100 : 0);
  1289. };
  1290. BootstrapTable.prototype.fitHeader = function () {
  1291. var that = this,
  1292. $fixedHeader,
  1293. $fixedBody,
  1294. fixedBody,
  1295. scrollWidth;
  1296. if (that.$el.is(':hidden')) {
  1297. that.timeoutFooter_ = setTimeout($.proxy(that.fitHeader, that), 100);
  1298. return;
  1299. }
  1300. $fixedHeader = this.$container.find('.fixed-table-header');
  1301. $fixedBody = this.$container.find('.fixed-table-body');
  1302. fixedBody = $fixedBody.get(0);
  1303. scrollWidth = fixedBody.scrollWidth > fixedBody.clientWidth &&
  1304. fixedBody.scrollHeight > fixedBody.clientHeight + this.$header.height() ?
  1305. getScrollBarWidth() : 0;
  1306. this.$el.css('margin-top', -this.$header.height());
  1307. this.$header_ = this.$header.clone(true, true);
  1308. this.$selectAll_ = this.$header_.find('[name="btSelectAll"]');
  1309. $fixedHeader.css({
  1310. 'margin-right': scrollWidth
  1311. }).find('table').css('width', this.$el.css('width'))
  1312. .html('').attr('class', this.$el.attr('class'))
  1313. .append(this.$header_);
  1314. // fix bug: $.data() is not working as expected after $.append()
  1315. this.$header.find('th').each(function (i) {
  1316. that.$header_.find('th').eq(i).data($(this).data());
  1317. });
  1318. this.$body.find('tr:first-child:not(.no-records-found) > *').each(function (i) {
  1319. that.$header_.find('div.fht-cell').eq(i).width($(this).innerWidth());
  1320. });
  1321. // horizontal scroll event
  1322. // TODO: it's probably better improving the layout than binding to scroll event
  1323. $fixedBody.off('scroll').on('scroll', function () {
  1324. $fixedHeader.scrollLeft($(this).scrollLeft());
  1325. });
  1326. that.trigger('post-header');
  1327. };
  1328. BootstrapTable.prototype.resetFooter = function () {
  1329. var that = this,
  1330. data = that.getData(),
  1331. html = [];
  1332. if (!this.options.showFooter || this.options.cardView) { //do nothing
  1333. return;
  1334. }
  1335. $.each(this.options.columns, function (i, column) {
  1336. var falign = '', // footer align style
  1337. style = '',
  1338. class_ = sprintf(' class="%s"', column['class']);
  1339. if (!column.visible) {
  1340. return;
  1341. }
  1342. if (that.options.cardView && (!column.cardVisible)) {
  1343. return;
  1344. }
  1345. falign = sprintf('text-align: %s; ', column.falign ? column.falign : column.align);
  1346. style = sprintf('vertical-align: %s; ', column.valign);
  1347. html.push('<td', class_, sprintf(' style="%s"', falign + style), '>');
  1348. html.push(calculateObjectValue(column, column.footerFormatter, [data], '&nbsp;') || '&nbsp;');
  1349. html.push('</td>');
  1350. });
  1351. this.$footer.find('tr').html(html.join(''));
  1352. clearTimeout(this.timeoutFooter_);
  1353. this.timeoutFooter_ = setTimeout($.proxy(this.fitFooter, this),
  1354. this.$el.is(':hidden') ? 100 : 0);
  1355. };
  1356. BootstrapTable.prototype.fitFooter = function () {
  1357. var that = this,
  1358. $fixedBody,
  1359. $footerTd,
  1360. elWidth,
  1361. scrollWidth;
  1362. clearTimeout(this.timeoutFooter_);
  1363. if (this.$el.is(':hidden')) {
  1364. this.timeoutFooter_ = setTimeout($.proxy(this.fitFooter, this), 100);
  1365. return;
  1366. }
  1367. $fixedBody = this.$container.find('.fixed-table-body');
  1368. elWidth = this.$el.css('width');
  1369. scrollWidth = elWidth > $fixedBody.width() ? getScrollBarWidth() : 0;
  1370. this.$footer.css({
  1371. 'margin-right': scrollWidth
  1372. }).find('table').css('width', elWidth)
  1373. .attr('class', this.$el.attr('class'));
  1374. $footerTd = this.$footer.find('td');
  1375. $fixedBody.find('tbody tr:first-child:not(.no-records-found) > td').each(function (i) {
  1376. $footerTd.eq(i).outerWidth($(this).outerWidth());
  1377. });
  1378. };
  1379. BootstrapTable.prototype.toggleColumn = function (index, checked, needUpdate) {
  1380. if (index === -1) {
  1381. return;
  1382. }
  1383. this.options.columns[index].visible = checked;
  1384. this.initHeader();
  1385. this.initSearch();
  1386. this.initPagination();
  1387. this.initBody();
  1388. if (this.options.showColumns) {
  1389. var $items = this.$toolbar.find('.keep-open input').prop('disabled', false);
  1390. if (needUpdate) {
  1391. $items.filter(sprintf('[value="%s"]', index)).prop('checked', checked);
  1392. }
  1393. if ($items.filter(':checked').length <= this.options.minimumCountColumns) {
  1394. $items.filter(':checked').prop('disabled', true);
  1395. }
  1396. }
  1397. };
  1398. BootstrapTable.prototype.toggleRow = function (index, isIdField, visible) {
  1399. if (index === -1) {
  1400. return;
  1401. }
  1402. $(this.$body[0]).children().filter(sprintf(isIdField ? '[value="%s"]' : '[data-index="%s"]', index))
  1403. [visible ? 'show' : 'hide']();
  1404. };
  1405. // PUBLIC FUNCTION DEFINITION
  1406. // =======================
  1407. BootstrapTable.prototype.resetView = function (params) {
  1408. var that = this,
  1409. padding = 0,
  1410. $tableContainer = that.$container.find('.fixed-table-container');
  1411. if (params && params.height) {
  1412. this.options.height = params.height;
  1413. }
  1414. this.$selectAll.prop('checked', this.$selectItem.length > 0 &&
  1415. this.$selectItem.length === this.$selectItem.filter(':checked').length);
  1416. if (this.options.height) {
  1417. var toolbarHeight = getRealHeight(this.$toolbar),
  1418. paginationHeight = getRealHeight(this.$pagination),
  1419. height = this.options.height - toolbarHeight - paginationHeight;
  1420. $tableContainer.css('height', height + 'px');
  1421. }
  1422. if (this.options.cardView) {
  1423. // remove the element css
  1424. that.$el.css('margin-top', '0');
  1425. $tableContainer.css('padding-bottom', '0');
  1426. return;
  1427. }
  1428. if (this.options.showHeader && this.options.height) {
  1429. this.$container.find('.fixed-table-header').show();
  1430. this.resetHeader();
  1431. padding += cellHeight;
  1432. } else {
  1433. this.$container.find('.fixed-table-header').hide();
  1434. this.trigger('post-header');
  1435. }
  1436. if (this.options.showFooter) {
  1437. this.resetFooter();
  1438. if (this.options.height) {
  1439. padding += cellHeight;
  1440. }
  1441. }
  1442. $tableContainer.css('padding-bottom', padding + 'px');
  1443. };
  1444. BootstrapTable.prototype.getData = function (useCurrentPage) {
  1445. return (this.searchText
  1446. || !$.isEmptyObject(this.filterColumns)
  1447. || !$.isEmptyObject(this.filterColumnsPartial)) ?
  1448. (useCurrentPage ? this.data.slice(this.pageFrom -1, this.pageTo)
  1449. : this.data) :
  1450. (useCurrentPage ? this.options.data.slice(this.pageFrom - 1, this.pageTo)
  1451. : this.options.data);
  1452. };
  1453. BootstrapTable.prototype.load = function (data) {
  1454. var fixedScroll = false;
  1455. // #431: support pagination
  1456. if (this.options.sidePagination === 'server') {
  1457. this.options.totalRows = data.total;
  1458. fixedScroll = data.fixedScroll;
  1459. data = data.rows;
  1460. } else if (!$.isArray(data)) { // support fixedScroll
  1461. fixedScroll = data.fixedScroll;
  1462. data = data.data;
  1463. }
  1464. this.initData(data);
  1465. this.initSearch();
  1466. this.initPagination();
  1467. this.initBody(fixedScroll);
  1468. };
  1469. BootstrapTable.prototype.append = function (data) {
  1470. this.initData(data, 'append');
  1471. this.initSearch();
  1472. this.initPagination();
  1473. this.initBody(true);
  1474. };
  1475. BootstrapTable.prototype.prepend = function (data) {
  1476. this.initData(data, 'prepend');
  1477. this.initSearch();
  1478. this.initPagination();
  1479. this.initBody(true);
  1480. };
  1481. BootstrapTable.prototype.remove = function (params) {
  1482. var len = this.options.data.length,
  1483. i, row;
  1484. if (!params.hasOwnProperty('field') || !params.hasOwnProperty('values')) {
  1485. return;
  1486. }
  1487. for (i = len - 1; i >= 0; i--) {
  1488. row = this.options.data[i];
  1489. if (!row.hasOwnProperty(params.field)) {
  1490. continue;
  1491. }
  1492. if ($.inArray(row[params.field], params.values) !== -1) {
  1493. this.options.data.splice(i, 1);
  1494. }
  1495. }
  1496. if (len === this.options.data.length) {
  1497. return;
  1498. }
  1499. this.initSearch();
  1500. this.initPagination();
  1501. this.initBody(true);
  1502. };
  1503. BootstrapTable.prototype.insertRow = function (params) {
  1504. if (!params.hasOwnProperty('index') || !params.hasOwnProperty('row')) {
  1505. return;
  1506. }
  1507. this.data.splice(params.index, 0, params.row);
  1508. this.initSearch();
  1509. this.initPagination();
  1510. this.initBody(true);
  1511. };
  1512. BootstrapTable.prototype.updateRow = function (params) {
  1513. if (!params.hasOwnProperty('index') || !params.hasOwnProperty('row')) {
  1514. return;
  1515. }
  1516. $.extend(this.data[params.index], params.row);
  1517. this.initBody(true);
  1518. };
  1519. BootstrapTable.prototype.showRow = function (params) {
  1520. if (!params.hasOwnProperty('index')) {
  1521. return;
  1522. }
  1523. this.toggleRow(params.index, params.isIdField === undefined ? false : true, true);
  1524. };
  1525. BootstrapTable.prototype.hideRow = function (params) {
  1526. if (!params.hasOwnProperty('index')) {
  1527. return;
  1528. }
  1529. this.toggleRow(params.index, params.isIdField === undefined ? false : true, false);
  1530. };
  1531. BootstrapTable.prototype.getRowsHidden = function (show) {
  1532. var rows = $(this.$body[0]).children().filter(':hidden'),
  1533. i = 0;
  1534. if (show) {
  1535. for (; i < rows.length; i++) {
  1536. $(rows[i]).show();
  1537. }
  1538. }
  1539. return rows;
  1540. }
  1541. BootstrapTable.prototype.mergeCells = function (options) {
  1542. var row = options.index,
  1543. col = $.inArray(options.field, this.header.fields),
  1544. rowspan = options.rowspan || 1,
  1545. colspan = options.colspan || 1,
  1546. i, j,
  1547. $tr = this.$body.find('tr'),
  1548. $td = $tr.eq(row).find('td').eq(col);
  1549. if (row < 0 || col < 0 || row >= this.data.length) {
  1550. return;
  1551. }
  1552. for (i = row; i < row + rowspan; i++) {
  1553. for (j = col; j < col + colspan; j++) {
  1554. $tr.eq(i).find('td').eq(j).hide();
  1555. }
  1556. }
  1557. $td.attr('rowspan', rowspan).attr('colspan', colspan).show();
  1558. };
  1559. BootstrapTable.prototype.getOptions = function () {
  1560. return this.options;
  1561. };
  1562. BootstrapTable.prototype.getSelections = function () {
  1563. var that = this;
  1564. return $.grep(this.data, function (row) {
  1565. return row[that.header.stateField];
  1566. });
  1567. };
  1568. BootstrapTable.prototype.checkAll = function () {
  1569. this.checkAll_(true);
  1570. };
  1571. BootstrapTable.prototype.uncheckAll = function () {
  1572. this.checkAll_(false);
  1573. };
  1574. BootstrapTable.prototype.checkAll_ = function (checked) {
  1575. var rows;
  1576. if (!checked) {
  1577. rows = this.getSelections();
  1578. }
  1579. this.$selectItem.filter(':enabled').prop('checked', checked);
  1580. this.updateRows(checked);
  1581. this.updateSelected();
  1582. if (checked) {
  1583. rows = this.getSelections();
  1584. }
  1585. this.trigger(checked ? 'check-all' : 'uncheck-all', rows);
  1586. };
  1587. BootstrapTable.prototype.check = function (index) {
  1588. this.check_(true, index);
  1589. };
  1590. BootstrapTable.prototype.uncheck = function (index) {
  1591. this.check_(false, index);
  1592. };
  1593. BootstrapTable.prototype.check_ = function (checked, index) {
  1594. this.$selectItem.filter(sprintf('[data-index="%s"]', index)).prop('checked', checked);
  1595. this.data[index][this.header.stateField] = checked;
  1596. this.updateSelected();
  1597. this.trigger(checked ? 'check' : 'uncheck', this.data[index]);
  1598. };
  1599. BootstrapTable.prototype.checkBy = function (obj) {
  1600. this.checkBy_(true, obj);
  1601. };
  1602. BootstrapTable.prototype.uncheckBy = function (obj) {
  1603. this.checkBy_(false, obj);
  1604. };
  1605. BootstrapTable.prototype.checkBy_ = function (checked, obj) {
  1606. if (!obj.hasOwnProperty('field') || !obj.hasOwnProperty('values')) {
  1607. return;
  1608. }
  1609. var that = this;
  1610. $.each(this.options.data, function (index, row) {
  1611. if (!row.hasOwnProperty(obj.field)) {
  1612. return false;
  1613. }
  1614. if ($.inArray(row[obj.field], obj.values) !== -1) {
  1615. that.$selectItem.filter(sprintf('[data-index="%s"]', index)).prop('checked', checked);
  1616. row[that.header.stateField] = checked;
  1617. that.trigger(checked ? 'check' : 'uncheck', row);
  1618. }
  1619. });
  1620. this.updateSelected();
  1621. };
  1622. BootstrapTable.prototype.destroy = function () {
  1623. this.$el.insertBefore(this.$container);
  1624. $(this.options.toolbar).insertBefore(this.$el);
  1625. this.$container.next().remove();
  1626. this.$container.remove();
  1627. this.$el.html(this.$el_.html())
  1628. .css('margin-top', '0')
  1629. .attr('class', this.$el_.attr('class') || ''); // reset the class
  1630. };
  1631. BootstrapTable.prototype.showLoading = function () {
  1632. this.$loading.show();
  1633. };
  1634. BootstrapTable.prototype.hideLoading = function () {
  1635. this.$loading.hide();
  1636. };
  1637. BootstrapTable.prototype.togglePagination = function () {
  1638. this.options.pagination = !this.options.pagination;
  1639. var button = this.$toolbar.find('button[name="paginationSwitch"] i');
  1640. if (this.options.pagination) {
  1641. button.attr("class", this.options.iconsPrefix + " " + this.options.icons.paginationSwitchDown);
  1642. } else {
  1643. button.attr("class", this.options.iconsPrefix + " " + this.options.icons.paginationSwitchUp);
  1644. }
  1645. this.updatePagination();
  1646. };
  1647. BootstrapTable.prototype.refresh = function (params) {
  1648. if (params && params.url) {
  1649. this.options.url = params.url;
  1650. this.options.pageNumber = 1;
  1651. }
  1652. this.initServer(params && params.silent, params && params.query);
  1653. };
  1654. BootstrapTable.prototype.resetWidth = function () {
  1655. if (this.options.showHeader && this.options.height) {
  1656. this.fitHeader();
  1657. }
  1658. if (this.options.showFooter) {
  1659. this.fitFooter();
  1660. }
  1661. };
  1662. BootstrapTable.prototype.showColumn = function (field) {
  1663. this.toggleColumn(getFieldIndex(this.options.columns, field), true, true);
  1664. };
  1665. BootstrapTable.prototype.hideColumn = function (field) {
  1666. this.toggleColumn(getFieldIndex(this.options.columns, field), false, true);
  1667. };
  1668. BootstrapTable.prototype.filterBy = function (columns) {
  1669. this.filterColumns = $.isEmptyObject(columns) ? {} : columns;
  1670. this.options.pageNumber = 1;
  1671. this.initSearch();
  1672. this.updatePagination();
  1673. };
  1674. BootstrapTable.prototype.scrollTo = function (value) {
  1675. var $tbody = this.$container.find('.fixed-table-body');
  1676. if (typeof value === 'string') {
  1677. value = value === 'bottom' ? $tbody[0].scrollHeight : 0;
  1678. }
  1679. if (typeof value === 'number') {
  1680. $tbody.scrollTop(value);
  1681. }
  1682. if (typeof value === undefined) {
  1683. return $tbody.scrollTop();
  1684. }
  1685. };
  1686. BootstrapTable.prototype.getScrollPosition = function () {
  1687. return this.scrollTo();
  1688. }
  1689. BootstrapTable.prototype.selectPage = function (page) {
  1690. if (page > 0 && page <= this.options.totalPages) {
  1691. this.options.pageNumber = page;
  1692. this.updatePagination();
  1693. }
  1694. };
  1695. BootstrapTable.prototype.prevPage = function () {
  1696. if (this.options.pageNumber > 1) {
  1697. this.options.pageNumber--;
  1698. this.updatePagination();
  1699. }
  1700. };
  1701. BootstrapTable.prototype.nextPage = function () {
  1702. if (this.options.pageNumber < this.options.totalPages) {
  1703. this.options.pageNumber++;
  1704. this.updatePagination();
  1705. }
  1706. };
  1707. BootstrapTable.prototype.toggleView = function () {
  1708. this.options.cardView = !this.options.cardView;
  1709. this.initHeader();
  1710. // Fixed remove toolbar when click cardView button.
  1711. //that.initToolbar();
  1712. this.initBody();
  1713. this.trigger('toggle', this.options.cardView);
  1714. };
  1715. // BOOTSTRAP TABLE PLUGIN DEFINITION
  1716. // =======================
  1717. var allowedMethods = [
  1718. 'getOptions',
  1719. 'getSelections', 'getData',
  1720. 'load', 'append', 'prepend', 'remove',
  1721. 'insertRow', 'updateRow',
  1722. 'showRow', 'hideRow', 'getRowsHidden',
  1723. 'mergeCells',
  1724. 'checkAll', 'uncheckAll',
  1725. 'check', 'uncheck',
  1726. 'checkBy', 'uncheckBy',
  1727. 'refresh',
  1728. 'resetView',
  1729. 'resetWidth',
  1730. 'destroy',
  1731. 'showLoading', 'hideLoading',
  1732. 'showColumn', 'hideColumn',
  1733. 'filterBy',
  1734. 'scrollTo',
  1735. 'getScrollPosition',
  1736. 'selectPage', 'prevPage', 'nextPage',
  1737. 'togglePagination',
  1738. 'toggleView'
  1739. ];
  1740. $.fn.bootstrapTable = function (option, _relatedTarget) {
  1741. var value;
  1742. this.each(function () {
  1743. var $this = $(this),
  1744. data = $this.data('bootstrap.table'),
  1745. options = $.extend({}, BootstrapTable.DEFAULTS, $this.data(),
  1746. typeof option === 'object' && option);
  1747. if (typeof option === 'string') {
  1748. if ($.inArray(option, allowedMethods) < 0) {
  1749. throw new Error("Unknown method: " + option);
  1750. }
  1751. if (!data) {
  1752. return;
  1753. }
  1754. value = data[option](_relatedTarget);
  1755. if (option === 'destroy') {
  1756. $this.removeData('bootstrap.table');
  1757. }
  1758. }
  1759. if (!data) {
  1760. $this.data('bootstrap.table', (data = new BootstrapTable(this, options)));
  1761. }
  1762. });
  1763. return typeof value === 'undefined' ? this : value;
  1764. };
  1765. $.fn.bootstrapTable.Constructor = BootstrapTable;
  1766. $.fn.bootstrapTable.defaults = BootstrapTable.DEFAULTS;
  1767. $.fn.bootstrapTable.columnDefaults = BootstrapTable.COLUMN_DEFAULTS;
  1768. $.fn.bootstrapTable.locales = BootstrapTable.LOCALES;
  1769. $.fn.bootstrapTable.methods = allowedMethods;
  1770. // BOOTSTRAP TABLE INIT
  1771. // =======================
  1772. $(function () {
  1773. $('[data-toggle="table"]').bootstrapTable();
  1774. });
  1775. }(jQuery);