bootstrap-table.js 71 KB

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