bootstrap-table.js 73 KB

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