bootstrap-table.js 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660
  1. /**
  2. * @author zhixin wen <wenzhixin2010@gmail.com>
  3. * version: 1.9.0
  4. * https://github.com/wenzhixin/bootstrap-table/
  5. */
  6. !function ($) {
  7. 'use strict';
  8. // TOOLS DEFINITION
  9. // ======================
  10. var cachedWidth = null;
  11. // it only does '%s', and return '' when arguments are undefined
  12. var sprintf = function (str) {
  13. var args = arguments,
  14. flag = true,
  15. i = 1;
  16. str = str.replace(/%s/g, function () {
  17. var arg = args[i++];
  18. if (typeof arg === 'undefined') {
  19. flag = false;
  20. return '';
  21. }
  22. return arg;
  23. });
  24. return flag ? str : '';
  25. };
  26. var getPropertyFromOther = function (list, from, to, value) {
  27. var result = '';
  28. $.each(list, function (i, item) {
  29. if (item[from] === value) {
  30. result = item[to];
  31. return false;
  32. }
  33. return true;
  34. });
  35. return result;
  36. };
  37. var getFieldIndex = function (columns, field) {
  38. var index = -1;
  39. $.each(columns, function (i, column) {
  40. if (column.field === field) {
  41. index = i;
  42. return false;
  43. }
  44. return true;
  45. });
  46. return index;
  47. };
  48. // http://jsfiddle.net/wenyi/47nz7ez9/3/
  49. var setFieldIndex = function (columns) {
  50. var i, j, k,
  51. totalCol = 0,
  52. flag = [];
  53. for (i = 0; i < columns[0].length; i++) {
  54. totalCol += columns[0][i].colspan || 1;
  55. }
  56. for (i = 0; i < columns.length; i++) {
  57. flag[i] = [];
  58. for (j = 0; j < totalCol; j++) {
  59. flag[i][j] = false;
  60. }
  61. }
  62. for (i = 0; i < columns.length; i++) {
  63. for (j = 0; j < columns[i].length; j++) {
  64. var r = columns[i][j],
  65. rowspan = r.rowspan || 1,
  66. colspan = r.colspan || 1,
  67. index = $.inArray(false, flag[i]);
  68. if (colspan === 1) {
  69. r.fieldIndex = index;
  70. // when field is undefined, use index instead
  71. if (typeof r.field === 'undefined') {
  72. r.field = index;
  73. }
  74. }
  75. for (k = 0; k < rowspan; k++) {
  76. flag[i + k][index] = true;
  77. }
  78. for (k = 0; k < colspan; k++) {
  79. flag[i][index + k] = true;
  80. }
  81. }
  82. }
  83. };
  84. var getScrollBarWidth = function () {
  85. if (cachedWidth === null) {
  86. var inner = $('<p/>').addClass('fixed-table-scroll-inner'),
  87. outer = $('<div/>').addClass('fixed-table-scroll-outer'),
  88. w1, w2;
  89. outer.append(inner);
  90. $('body').append(outer);
  91. w1 = inner[0].offsetWidth;
  92. outer.css('overflow', 'scroll');
  93. w2 = inner[0].offsetWidth;
  94. if (w1 === w2) {
  95. w2 = outer[0].clientWidth;
  96. }
  97. outer.remove();
  98. cachedWidth = w1 - w2;
  99. }
  100. return cachedWidth;
  101. };
  102. var calculateObjectValue = function (self, name, args, defaultValue) {
  103. var func = name;
  104. if (typeof name === 'string') {
  105. // support obj.func1.func2
  106. var names = name.split('.');
  107. if (names.length > 1) {
  108. func = window;
  109. $.each(names, function (i, f) {
  110. func = func[f];
  111. });
  112. } else {
  113. func = window[name];
  114. }
  115. }
  116. if (typeof func === 'object') {
  117. return func;
  118. }
  119. if (typeof func === 'function') {
  120. return func.apply(self, args);
  121. }
  122. if (!func && typeof name === 'string' && sprintf.apply(this, [name].concat(args))) {
  123. return sprintf.apply(this, [name].concat(args));
  124. }
  125. return defaultValue;
  126. };
  127. var compareObjects = function (objectA, objectB, compareLength) {
  128. // Create arrays of property names
  129. var objectAProperties = Object.getOwnPropertyNames(objectA),
  130. objectBProperties = Object.getOwnPropertyNames(objectB),
  131. propName = '';
  132. if (compareLength) {
  133. // If number of properties is different, objects are not equivalent
  134. if (objectAProperties.length !== objectBProperties.length) {
  135. return false;
  136. }
  137. }
  138. for (var i = 0; i < objectAProperties.length; i++) {
  139. propName = objectAProperties[i];
  140. // If the property is not in the object B properties, continue with the next property
  141. if ($.inArray(propName, objectBProperties) > -1) {
  142. // If values of same property are not equal, objects are not equivalent
  143. if (objectA[propName] !== objectB[propName]) {
  144. return false;
  145. }
  146. }
  147. }
  148. // If we made it this far, objects are considered equivalent
  149. return true;
  150. };
  151. var escapeHTML = function (text) {
  152. if (typeof text === 'string') {
  153. return text
  154. .replace(/&/g, "&amp;")
  155. .replace(/</g, "&lt;")
  156. .replace(/>/g, "&gt;")
  157. .replace(/"/g, "&quot;")
  158. .replace(/'/g, "&#039;");
  159. }
  160. return text;
  161. };
  162. var getRealHeight = function ($el) {
  163. var height = 0;
  164. $el.children().each(function () {
  165. if (height < $(this).outerHeight(true)) {
  166. height = $(this).outerHeight(true);
  167. }
  168. });
  169. return height;
  170. };
  171. var getRealDataAttr = function (dataAttr) {
  172. for (var attr in dataAttr) {
  173. var auxAttr = attr.split(/(?=[A-Z])/).join('-').toLowerCase();
  174. if (auxAttr !== attr) {
  175. dataAttr[auxAttr] = dataAttr[attr];
  176. delete dataAttr[attr];
  177. }
  178. }
  179. return dataAttr;
  180. };
  181. var getItemField = function (item, field) {
  182. var value = item;
  183. if (typeof field !== 'string' || item.hasOwnProperty(field)) {
  184. return item[field];
  185. }
  186. var props = field.split('.');
  187. for (var p in props) {
  188. value = value[props[p]];
  189. }
  190. return value;
  191. };
  192. // BOOTSTRAP TABLE CLASS DEFINITION
  193. // ======================
  194. var BootstrapTable = function (el, options) {
  195. this.options = options;
  196. this.$el = $(el);
  197. this.$el_ = this.$el.clone();
  198. this.timeoutId_ = 0;
  199. this.timeoutFooter_ = 0;
  200. this.init();
  201. };
  202. BootstrapTable.DEFAULTS = {
  203. classes: 'table table-hover',
  204. locale: undefined,
  205. height: undefined,
  206. undefinedText: '-',
  207. sortName: undefined,
  208. sortOrder: 'asc',
  209. striped: false,
  210. columns: [[]],
  211. data: [],
  212. dataField: 'rows',
  213. method: 'get',
  214. url: undefined,
  215. ajax: undefined,
  216. cache: true,
  217. contentType: 'application/json',
  218. dataType: 'json',
  219. ajaxOptions: {},
  220. queryParams: function (params) {
  221. return params;
  222. },
  223. queryParamsType: 'limit', // undefined
  224. responseHandler: function (res) {
  225. return res;
  226. },
  227. pagination: false,
  228. onlyInfoPagination: false,
  229. sidePagination: 'client', // client or server
  230. totalRows: 0, // server side need to set
  231. pageNumber: 1,
  232. pageSize: 10,
  233. pageList: [10, 25, 50, 100],
  234. paginationHAlign: 'right', //right, left
  235. paginationVAlign: 'bottom', //bottom, top, both
  236. paginationDetailHAlign: 'left', //right, left
  237. paginationFirstText: '&laquo;',
  238. paginationPreText: '&lsaquo;',
  239. paginationNextText: '&rsaquo;',
  240. paginationLastText: '&raquo;',
  241. search: false,
  242. strictSearch: false,
  243. searchAlign: 'right',
  244. selectItemName: 'btSelectItem',
  245. showHeader: true,
  246. showFooter: false,
  247. showColumns: false,
  248. showPaginationSwitch: false,
  249. showRefresh: false,
  250. showToggle: false,
  251. buttonsAlign: 'right',
  252. smartDisplay: true,
  253. minimumCountColumns: 1,
  254. idField: undefined,
  255. uniqueId: undefined,
  256. cardView: false,
  257. detailView: false,
  258. detailFormatter: function (index, row) {
  259. return '';
  260. },
  261. trimOnSearch: true,
  262. clickToSelect: false,
  263. singleSelect: false,
  264. toolbar: undefined,
  265. toolbarAlign: 'left',
  266. checkboxHeader: true,
  267. sortable: true,
  268. silentSort: true,
  269. maintainSelected: false,
  270. searchTimeOut: 500,
  271. searchText: '',
  272. iconSize: undefined,
  273. iconsPrefix: 'glyphicon', // glyphicon of fa (font awesome)
  274. icons: {
  275. paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
  276. paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
  277. refresh: 'glyphicon-refresh icon-refresh',
  278. toggle: 'glyphicon-list-alt icon-list-alt',
  279. columns: 'glyphicon-th icon-th',
  280. detailOpen: 'glyphicon-plus icon-plus',
  281. detailClose: 'glyphicon-minus icon-minus'
  282. },
  283. rowStyle: function (row, index) {
  284. return {};
  285. },
  286. rowAttributes: function (row, index) {
  287. return {};
  288. },
  289. onAll: function (name, args) {
  290. return false;
  291. },
  292. onClickCell: function (field, value, row, $element) {
  293. return false;
  294. },
  295. onDblClickCell: function (field, value, row, $element) {
  296. return false;
  297. },
  298. onClickRow: function (item, $element) {
  299. return false;
  300. },
  301. onDblClickRow: function (item, $element) {
  302. return false;
  303. },
  304. onSort: function (name, order) {
  305. return false;
  306. },
  307. onCheck: function (row) {
  308. return false;
  309. },
  310. onUncheck: function (row) {
  311. return false;
  312. },
  313. onCheckAll: function (rows) {
  314. return false;
  315. },
  316. onUncheckAll: function (rows) {
  317. return false;
  318. },
  319. onCheckSome: function (rows) {
  320. return false;
  321. },
  322. onUncheckSome: function (rows) {
  323. return false;
  324. },
  325. onLoadSuccess: function (data) {
  326. return false;
  327. },
  328. onLoadError: function (status) {
  329. return false;
  330. },
  331. onColumnSwitch: function (field, checked) {
  332. return false;
  333. },
  334. onPageChange: function (number, size) {
  335. return false;
  336. },
  337. onSearch: function (text) {
  338. return false;
  339. },
  340. onToggle: function (cardView) {
  341. return false;
  342. },
  343. onPreBody: function (data) {
  344. return false;
  345. },
  346. onPostBody: function () {
  347. return false;
  348. },
  349. onPostHeader: function () {
  350. return false;
  351. },
  352. onExpandRow: function (index, row, $detail) {
  353. return false;
  354. },
  355. onCollapseRow: function (index, row) {
  356. return false;
  357. },
  358. onRefreshOptions: function (options) {
  359. return false;
  360. },
  361. onResetView: function () {
  362. return false;
  363. }
  364. };
  365. BootstrapTable.LOCALES = [];
  366. BootstrapTable.LOCALES['en-US'] = BootstrapTable.LOCALES['en'] = {
  367. formatLoadingMessage: function () {
  368. return 'Loading, please wait...';
  369. },
  370. formatRecordsPerPage: function (pageNumber) {
  371. return sprintf('%s records per page', pageNumber);
  372. },
  373. formatShowingRows: function (pageFrom, pageTo, totalRows) {
  374. return sprintf('Showing %s to %s of %s rows', pageFrom, pageTo, totalRows);
  375. },
  376. formatDetailPagination: function (totalRows) {
  377. return sprintf('Showing %s rows', totalRows);
  378. },
  379. formatSearch: function () {
  380. return 'Search';
  381. },
  382. formatNoMatches: function () {
  383. return 'No matching records found';
  384. },
  385. formatPaginationSwitch: function () {
  386. return 'Hide/Show pagination';
  387. },
  388. formatRefresh: function () {
  389. return 'Refresh';
  390. },
  391. formatToggle: function () {
  392. return 'Toggle';
  393. },
  394. formatColumns: function () {
  395. return 'Columns';
  396. },
  397. formatAllRows: function () {
  398. return 'All';
  399. }
  400. };
  401. $.extend(BootstrapTable.DEFAULTS, BootstrapTable.LOCALES['en-US']);
  402. BootstrapTable.COLUMN_DEFAULTS = {
  403. radio: false,
  404. checkbox: false,
  405. checkboxEnabled: true,
  406. field: undefined,
  407. title: undefined,
  408. titleTooltip: undefined,
  409. 'class': undefined,
  410. align: undefined, // left, right, center
  411. halign: undefined, // left, right, center
  412. falign: undefined, // left, right, center
  413. valign: undefined, // top, middle, bottom
  414. width: undefined,
  415. sortable: false,
  416. order: 'asc', // asc, desc
  417. visible: true,
  418. switchable: true,
  419. clickToSelect: true,
  420. formatter: undefined,
  421. footerFormatter: undefined,
  422. events: undefined,
  423. sorter: undefined,
  424. sortName: undefined,
  425. cellStyle: undefined,
  426. searchable: true,
  427. searchFormatter: true,
  428. cardVisible: true
  429. };
  430. BootstrapTable.EVENTS = {
  431. 'all.bs.table': 'onAll',
  432. 'click-cell.bs.table': 'onClickCell',
  433. 'dbl-click-cell.bs.table': 'onDblClickCell',
  434. 'click-row.bs.table': 'onClickRow',
  435. 'dbl-click-row.bs.table': 'onDblClickRow',
  436. 'sort.bs.table': 'onSort',
  437. 'check.bs.table': 'onCheck',
  438. 'uncheck.bs.table': 'onUncheck',
  439. 'check-all.bs.table': 'onCheckAll',
  440. 'uncheck-all.bs.table': 'onUncheckAll',
  441. 'check-some.bs.table': 'onCheckSome',
  442. 'uncheck-some.bs.table': 'onUncheckSome',
  443. 'load-success.bs.table': 'onLoadSuccess',
  444. 'load-error.bs.table': 'onLoadError',
  445. 'column-switch.bs.table': 'onColumnSwitch',
  446. 'page-change.bs.table': 'onPageChange',
  447. 'search.bs.table': 'onSearch',
  448. 'toggle.bs.table': 'onToggle',
  449. 'pre-body.bs.table': 'onPreBody',
  450. 'post-body.bs.table': 'onPostBody',
  451. 'post-header.bs.table': 'onPostHeader',
  452. 'expand-row.bs.table': 'onExpandRow',
  453. 'collapse-row.bs.table': 'onCollapseRow',
  454. 'refresh-options.bs.table': 'onRefreshOptions',
  455. 'reset-view.bs.table': 'onResetView'
  456. };
  457. BootstrapTable.prototype.init = function () {
  458. this.initLocale();
  459. this.initContainer();
  460. this.initTable();
  461. this.initHeader();
  462. this.initData();
  463. this.initFooter();
  464. this.initToolbar();
  465. this.initPagination();
  466. this.initBody();
  467. this.initSearchText();
  468. this.initServer();
  469. };
  470. BootstrapTable.prototype.initLocale = function () {
  471. if (this.options.locale) {
  472. var parts = this.options.locale.split(/-|_/);
  473. parts[0].toLowerCase();
  474. parts[1] && parts[1].toUpperCase();
  475. if ($.fn.bootstrapTable.locales[this.options.locale]) {
  476. // locale as requested
  477. $.extend(this.options, $.fn.bootstrapTable.locales[this.options.locale]);
  478. } else if ($.fn.bootstrapTable.locales[parts.join('-')]) {
  479. // locale with sep set to - (in case original was specified with _)
  480. $.extend(this.options, $.fn.bootstrapTable.locales[parts.join('-')]);
  481. } else if ($.fn.bootstrapTable.locales[parts[0]]) {
  482. // short locale language code (i.e. 'en')
  483. $.extend(this.options, $.fn.bootstrapTable.locales[parts[0]]);
  484. }
  485. }
  486. };
  487. BootstrapTable.prototype.initContainer = function () {
  488. this.$container = $([
  489. '<div class="bootstrap-table">',
  490. '<div class="fixed-table-toolbar"></div>',
  491. this.options.paginationVAlign === 'top' || this.options.paginationVAlign === 'both' ?
  492. '<div class="fixed-table-pagination" style="clear: both;"></div>' :
  493. '',
  494. '<div class="fixed-table-container">',
  495. '<div class="fixed-table-header"><table></table></div>',
  496. '<div class="fixed-table-body">',
  497. '<div class="fixed-table-loading">',
  498. this.options.formatLoadingMessage(),
  499. '</div>',
  500. '</div>',
  501. '<div class="fixed-table-footer"><table><tr></tr></table></div>',
  502. this.options.paginationVAlign === 'bottom' || this.options.paginationVAlign === 'both' ?
  503. '<div class="fixed-table-pagination"></div>' :
  504. '',
  505. '</div>',
  506. '</div>'
  507. ].join(''));
  508. this.$container.insertAfter(this.$el);
  509. this.$tableContainer = this.$container.find('.fixed-table-container');
  510. this.$tableHeader = this.$container.find('.fixed-table-header');
  511. this.$tableBody = this.$container.find('.fixed-table-body');
  512. this.$tableLoading = this.$container.find('.fixed-table-loading');
  513. this.$tableFooter = this.$container.find('.fixed-table-footer');
  514. this.$toolbar = this.$container.find('.fixed-table-toolbar');
  515. this.$pagination = this.$container.find('.fixed-table-pagination');
  516. this.$tableBody.append(this.$el);
  517. this.$container.after('<div class="clearfix"></div>');
  518. this.$el.addClass(this.options.classes);
  519. if (this.options.striped) {
  520. this.$el.addClass('table-striped');
  521. }
  522. if ($.inArray('table-no-bordered', this.options.classes.split(' ')) !== -1) {
  523. this.$tableContainer.addClass('table-no-bordered');
  524. }
  525. };
  526. BootstrapTable.prototype.initTable = function () {
  527. var that = this,
  528. columns = [],
  529. data = [];
  530. this.$header = this.$el.find('>thead');
  531. if (!this.$header.length) {
  532. this.$header = $('<thead></thead>').appendTo(this.$el);
  533. }
  534. this.$header.find('tr').each(function () {
  535. var column = [];
  536. $(this).find('th').each(function () {
  537. column.push($.extend({}, {
  538. title: $(this).html(),
  539. 'class': $(this).attr('class'),
  540. titleTooltip: $(this).attr('title'),
  541. rowspan: $(this).attr('rowspan') ? +$(this).attr('rowspan') : undefined,
  542. colspan: $(this).attr('colspan') ? +$(this).attr('colspan') : undefined
  543. }, $(this).data()));
  544. });
  545. columns.push(column);
  546. });
  547. if (!$.isArray(this.options.columns[0])) {
  548. this.options.columns = [this.options.columns];
  549. }
  550. this.options.columns = $.extend(true, [], columns, this.options.columns);
  551. this.columns = [];
  552. setFieldIndex(this.options.columns);
  553. $.each(this.options.columns, function (i, columns) {
  554. $.each(columns, function (j, column) {
  555. column = $.extend({}, BootstrapTable.COLUMN_DEFAULTS, column);
  556. if (typeof column.fieldIndex !== 'undefined') {
  557. that.columns[column.fieldIndex] = column;
  558. }
  559. that.options.columns[i][j] = column;
  560. });
  561. });
  562. // if options.data is setting, do not process tbody data
  563. if (this.options.data.length) {
  564. return;
  565. }
  566. this.$el.find('>tbody>tr').each(function () {
  567. var row = {};
  568. // save tr's id, class and data-* attributes
  569. row._id = $(this).attr('id');
  570. row._class = $(this).attr('class');
  571. row._data = getRealDataAttr($(this).data());
  572. $(this).find('td').each(function (i) {
  573. var field = that.columns[i].field;
  574. row[field] = $(this).html();
  575. // save td's id, class and data-* attributes
  576. row['_' + field + '_id'] = $(this).attr('id');
  577. row['_' + field + '_class'] = $(this).attr('class');
  578. row['_' + field + '_rowspan'] = $(this).attr('rowspan');
  579. row['_' + field + '_title'] = $(this).attr('title');
  580. row['_' + field + '_data'] = getRealDataAttr($(this).data());
  581. });
  582. data.push(row);
  583. });
  584. this.options.data = data;
  585. };
  586. BootstrapTable.prototype.initHeader = function () {
  587. var that = this,
  588. visibleColumns = {},
  589. html = [];
  590. this.header = {
  591. fields: [],
  592. styles: [],
  593. classes: [],
  594. formatters: [],
  595. events: [],
  596. sorters: [],
  597. sortNames: [],
  598. cellStyles: [],
  599. searchables: []
  600. };
  601. $.each(this.options.columns, function (i, columns) {
  602. html.push('<tr>');
  603. if (i == 0 && !that.options.cardView && that.options.detailView) {
  604. html.push(sprintf('<th class="detail" rowspan="%s"><div class="fht-cell"></div></th>',
  605. that.options.columns.length));
  606. }
  607. $.each(columns, function (j, column) {
  608. var text = '',
  609. halign = '', // header align style
  610. align = '', // body align style
  611. style = '',
  612. class_ = sprintf(' class="%s"', column['class']),
  613. order = that.options.sortOrder || column.order,
  614. unitWidth = 'px',
  615. width = column.width;
  616. if (column.width !== undefined && (!that.options.cardView)) {
  617. if (typeof column.width === 'string') {
  618. if (column.width.indexOf('%') !== -1) {
  619. unitWidth = '%';
  620. }
  621. }
  622. }
  623. if (column.width && typeof column.width === 'string') {
  624. width = column.width.replace('%', '').replace('px', '');
  625. }
  626. halign = sprintf('text-align: %s; ', column.halign ? column.halign : column.align);
  627. align = sprintf('text-align: %s; ', column.align);
  628. style = sprintf('vertical-align: %s; ', column.valign);
  629. style += sprintf('width: %s; ', (column.checkbox || column.radio) && !width ?
  630. '36px' : (width ? width + unitWidth : undefined));
  631. if (typeof column.fieldIndex !== 'undefined') {
  632. that.header.fields[column.fieldIndex] = column.field;
  633. that.header.styles[column.fieldIndex] = align + style;
  634. that.header.classes[column.fieldIndex] = class_;
  635. that.header.formatters[column.fieldIndex] = column.formatter;
  636. that.header.events[column.fieldIndex] = column.events;
  637. that.header.sorters[column.fieldIndex] = column.sorter;
  638. that.header.sortNames[column.fieldIndex] = column.sortName;
  639. that.header.cellStyles[column.fieldIndex] = column.cellStyle;
  640. that.header.searchables[column.fieldIndex] = column.searchable;
  641. if (!column.visible) {
  642. return;
  643. }
  644. if (that.options.cardView && (!column.cardVisible)) {
  645. return;
  646. }
  647. visibleColumns[column.field] = column;
  648. }
  649. html.push('<th' + sprintf(' title="%s"', column.titleTooltip),
  650. column.checkbox || column.radio ?
  651. sprintf(' class="bs-checkbox %s"', column['class'] || '') :
  652. class_,
  653. sprintf(' style="%s"', halign + style),
  654. sprintf(' rowspan="%s"', column.rowspan),
  655. sprintf(' colspan="%s"', column.colspan),
  656. sprintf(' data-field="%s"', column.field),
  657. "tabindex='0'",
  658. '>');
  659. html.push(sprintf('<div class="th-inner %s">', that.options.sortable && column.sortable ?
  660. 'sortable both' : ''));
  661. text = column.title;
  662. if (column.checkbox) {
  663. if (!that.options.singleSelect && that.options.checkboxHeader) {
  664. text = '<input name="btSelectAll" type="checkbox" />';
  665. }
  666. that.header.stateField = column.field;
  667. }
  668. if (column.radio) {
  669. text = '';
  670. that.header.stateField = column.field;
  671. that.options.singleSelect = true;
  672. }
  673. html.push(text);
  674. html.push('</div>');
  675. html.push('<div class="fht-cell"></div>');
  676. html.push('</div>');
  677. html.push('</th>');
  678. });
  679. html.push('</tr>');
  680. });
  681. this.$header.html(html.join(''));
  682. this.$header.find('th[data-field]').each(function (i) {
  683. $(this).data(visibleColumns[$(this).data('field')]);
  684. });
  685. this.$container.off('click', '.th-inner').on('click', '.th-inner', function (event) {
  686. if (that.options.sortable && $(this).parent().data().sortable) {
  687. that.onSort(event);
  688. }
  689. });
  690. this.$header.children().children().off('keypress').on('keypress', function (event) {
  691. if (that.options.sortable && $(this).data().sortable) {
  692. var code = event.keyCode || event.which;
  693. if (code == 13) { //Enter keycode
  694. that.onSort(event);
  695. }
  696. }
  697. });
  698. if (!this.options.showHeader || this.options.cardView) {
  699. this.$header.hide();
  700. this.$tableHeader.hide();
  701. this.$tableLoading.css('top', 0);
  702. } else {
  703. this.$header.show();
  704. this.$tableHeader.show();
  705. this.$tableLoading.css('top', this.$header.outerHeight() + 1);
  706. // Assign the correct sortable arrow
  707. this.getCaret();
  708. }
  709. this.$selectAll = this.$header.find('[name="btSelectAll"]');
  710. this.$container.off('click', '[name="btSelectAll"]')
  711. .on('click', '[name="btSelectAll"]', function () {
  712. var checked = $(this).prop('checked');
  713. that[checked ? 'checkAll' : 'uncheckAll']();
  714. that.updateSelected();
  715. });
  716. };
  717. BootstrapTable.prototype.initFooter = function () {
  718. if (!this.options.showFooter || this.options.cardView) {
  719. this.$tableFooter.hide();
  720. } else {
  721. this.$tableFooter.show();
  722. }
  723. };
  724. /**
  725. * @param data
  726. * @param type: append / prepend
  727. */
  728. BootstrapTable.prototype.initData = function (data, type) {
  729. if (type === 'append') {
  730. this.data = this.data.concat(data);
  731. } else if (type === 'prepend') {
  732. this.data = [].concat(data).concat(this.data);
  733. } else {
  734. this.data = data || this.options.data;
  735. }
  736. // Fix #839 Records deleted when adding new row on filtered table
  737. if (type === 'append') {
  738. this.options.data = this.options.data.concat(data);
  739. } else if (type === 'prepend') {
  740. this.options.data = [].concat(data).concat(this.options.data);
  741. } else {
  742. this.options.data = this.data;
  743. }
  744. if (this.options.sidePagination === 'server') {
  745. return;
  746. }
  747. this.initSort();
  748. };
  749. BootstrapTable.prototype.initSort = function () {
  750. var that = this,
  751. name = this.options.sortName,
  752. order = this.options.sortOrder === 'desc' ? -1 : 1,
  753. index = $.inArray(this.options.sortName, this.header.fields);
  754. if (index !== -1) {
  755. this.data.sort(function (a, b) {
  756. if (that.header.sortNames[index]) {
  757. name = that.header.sortNames[index];
  758. }
  759. var aa = getItemField(a, name),
  760. bb = getItemField(b, name),
  761. value = calculateObjectValue(that.header, that.header.sorters[index], [aa, bb]);
  762. if (value !== undefined) {
  763. return order * value;
  764. }
  765. // Fix #161: undefined or null string sort bug.
  766. if (aa === undefined || aa === null) {
  767. aa = '';
  768. }
  769. if (bb === undefined || bb === null) {
  770. bb = '';
  771. }
  772. // IF both values are numeric, do a numeric comparison
  773. if ($.isNumeric(aa) && $.isNumeric(bb)) {
  774. // Convert numerical values form string to float.
  775. aa = parseFloat(aa);
  776. bb = parseFloat(bb);
  777. if (aa < bb) {
  778. return order * -1;
  779. }
  780. return order;
  781. }
  782. if (aa === bb) {
  783. return 0;
  784. }
  785. // If value is not a string, convert to string
  786. if (typeof aa !== 'string') {
  787. aa = aa.toString();
  788. }
  789. if (aa.localeCompare(bb) === -1) {
  790. return order * -1;
  791. }
  792. return order;
  793. });
  794. }
  795. };
  796. BootstrapTable.prototype.onSort = function (event) {
  797. var $this = event.type === "keypress" ? $(event.currentTarget) : $(event.currentTarget).parent(),
  798. $this_ = this.$header.find('th').eq($this.index());
  799. this.$header.add(this.$header_).find('span.order').remove();
  800. if (this.options.sortName === $this.data('field')) {
  801. this.options.sortOrder = this.options.sortOrder === 'asc' ? 'desc' : 'asc';
  802. } else {
  803. this.options.sortName = $this.data('field');
  804. this.options.sortOrder = $this.data('order') === 'asc' ? 'desc' : 'asc';
  805. }
  806. this.trigger('sort', this.options.sortName, this.options.sortOrder);
  807. $this.add($this_).data('order', this.options.sortOrder);
  808. // Assign the correct sortable arrow
  809. this.getCaret();
  810. if (this.options.sidePagination === 'server') {
  811. this.initServer(this.options.silentSort);
  812. return;
  813. }
  814. this.initSort();
  815. this.initBody();
  816. };
  817. BootstrapTable.prototype.initToolbar = function () {
  818. var that = this,
  819. html = [],
  820. timeoutId = 0,
  821. $keepOpen,
  822. $search,
  823. switchableCount = 0;
  824. this.$toolbar.html('');
  825. if (typeof this.options.toolbar === 'string' || typeof this.options.toolbar === 'object') {
  826. $(sprintf('<div class="bars pull-%s"></div>', this.options.toolbarAlign))
  827. .appendTo(this.$toolbar)
  828. .append($(this.options.toolbar));
  829. }
  830. // showColumns, showToggle, showRefresh
  831. html = [sprintf('<div class="columns columns-%s btn-group pull-%s">',
  832. this.options.buttonsAlign, this.options.buttonsAlign)];
  833. if (typeof this.options.icons === 'string') {
  834. this.options.icons = calculateObjectValue(null, this.options.icons);
  835. }
  836. if (this.options.showPaginationSwitch) {
  837. html.push(sprintf('<button class="btn btn-default" type="button" name="paginationSwitch" title="%s">',
  838. this.options.formatPaginationSwitch()),
  839. sprintf('<i class="%s %s"></i>', this.options.iconsPrefix, this.options.icons.paginationSwitchDown),
  840. '</button>');
  841. }
  842. if (this.options.showRefresh) {
  843. html.push(sprintf('<button class="btn btn-default' + (this.options.iconSize === undefined ? '' : ' btn-' + this.options.iconSize) + '" type="button" name="refresh" title="%s">',
  844. this.options.formatRefresh()),
  845. sprintf('<i class="%s %s"></i>', this.options.iconsPrefix, this.options.icons.refresh),
  846. '</button>');
  847. }
  848. if (this.options.showToggle) {
  849. html.push(sprintf('<button class="btn btn-default' + (this.options.iconSize === undefined ? '' : ' btn-' + this.options.iconSize) + '" type="button" name="toggle" title="%s">',
  850. this.options.formatToggle()),
  851. sprintf('<i class="%s %s"></i>', this.options.iconsPrefix, this.options.icons.toggle),
  852. '</button>');
  853. }
  854. if (this.options.showColumns) {
  855. html.push(sprintf('<div class="keep-open btn-group" title="%s">',
  856. this.options.formatColumns()),
  857. '<button type="button" class="btn btn-default' + (this.options.iconSize == undefined ? '' : ' btn-' + this.options.iconSize) + ' dropdown-toggle" data-toggle="dropdown">',
  858. sprintf('<i class="%s %s"></i>', this.options.iconsPrefix, this.options.icons.columns),
  859. ' <span class="caret"></span>',
  860. '</button>',
  861. '<ul class="dropdown-menu" role="menu">');
  862. $.each(this.columns, function (i, column) {
  863. if (column.radio || column.checkbox) {
  864. return;
  865. }
  866. if (that.options.cardView && (!column.cardVisible)) {
  867. return;
  868. }
  869. var checked = column.visible ? ' checked="checked"' : '';
  870. if (column.switchable) {
  871. html.push(sprintf('<li>' +
  872. '<label><input type="checkbox" data-field="%s" value="%s"%s> %s</label>' +
  873. '</li>', column.field, i, checked, column.title));
  874. switchableCount++;
  875. }
  876. });
  877. html.push('</ul>',
  878. '</div>');
  879. }
  880. html.push('</div>');
  881. // Fix #188: this.showToolbar is for extentions
  882. if (this.showToolbar || html.length > 2) {
  883. this.$toolbar.append(html.join(''));
  884. }
  885. if (this.options.showPaginationSwitch) {
  886. this.$toolbar.find('button[name="paginationSwitch"]')
  887. .off('click').on('click', $.proxy(this.togglePagination, this));
  888. }
  889. if (this.options.showRefresh) {
  890. this.$toolbar.find('button[name="refresh"]')
  891. .off('click').on('click', $.proxy(this.refresh, this));
  892. }
  893. if (this.options.showToggle) {
  894. this.$toolbar.find('button[name="toggle"]')
  895. .off('click').on('click', function () {
  896. that.toggleView();
  897. });
  898. }
  899. if (this.options.showColumns) {
  900. $keepOpen = this.$toolbar.find('.keep-open');
  901. if (switchableCount <= this.options.minimumCountColumns) {
  902. $keepOpen.find('input').prop('disabled', true);
  903. }
  904. $keepOpen.find('li').off('click').on('click', function (event) {
  905. event.stopImmediatePropagation();
  906. });
  907. $keepOpen.find('input').off('click').on('click', function () {
  908. var $this = $(this);
  909. that.toggleColumn(getFieldIndex(that.columns,
  910. $(this).data('field')), $this.prop('checked'), false);
  911. that.trigger('column-switch', $(this).data('field'), $this.prop('checked'));
  912. });
  913. }
  914. if (this.options.search) {
  915. html = [];
  916. html.push(
  917. '<div class="pull-' + this.options.searchAlign + ' search">',
  918. sprintf('<input class="form-control' + (this.options.iconSize === undefined ? '' : ' input-' + this.options.iconSize) + '" type="text" placeholder="%s">',
  919. this.options.formatSearch()),
  920. '</div>');
  921. this.$toolbar.append(html.join(''));
  922. $search = this.$toolbar.find('.search input');
  923. $search.off('keyup drop').on('keyup drop', function (event) {
  924. clearTimeout(timeoutId); // doesn't matter if it's 0
  925. timeoutId = setTimeout(function () {
  926. that.onSearch(event);
  927. }, that.options.searchTimeOut);
  928. });
  929. }
  930. };
  931. BootstrapTable.prototype.onSearch = function (event) {
  932. var text = $.trim($(event.currentTarget).val());
  933. // trim search input
  934. if (this.options.trimOnSearch && $(event.currentTarget).val() !== text) {
  935. $(event.currentTarget).val(text);
  936. }
  937. if (text === this.searchText) {
  938. return;
  939. }
  940. this.searchText = text;
  941. this.options.pageNumber = 1;
  942. this.initSearch();
  943. this.updatePagination();
  944. this.trigger('search', text);
  945. };
  946. BootstrapTable.prototype.initSearch = function () {
  947. var that = this;
  948. if (this.options.sidePagination !== 'server') {
  949. var s = this.searchText && this.searchText.toLowerCase();
  950. var f = $.isEmptyObject(this.filterColumns) ? null : this.filterColumns;
  951. // Check filter
  952. this.data = f ? $.grep(this.options.data, function (item, i) {
  953. for (var key in f) {
  954. if (item[key] !== f[key]) {
  955. return false;
  956. }
  957. }
  958. return true;
  959. }) : this.options.data;
  960. this.data = s ? $.grep(this.data, function (item, i) {
  961. for (var key in item) {
  962. key = $.isNumeric(key) ? parseInt(key, 10) : key;
  963. var value = item[key],
  964. column = that.columns[getFieldIndex(that.columns, key)],
  965. j = $.inArray(key, that.header.fields);
  966. // Fix #142: search use formated data
  967. if (column && column.searchFormatter) {
  968. value = calculateObjectValue(column,
  969. that.header.formatters[j], [value, item, i], value);
  970. }
  971. var index = $.inArray(key, that.header.fields);
  972. if (index !== -1 && that.header.searchables[index] && (typeof value === 'string' || typeof value === 'number')) {
  973. if (that.options.strictSearch) {
  974. if ((value + '').toLowerCase() === s) {
  975. return true;
  976. }
  977. } else {
  978. if ((value + '').toLowerCase().indexOf(s) !== -1) {
  979. return true;
  980. }
  981. }
  982. }
  983. }
  984. return false;
  985. }) : this.data;
  986. }
  987. };
  988. BootstrapTable.prototype.initPagination = function () {
  989. if (!this.options.pagination) {
  990. this.$pagination.hide();
  991. return;
  992. } else {
  993. this.$pagination.show();
  994. }
  995. var that = this,
  996. html = [],
  997. $allSelected = false,
  998. i, from, to,
  999. $pageList,
  1000. $first, $pre,
  1001. $next, $last,
  1002. $number,
  1003. data = this.getData();
  1004. if (this.options.sidePagination !== 'server') {
  1005. this.options.totalRows = data.length;
  1006. }
  1007. this.totalPages = 0;
  1008. if (this.options.totalRows) {
  1009. if (this.options.pageSize === this.options.formatAllRows()) {
  1010. this.options.pageSize = this.options.totalRows;
  1011. $allSelected = true;
  1012. } else if (this.options.pageSize === this.options.totalRows) {
  1013. // Fix #667 Table with pagination,
  1014. // multiple pages and a search that matches to one page throws exception
  1015. var pageLst = typeof this.options.pageList === 'string' ?
  1016. this.options.pageList.replace('[', '').replace(']', '')
  1017. .replace(/ /g, '').toLowerCase().split(',') : this.options.pageList;
  1018. if ($.inArray(this.options.formatAllRows().toLowerCase(), pageLst) > -1) {
  1019. $allSelected = true;
  1020. }
  1021. }
  1022. this.totalPages = ~~((this.options.totalRows - 1) / this.options.pageSize) + 1;
  1023. this.options.totalPages = this.totalPages;
  1024. }
  1025. if (this.totalPages > 0 && this.options.pageNumber > this.totalPages) {
  1026. this.options.pageNumber = this.totalPages;
  1027. }
  1028. this.pageFrom = (this.options.pageNumber - 1) * this.options.pageSize + 1;
  1029. this.pageTo = this.options.pageNumber * this.options.pageSize;
  1030. if (this.pageTo > this.options.totalRows) {
  1031. this.pageTo = this.options.totalRows;
  1032. }
  1033. html.push(
  1034. '<div class="pull-' + this.options.paginationDetailHAlign + ' pagination-detail">',
  1035. '<span class="pagination-info">',
  1036. this.options.onlyInfoPagination ? this.options.formatDetailPagination(this.options.totalRows) :
  1037. this.options.formatShowingRows(this.pageFrom, this.pageTo, this.options.totalRows),
  1038. '</span>');
  1039. if (!this.options.onlyInfoPagination) {
  1040. html.push('<span class="page-list">');
  1041. var pageNumber = [
  1042. sprintf('<span class="btn-group %s">',
  1043. this.options.paginationVAlign === 'top' || this.options.paginationVAlign === 'both' ?
  1044. 'dropdown' : 'dropup'),
  1045. '<button type="button" class="btn btn-default ' +
  1046. (this.options.iconSize === undefined ? '' : ' btn-' + this.options.iconSize) +
  1047. ' dropdown-toggle" data-toggle="dropdown">',
  1048. '<span class="page-size">',
  1049. $allSelected ? this.options.formatAllRows() : this.options.pageSize,
  1050. '</span>',
  1051. ' <span class="caret"></span>',
  1052. '</button>',
  1053. '<ul class="dropdown-menu" role="menu">'
  1054. ],
  1055. pageList = this.options.pageList;
  1056. if (typeof this.options.pageList === 'string') {
  1057. var list = this.options.pageList.replace('[', '').replace(']', '')
  1058. .replace(/ /g, '').split(',');
  1059. pageList = [];
  1060. $.each(list, function (i, value) {
  1061. pageList.push(value.toUpperCase() === that.options.formatAllRows().toUpperCase() ?
  1062. that.options.formatAllRows() : +value);
  1063. });
  1064. }
  1065. $.each(pageList, function (i, page) {
  1066. if (!that.options.smartDisplay || i === 0 || pageList[i - 1] <= that.options.totalRows) {
  1067. var active;
  1068. if ($allSelected) {
  1069. active = page === that.options.formatAllRows() ? ' class="active"' : '';
  1070. } else {
  1071. active = page === that.options.pageSize ? ' class="active"' : '';
  1072. }
  1073. pageNumber.push(sprintf('<li%s><a href="javascript:void(0)">%s</a></li>', active, page));
  1074. }
  1075. });
  1076. pageNumber.push('</ul></span>');
  1077. html.push(this.options.formatRecordsPerPage(pageNumber.join('')));
  1078. html.push('</span>');
  1079. html.push('</div>',
  1080. '<div class="pull-' + this.options.paginationHAlign + ' pagination">',
  1081. '<ul class="pagination' + (this.options.iconSize === undefined ? '' : ' pagination-' + this.options.iconSize) + '">',
  1082. '<li class="page-first"><a href="javascript:void(0)">' + this.options.paginationFirstText + '</a></li>',
  1083. '<li class="page-pre"><a href="javascript:void(0)">' + this.options.paginationPreText + '</a></li>');
  1084. if (this.totalPages < 5) {
  1085. from = 1;
  1086. to = this.totalPages;
  1087. } else {
  1088. from = this.options.pageNumber - 2;
  1089. to = from + 4;
  1090. if (from < 1) {
  1091. from = 1;
  1092. to = 5;
  1093. }
  1094. if (to > this.totalPages) {
  1095. to = this.totalPages;
  1096. from = to - 4;
  1097. }
  1098. }
  1099. for (i = from; i <= to; i++) {
  1100. html.push('<li class="page-number' + (i === this.options.pageNumber ? ' active' : '') + '">',
  1101. '<a href="javascript:void(0)">', i, '</a>',
  1102. '</li>');
  1103. }
  1104. html.push(
  1105. '<li class="page-next"><a href="javascript:void(0)">' + this.options.paginationNextText + '</a></li>',
  1106. '<li class="page-last"><a href="javascript:void(0)">' + this.options.paginationLastText + '</a></li>',
  1107. '</ul>',
  1108. '</div>');
  1109. }
  1110. this.$pagination.html(html.join(''));
  1111. if (!this.options.onlyInfoPagination) {
  1112. $pageList = this.$pagination.find('.page-list a');
  1113. $first = this.$pagination.find('.page-first');
  1114. $pre = this.$pagination.find('.page-pre');
  1115. $next = this.$pagination.find('.page-next');
  1116. $last = this.$pagination.find('.page-last');
  1117. $number = this.$pagination.find('.page-number');
  1118. if (this.options.pageNumber <= 1) {
  1119. $first.addClass('disabled');
  1120. $pre.addClass('disabled');
  1121. }
  1122. if (this.options.pageNumber >= this.totalPages) {
  1123. $next.addClass('disabled');
  1124. $last.addClass('disabled');
  1125. }
  1126. if (this.options.smartDisplay) {
  1127. if (this.totalPages <= 1) {
  1128. this.$pagination.find('div.pagination').hide();
  1129. }
  1130. if (pageList.length < 2 || this.options.totalRows <= pageList[0]) {
  1131. this.$pagination.find('span.page-list').hide();
  1132. }
  1133. // when data is empty, hide the pagination
  1134. this.$pagination[this.getData().length ? 'show' : 'hide']();
  1135. }
  1136. if ($allSelected) {
  1137. this.options.pageSize = this.options.formatAllRows();
  1138. }
  1139. $pageList.off('click').on('click', $.proxy(this.onPageListChange, this));
  1140. $first.off('click').on('click', $.proxy(this.onPageFirst, this));
  1141. $pre.off('click').on('click', $.proxy(this.onPagePre, this));
  1142. $next.off('click').on('click', $.proxy(this.onPageNext, this));
  1143. $last.off('click').on('click', $.proxy(this.onPageLast, this));
  1144. $number.off('click').on('click', $.proxy(this.onPageNumber, this));
  1145. }
  1146. };
  1147. BootstrapTable.prototype.updatePagination = function (event) {
  1148. // Fix #171: IE disabled button can be clicked bug.
  1149. if (event && $(event.currentTarget).hasClass('disabled')) {
  1150. return;
  1151. }
  1152. if (!this.options.maintainSelected) {
  1153. this.resetRows();
  1154. }
  1155. this.initPagination();
  1156. if (this.options.sidePagination === 'server') {
  1157. this.initServer();
  1158. } else {
  1159. this.initBody();
  1160. }
  1161. this.trigger('page-change', this.options.pageNumber, this.options.pageSize);
  1162. };
  1163. BootstrapTable.prototype.onPageListChange = function (event) {
  1164. var $this = $(event.currentTarget);
  1165. $this.parent().addClass('active').siblings().removeClass('active');
  1166. this.options.pageSize = $this.text().toUpperCase() === this.options.formatAllRows().toUpperCase() ?
  1167. this.options.formatAllRows() : +$this.text();
  1168. this.$toolbar.find('.page-size').text(this.options.pageSize);
  1169. this.updatePagination(event);
  1170. };
  1171. BootstrapTable.prototype.onPageFirst = function (event) {
  1172. this.options.pageNumber = 1;
  1173. this.updatePagination(event);
  1174. };
  1175. BootstrapTable.prototype.onPagePre = function (event) {
  1176. this.options.pageNumber--;
  1177. this.updatePagination(event);
  1178. };
  1179. BootstrapTable.prototype.onPageNext = function (event) {
  1180. this.options.pageNumber++;
  1181. this.updatePagination(event);
  1182. };
  1183. BootstrapTable.prototype.onPageLast = function (event) {
  1184. this.options.pageNumber = this.totalPages;
  1185. this.updatePagination(event);
  1186. };
  1187. BootstrapTable.prototype.onPageNumber = function (event) {
  1188. if (this.options.pageNumber === +$(event.currentTarget).text()) {
  1189. return;
  1190. }
  1191. this.options.pageNumber = +$(event.currentTarget).text();
  1192. this.updatePagination(event);
  1193. };
  1194. BootstrapTable.prototype.initBody = function (fixedScroll) {
  1195. var that = this,
  1196. html = [],
  1197. data = this.getData();
  1198. this.trigger('pre-body', data);
  1199. this.$body = this.$el.find('>tbody');
  1200. if (!this.$body.length) {
  1201. this.$body = $('<tbody></tbody>').appendTo(this.$el);
  1202. }
  1203. //Fix #389 Bootstrap-table-flatJSON is not working
  1204. if (!this.options.pagination || this.options.sidePagination === 'server') {
  1205. this.pageFrom = 1;
  1206. this.pageTo = data.length;
  1207. }
  1208. for (var i = this.pageFrom - 1; i < this.pageTo; i++) {
  1209. var key,
  1210. item = data[i],
  1211. style = {},
  1212. csses = [],
  1213. data_ = '',
  1214. attributes = {},
  1215. htmlAttributes = [];
  1216. style = calculateObjectValue(this.options, this.options.rowStyle, [item, i], style);
  1217. if (style && style.css) {
  1218. for (key in style.css) {
  1219. csses.push(key + ': ' + style.css[key]);
  1220. }
  1221. }
  1222. attributes = calculateObjectValue(this.options,
  1223. this.options.rowAttributes, [item, i], attributes);
  1224. if (attributes) {
  1225. for (key in attributes) {
  1226. htmlAttributes.push(sprintf('%s="%s"', key, escapeHTML(attributes[key])));
  1227. }
  1228. }
  1229. if (item._data && !$.isEmptyObject(item._data)) {
  1230. $.each(item._data, function (k, v) {
  1231. // ignore data-index
  1232. if (k === 'index') {
  1233. return;
  1234. }
  1235. data_ += sprintf(' data-%s="%s"', k, v);
  1236. });
  1237. }
  1238. html.push('<tr',
  1239. sprintf(' %s', htmlAttributes.join(' ')),
  1240. sprintf(' id="%s"', $.isArray(item) ? undefined : item._id),
  1241. sprintf(' class="%s"', style.classes || ($.isArray(item) ? undefined : item._class)),
  1242. sprintf(' data-index="%s"', i),
  1243. sprintf(' data-uniqueid="%s"', item[this.options.uniqueId]),
  1244. sprintf('%s', data_),
  1245. '>'
  1246. );
  1247. if (this.options.cardView) {
  1248. html.push(sprintf('<td colspan="%s">', this.header.fields.length));
  1249. }
  1250. if (!this.options.cardView && this.options.detailView) {
  1251. html.push('<td>',
  1252. '<a class="detail-icon" href="javascript:">',
  1253. sprintf('<i class="%s %s"></i>', this.options.iconsPrefix, this.options.icons.detailOpen),
  1254. '</a>',
  1255. '</td>');
  1256. }
  1257. $.each(this.header.fields, function (j, field) {
  1258. var text = '',
  1259. value = getItemField(item, field),
  1260. type = '',
  1261. cellStyle = {},
  1262. id_ = '',
  1263. class_ = that.header.classes[j],
  1264. data_ = '',
  1265. rowspan_ = '',
  1266. title_ = '',
  1267. column = that.columns[getFieldIndex(that.columns, field)];
  1268. if (!column.visible) {
  1269. return;
  1270. }
  1271. style = sprintf('style="%s"', csses.concat(that.header.styles[j]).join('; '));
  1272. value = calculateObjectValue(column,
  1273. that.header.formatters[j], [value, item, i], value);
  1274. // handle td's id and class
  1275. if (item['_' + field + '_id']) {
  1276. id_ = sprintf(' id="%s"', item['_' + field + '_id']);
  1277. }
  1278. if (item['_' + field + '_class']) {
  1279. class_ = sprintf(' class="%s"', item['_' + field + '_class']);
  1280. }
  1281. if (item['_' + field + '_rowspan']) {
  1282. rowspan_ = sprintf(' rowspan="%s"', item['_' + field + '_rowspan']);
  1283. }
  1284. if (item['_' + field + '_title']) {
  1285. title_ = sprintf(' title="%s"', item['_' + field + '_title']);
  1286. }
  1287. cellStyle = calculateObjectValue(that.header,
  1288. that.header.cellStyles[j], [value, item, i], cellStyle);
  1289. if (cellStyle.classes) {
  1290. class_ = sprintf(' class="%s"', cellStyle.classes);
  1291. }
  1292. if (cellStyle.css) {
  1293. var csses_ = [];
  1294. for (var key in cellStyle.css) {
  1295. csses_.push(key + ': ' + cellStyle.css[key]);
  1296. }
  1297. style = sprintf('style="%s"', csses_.concat(that.header.styles[j]).join('; '));
  1298. }
  1299. if (item['_' + field + '_data'] && !$.isEmptyObject(item['_' + field + '_data'])) {
  1300. $.each(item['_' + field + '_data'], function (k, v) {
  1301. // ignore data-index
  1302. if (k === 'index') {
  1303. return;
  1304. }
  1305. data_ += sprintf(' data-%s="%s"', k, v);
  1306. });
  1307. }
  1308. if (column.checkbox || column.radio) {
  1309. type = column.checkbox ? 'checkbox' : type;
  1310. type = column.radio ? 'radio' : type;
  1311. text = [that.options.cardView ?
  1312. '<div class="card-view">' : '<td class="bs-checkbox">',
  1313. '<input' +
  1314. sprintf(' data-index="%s"', i) +
  1315. sprintf(' name="%s"', that.options.selectItemName) +
  1316. sprintf(' type="%s"', type) +
  1317. sprintf(' value="%s"', item[that.options.idField]) +
  1318. sprintf(' checked="%s"', value === true ||
  1319. (value && value.checked) ? 'checked' : undefined) +
  1320. sprintf(' disabled="%s"', !column.checkboxEnabled ||
  1321. (value && value.disabled) ? 'disabled' : undefined) +
  1322. ' />',
  1323. that.header.formatters[j] && typeof value === 'string' ? value : '',
  1324. that.options.cardView ? '</div>' : '</td>'
  1325. ].join('');
  1326. item[that.header.stateField] = value === true || (value && value.checked);
  1327. } else {
  1328. value = typeof value === 'undefined' || value === null ?
  1329. that.options.undefinedText : value;
  1330. text = that.options.cardView ? ['<div class="card-view">',
  1331. that.options.showHeader ? sprintf('<span class="title" %s>%s</span>', style,
  1332. getPropertyFromOther(that.columns, 'field', 'title', field)) : '',
  1333. sprintf('<span class="value">%s</span>', value),
  1334. '</div>'
  1335. ].join('') : [sprintf('<td%s %s %s %s %s %s>', id_, class_, style, data_, rowspan_, title_),
  1336. value,
  1337. '</td>'
  1338. ].join('');
  1339. // Hide empty data on Card view when smartDisplay is set to true.
  1340. if (that.options.cardView && that.options.smartDisplay && value === '') {
  1341. // Should set a placeholder for event binding correct fieldIndex
  1342. text = '<div class="card-view"></div>';
  1343. }
  1344. }
  1345. html.push(text);
  1346. });
  1347. if (this.options.cardView) {
  1348. html.push('</td>');
  1349. }
  1350. html.push('</tr>');
  1351. }
  1352. // show no records
  1353. if (!html.length) {
  1354. html.push('<tr class="no-records-found">',
  1355. sprintf('<td colspan="%s">%s</td>',
  1356. this.$header.find('th').length, this.options.formatNoMatches()),
  1357. '</tr>');
  1358. }
  1359. this.$body.html(html.join(''));
  1360. if (!fixedScroll) {
  1361. this.scrollTo(0);
  1362. }
  1363. // click to select by column
  1364. this.$body.find('> tr[data-index] > td').off('click dblclick').on('click dblclick', function (e) {
  1365. var $td = $(this),
  1366. $tr = $td.parent(),
  1367. item = that.data[$tr.data('index')],
  1368. index = $td[0].cellIndex,
  1369. field = that.header.fields[that.options.detailView && !that.options.cardView ? index - 1 : index],
  1370. column = that.columns[getFieldIndex(that.columns, field)],
  1371. value = getItemField(item, field);
  1372. if ($td.find('.detail-icon').length) {
  1373. return;
  1374. }
  1375. that.trigger(e.type === 'click' ? 'click-cell' : 'dbl-click-cell', field, value, item, $td);
  1376. that.trigger(e.type === 'click' ? 'click-row' : 'dbl-click-row', item, $tr);
  1377. // if click to select - then trigger the checkbox/radio click
  1378. if (e.type === 'click' && that.options.clickToSelect && column.clickToSelect) {
  1379. var $selectItem = $tr.find(sprintf('[name="%s"]', that.options.selectItemName));
  1380. if ($selectItem.length) {
  1381. $selectItem[0].click(); // #144: .trigger('click') bug
  1382. }
  1383. }
  1384. });
  1385. this.$body.find('> tr[data-index] > td > .detail-icon').off('click').on('click', function () {
  1386. var $this = $(this),
  1387. $tr = $this.parent().parent(),
  1388. index = $tr.data('index'),
  1389. row = data[index]; // Fix #980 Detail view, when searching, returns wrong row
  1390. // remove and update
  1391. if ($tr.next().is('tr.detail-view')) {
  1392. $this.find('i').attr('class', sprintf('%s %s', that.options.iconsPrefix, that.options.icons.detailOpen));
  1393. $tr.next().remove();
  1394. that.trigger('collapse-row', index, row);
  1395. } else {
  1396. $this.find('i').attr('class', sprintf('%s %s', that.options.iconsPrefix, that.options.icons.detailClose));
  1397. $tr.after(sprintf('<tr class="detail-view"><td colspan="%s">%s</td></tr>',
  1398. $tr.find('td').length, calculateObjectValue(that.options,
  1399. that.options.detailFormatter, [index, row], '')));
  1400. that.trigger('expand-row', index, row, $tr.next().find('td'));
  1401. }
  1402. that.resetView();
  1403. });
  1404. this.$selectItem = this.$body.find(sprintf('[name="%s"]', this.options.selectItemName));
  1405. this.$selectItem.off('click').on('click', function (event) {
  1406. event.stopImmediatePropagation();
  1407. var $this = $(this),
  1408. checked = $this.prop('checked'),
  1409. row = that.data[$this.data('index')];
  1410. if (that.options.maintainSelected && $(this).is(':radio')) {
  1411. $.each(that.options.data, function (i, row) {
  1412. row[that.header.stateField] = false;
  1413. });
  1414. }
  1415. row[that.header.stateField] = checked;
  1416. if (that.options.singleSelect) {
  1417. that.$selectItem.not(this).each(function () {
  1418. that.data[$(this).data('index')][that.header.stateField] = false;
  1419. });
  1420. that.$selectItem.filter(':checked').not(this).prop('checked', false);
  1421. }
  1422. that.updateSelected();
  1423. that.trigger(checked ? 'check' : 'uncheck', row, $this);
  1424. });
  1425. $.each(this.header.events, function (i, events) {
  1426. if (!events) {
  1427. return;
  1428. }
  1429. // fix bug, if events is defined with namespace
  1430. if (typeof events === 'string') {
  1431. events = calculateObjectValue(null, events);
  1432. }
  1433. var field = that.header.fields[i],
  1434. fieldIndex = $.inArray(field, that.getVisibleFields());
  1435. if (that.options.detailView && !that.options.cardView) {
  1436. fieldIndex += 1;
  1437. }
  1438. for (var key in events) {
  1439. that.$body.find('>tr:not(.no-records-found)').each(function () {
  1440. var $tr = $(this),
  1441. $td = $tr.find(that.options.cardView ? '.card-view' : 'td').eq(fieldIndex),
  1442. index = key.indexOf(' '),
  1443. name = key.substring(0, index),
  1444. el = key.substring(index + 1),
  1445. func = events[key];
  1446. $td.find(el).off(name).on(name, function (e) {
  1447. var index = $tr.data('index'),
  1448. row = that.data[index],
  1449. value = row[field];
  1450. func.apply(this, [e, value, row, index]);
  1451. });
  1452. });
  1453. }
  1454. });
  1455. this.updateSelected();
  1456. this.resetView();
  1457. this.trigger('post-body');
  1458. };
  1459. BootstrapTable.prototype.initServer = function (silent, query) {
  1460. var that = this,
  1461. data = {},
  1462. params = {
  1463. pageSize: this.options.pageSize === this.options.formatAllRows() ?
  1464. this.options.totalRows : this.options.pageSize,
  1465. pageNumber: this.options.pageNumber,
  1466. searchText: this.searchText,
  1467. sortName: this.options.sortName,
  1468. sortOrder: this.options.sortOrder
  1469. },
  1470. request;
  1471. if (!this.options.url && !this.options.ajax) {
  1472. return;
  1473. }
  1474. if (this.options.queryParamsType === 'limit') {
  1475. params = {
  1476. search: params.searchText,
  1477. sort: params.sortName,
  1478. order: params.sortOrder
  1479. };
  1480. if (this.options.pagination) {
  1481. params.limit = this.options.pageSize === this.options.formatAllRows() ?
  1482. this.options.totalRows : this.options.pageSize;
  1483. params.offset = this.options.pageSize === this.options.formatAllRows() ?
  1484. 0 : this.options.pageSize * (this.options.pageNumber - 1);
  1485. }
  1486. }
  1487. if (!($.isEmptyObject(this.filterColumnsPartial))) {
  1488. params['filter'] = JSON.stringify(this.filterColumnsPartial, null);
  1489. }
  1490. data = calculateObjectValue(this.options, this.options.queryParams, [params], data);
  1491. $.extend(data, query || {});
  1492. // false to stop request
  1493. if (data === false) {
  1494. return;
  1495. }
  1496. if (!silent) {
  1497. this.$tableLoading.show();
  1498. }
  1499. request = $.extend({}, calculateObjectValue(null, this.options.ajaxOptions), {
  1500. type: this.options.method,
  1501. url: this.options.url,
  1502. data: this.options.contentType === 'application/json' && this.options.method === 'post' ?
  1503. JSON.stringify(data) : data,
  1504. cache: this.options.cache,
  1505. contentType: this.options.contentType,
  1506. dataType: this.options.dataType,
  1507. success: function (res) {
  1508. res = calculateObjectValue(that.options, that.options.responseHandler, [res], res);
  1509. that.load(res);
  1510. that.trigger('load-success', res);
  1511. },
  1512. error: function (res) {
  1513. that.trigger('load-error', res.status, res);
  1514. },
  1515. complete: function () {
  1516. if (!silent) {
  1517. that.$tableLoading.hide();
  1518. }
  1519. }
  1520. });
  1521. if (this.options.ajax) {
  1522. calculateObjectValue(this, this.options.ajax, [request], null);
  1523. } else {
  1524. $.ajax(request);
  1525. }
  1526. };
  1527. BootstrapTable.prototype.initSearchText = function () {
  1528. if (this.options.search) {
  1529. if (this.options.searchText !== '') {
  1530. var $search = this.$toolbar.find('.search input');
  1531. $search.val(this.options.searchText);
  1532. this.onSearch({currentTarget: $search});
  1533. }
  1534. }
  1535. };
  1536. BootstrapTable.prototype.getCaret = function () {
  1537. var that = this;
  1538. $.each(this.$header.find('th'), function (i, th) {
  1539. $(th).find('.sortable').removeClass('desc asc').addClass($(th).data('field') === that.options.sortName ? that.options.sortOrder : 'both');
  1540. });
  1541. };
  1542. BootstrapTable.prototype.updateSelected = function () {
  1543. var checkAll = this.$selectItem.filter(':enabled').length &&
  1544. this.$selectItem.filter(':enabled').length ===
  1545. this.$selectItem.filter(':enabled').filter(':checked').length;
  1546. this.$selectAll.add(this.$selectAll_).prop('checked', checkAll);
  1547. this.$selectItem.each(function () {
  1548. $(this).closest('tr')[$(this).prop('checked') ? 'addClass' : 'removeClass']('selected');
  1549. });
  1550. };
  1551. BootstrapTable.prototype.updateRows = function () {
  1552. var that = this;
  1553. this.$selectItem.each(function () {
  1554. that.data[$(this).data('index')][that.header.stateField] = $(this).prop('checked');
  1555. });
  1556. };
  1557. BootstrapTable.prototype.resetRows = function () {
  1558. var that = this;
  1559. $.each(this.data, function (i, row) {
  1560. that.$selectAll.prop('checked', false);
  1561. that.$selectItem.prop('checked', false);
  1562. if (that.header.stateField) {
  1563. row[that.header.stateField] = false;
  1564. }
  1565. });
  1566. };
  1567. BootstrapTable.prototype.trigger = function (name) {
  1568. var args = Array.prototype.slice.call(arguments, 1);
  1569. name += '.bs.table';
  1570. this.options[BootstrapTable.EVENTS[name]].apply(this.options, args);
  1571. this.$el.trigger($.Event(name), args);
  1572. this.options.onAll(name, args);
  1573. this.$el.trigger($.Event('all.bs.table'), [name, args]);
  1574. };
  1575. BootstrapTable.prototype.resetHeader = function () {
  1576. // fix #61: the hidden table reset header bug.
  1577. // fix bug: get $el.css('width') error sometime (height = 500)
  1578. clearTimeout(this.timeoutId_);
  1579. this.timeoutId_ = setTimeout($.proxy(this.fitHeader, this), this.$el.is(':hidden') ? 100 : 0);
  1580. };
  1581. BootstrapTable.prototype.fitHeader = function () {
  1582. var that = this,
  1583. fixedBody,
  1584. scrollWidth,
  1585. focused,
  1586. focusedTemp;
  1587. if (that.$el.is(':hidden')) {
  1588. that.timeoutId_ = setTimeout($.proxy(that.fitHeader, that), 100);
  1589. return;
  1590. }
  1591. fixedBody = this.$tableBody.get(0);
  1592. scrollWidth = fixedBody.scrollWidth > fixedBody.clientWidth &&
  1593. fixedBody.scrollHeight > fixedBody.clientHeight + this.$header.outerHeight() ?
  1594. getScrollBarWidth() : 0;
  1595. this.$el.css('margin-top', -this.$header.outerHeight());
  1596. focused = $(':focus');
  1597. if (focused.length > 0) {
  1598. var $th = focused.parents('th');
  1599. if ($th.length > 0) {
  1600. var dataField = $th.attr('data-field');
  1601. if (dataField !== undefined) {
  1602. var $headerTh = this.$header.find("[data-field='" + dataField + "']");
  1603. if ($headerTh.length > 0) {
  1604. $headerTh.find(":input").addClass("focus-temp");
  1605. }
  1606. }
  1607. }
  1608. }
  1609. this.$header_ = this.$header.clone(true, true);
  1610. this.$selectAll_ = this.$header_.find('[name="btSelectAll"]');
  1611. this.$tableHeader.css({
  1612. 'margin-right': scrollWidth
  1613. }).find('table').css('width', this.$el.outerWidth())
  1614. .html('').attr('class', this.$el.attr('class'))
  1615. .append(this.$header_);
  1616. focusedTemp = $('.focus-temp:visible:eq(0)');
  1617. if (focusedTemp.length > 0) {
  1618. focusedTemp.focus();
  1619. this.$header.find('.focus-temp').removeClass('focus-temp');
  1620. }
  1621. // fix bug: $.data() is not working as expected after $.append()
  1622. this.$header.find('th[data-field]').each(function (i) {
  1623. that.$header_.find(sprintf('th[data-field="%s"]', $(this).data('field'))).data($(this).data());
  1624. });
  1625. var visibleFields = this.getVisibleFields();
  1626. this.$body.find('>tr:first-child:not(.no-records-found) > *').each(function (i) {
  1627. var $this = $(this),
  1628. index = i;
  1629. if (that.options.detailView && !that.options.cardView) {
  1630. if (i === 0) {
  1631. that.$header_.find('th.detail').find('.fht-cell').width($this.innerWidth());
  1632. }
  1633. index = i - 1;
  1634. }
  1635. that.$header_.find(sprintf('th[data-field="%s"]', visibleFields[index]))
  1636. .find('.fht-cell').width($this.innerWidth());
  1637. });
  1638. // horizontal scroll event
  1639. // TODO: it's probably better improving the layout than binding to scroll event
  1640. this.$tableBody.off('scroll').on('scroll', function () {
  1641. that.$tableHeader.scrollLeft($(this).scrollLeft());
  1642. if (that.options.showFooter && !that.options.cardView) {
  1643. that.$tableFooter.scrollLeft($(this).scrollLeft());
  1644. }
  1645. });
  1646. that.trigger('post-header');
  1647. };
  1648. BootstrapTable.prototype.resetFooter = function () {
  1649. var that = this,
  1650. data = that.getData(),
  1651. html = [];
  1652. if (!this.options.showFooter || this.options.cardView) { //do nothing
  1653. return;
  1654. }
  1655. if (!this.options.cardView && this.options.detailView) {
  1656. html.push('<td><div class="th-inner">&nbsp;</div><div class="fht-cell"></div></td>');
  1657. }
  1658. $.each(this.columns, function (i, column) {
  1659. var falign = '', // footer align style
  1660. style = '',
  1661. class_ = sprintf(' class="%s"', column['class']);
  1662. if (!column.visible) {
  1663. return;
  1664. }
  1665. if (that.options.cardView && (!column.cardVisible)) {
  1666. return;
  1667. }
  1668. falign = sprintf('text-align: %s; ', column.falign ? column.falign : column.align);
  1669. style = sprintf('vertical-align: %s; ', column.valign);
  1670. html.push('<td', class_, sprintf(' style="%s"', falign + style), '>');
  1671. html.push('<div class="th-inner">');
  1672. html.push(calculateObjectValue(column, column.footerFormatter, [data], '&nbsp;') || '&nbsp;');
  1673. html.push('</div>');
  1674. html.push('<div class="fht-cell"></div>');
  1675. html.push('</div>');
  1676. html.push('</td>');
  1677. });
  1678. this.$tableFooter.find('tr').html(html.join(''));
  1679. clearTimeout(this.timeoutFooter_);
  1680. this.timeoutFooter_ = setTimeout($.proxy(this.fitFooter, this),
  1681. this.$el.is(':hidden') ? 100 : 0);
  1682. };
  1683. BootstrapTable.prototype.fitFooter = function () {
  1684. var that = this,
  1685. $footerTd,
  1686. elWidth,
  1687. scrollWidth;
  1688. clearTimeout(this.timeoutFooter_);
  1689. if (this.$el.is(':hidden')) {
  1690. this.timeoutFooter_ = setTimeout($.proxy(this.fitFooter, this), 100);
  1691. return;
  1692. }
  1693. elWidth = this.$el.css('width');
  1694. scrollWidth = elWidth > this.$tableBody.width() ? getScrollBarWidth() : 0;
  1695. this.$tableFooter.css({
  1696. 'margin-right': scrollWidth
  1697. }).find('table').css('width', elWidth)
  1698. .attr('class', this.$el.attr('class'));
  1699. $footerTd = this.$tableFooter.find('td');
  1700. this.$body.find('>tr:first-child:not(.no-records-found) > *').each(function (i) {
  1701. var $this = $(this);
  1702. $footerTd.eq(i).find('.fht-cell').width($this.innerWidth());
  1703. });
  1704. };
  1705. BootstrapTable.prototype.toggleColumn = function (index, checked, needUpdate) {
  1706. if (index === -1) {
  1707. return;
  1708. }
  1709. this.columns[index].visible = checked;
  1710. this.initHeader();
  1711. this.initSearch();
  1712. this.initPagination();
  1713. this.initBody();
  1714. if (this.options.showColumns) {
  1715. var $items = this.$toolbar.find('.keep-open input').prop('disabled', false);
  1716. if (needUpdate) {
  1717. $items.filter(sprintf('[value="%s"]', index)).prop('checked', checked);
  1718. }
  1719. if ($items.filter(':checked').length <= this.options.minimumCountColumns) {
  1720. $items.filter(':checked').prop('disabled', true);
  1721. }
  1722. }
  1723. };
  1724. BootstrapTable.prototype.toggleRow = function (index, uniqueId, visible) {
  1725. if (index === -1) {
  1726. return;
  1727. }
  1728. this.$body.find(typeof index !== 'undefined' ?
  1729. sprintf('tr[data-index="%s"]', index) :
  1730. sprintf('tr[data-uniqueid="%s"]', uniqueId))
  1731. [visible ? 'show' : 'hide']();
  1732. };
  1733. BootstrapTable.prototype.getVisibleFields = function () {
  1734. var that = this,
  1735. visibleFields = [];
  1736. $.each(this.header.fields, function (j, field) {
  1737. var column = that.columns[getFieldIndex(that.columns, field)];
  1738. if (!column.visible) {
  1739. return;
  1740. }
  1741. visibleFields.push(field);
  1742. });
  1743. return visibleFields;
  1744. };
  1745. // PUBLIC FUNCTION DEFINITION
  1746. // =======================
  1747. BootstrapTable.prototype.resetView = function (params) {
  1748. var padding = 0;
  1749. if (params && params.height) {
  1750. this.options.height = params.height;
  1751. }
  1752. this.$selectAll.prop('checked', this.$selectItem.length > 0 &&
  1753. this.$selectItem.length === this.$selectItem.filter(':checked').length);
  1754. if (this.options.height) {
  1755. var toolbarHeight = getRealHeight(this.$toolbar),
  1756. paginationHeight = getRealHeight(this.$pagination),
  1757. height = this.options.height - toolbarHeight - paginationHeight;
  1758. this.$tableContainer.css('height', height + 'px');
  1759. }
  1760. if (this.options.cardView) {
  1761. // remove the element css
  1762. this.$el.css('margin-top', '0');
  1763. this.$tableContainer.css('padding-bottom', '0');
  1764. return;
  1765. }
  1766. if (this.options.showHeader && this.options.height) {
  1767. this.$tableHeader.show();
  1768. this.resetHeader();
  1769. padding += this.$header.outerHeight();
  1770. } else {
  1771. this.$tableHeader.hide();
  1772. this.trigger('post-header');
  1773. }
  1774. if (this.options.showFooter) {
  1775. this.resetFooter();
  1776. if (this.options.height) {
  1777. padding += this.$tableFooter.outerHeight() + 1;
  1778. }
  1779. }
  1780. // Assign the correct sortable arrow
  1781. this.getCaret();
  1782. this.$tableContainer.css('padding-bottom', padding + 'px');
  1783. this.trigger('reset-view');
  1784. };
  1785. BootstrapTable.prototype.getData = function (useCurrentPage) {
  1786. return (this.searchText || !$.isEmptyObject(this.filterColumns) || !$.isEmptyObject(this.filterColumnsPartial)) ?
  1787. (useCurrentPage ? this.data.slice(this.pageFrom - 1, this.pageTo) : this.data) :
  1788. (useCurrentPage ? this.options.data.slice(this.pageFrom - 1, this.pageTo) : this.options.data);
  1789. };
  1790. BootstrapTable.prototype.load = function (data) {
  1791. var fixedScroll = false;
  1792. // #431: support pagination
  1793. if (this.options.sidePagination === 'server') {
  1794. this.options.totalRows = data.total;
  1795. fixedScroll = data.fixedScroll;
  1796. data = data[this.options.dataField];
  1797. } else if (!$.isArray(data)) { // support fixedScroll
  1798. fixedScroll = data.fixedScroll;
  1799. data = data.data;
  1800. }
  1801. this.initData(data);
  1802. this.initSearch();
  1803. this.initPagination();
  1804. this.initBody(fixedScroll);
  1805. };
  1806. BootstrapTable.prototype.append = function (data) {
  1807. this.initData(data, 'append');
  1808. this.initSearch();
  1809. this.initPagination();
  1810. this.initBody(true);
  1811. };
  1812. BootstrapTable.prototype.prepend = function (data) {
  1813. this.initData(data, 'prepend');
  1814. this.initSearch();
  1815. this.initPagination();
  1816. this.initBody(true);
  1817. };
  1818. BootstrapTable.prototype.remove = function (params) {
  1819. var len = this.options.data.length,
  1820. i, row;
  1821. if (!params.hasOwnProperty('field') || !params.hasOwnProperty('values')) {
  1822. return;
  1823. }
  1824. for (i = len - 1; i >= 0; i--) {
  1825. row = this.options.data[i];
  1826. if (!row.hasOwnProperty(params.field)) {
  1827. continue;
  1828. }
  1829. if ($.inArray(row[params.field], params.values) !== -1) {
  1830. this.options.data.splice(i, 1);
  1831. }
  1832. }
  1833. if (len === this.options.data.length) {
  1834. return;
  1835. }
  1836. this.initSearch();
  1837. this.initPagination();
  1838. this.initBody(true);
  1839. };
  1840. BootstrapTable.prototype.removeAll = function () {
  1841. if (this.options.data.length > 0) {
  1842. this.options.data.splice(0, this.options.data.length);
  1843. this.initSearch();
  1844. this.initPagination();
  1845. this.initBody(true);
  1846. }
  1847. };
  1848. BootstrapTable.prototype.getRowByUniqueId = function (id) {
  1849. var uniqueId = this.options.uniqueId,
  1850. len = this.options.data.length,
  1851. dataRow = null,
  1852. i, row, rowUniqueId;
  1853. for (i = len - 1; i >= 0; i--) {
  1854. row = this.options.data[i];
  1855. if (row.hasOwnProperty(uniqueId)) { // uniqueId is a column
  1856. rowUniqueId = row[uniqueId];
  1857. } else if(row._data.hasOwnProperty(uniqueId)) { // uniqueId is a row data property
  1858. rowUniqueId = row._data[uniqueId];
  1859. } else {
  1860. continue;
  1861. }
  1862. if (typeof rowUniqueId === 'string') {
  1863. id = id.toString();
  1864. } else if (typeof rowUniqueId === 'number') {
  1865. if ((Number(rowUniqueId) === rowUniqueId) && (rowUniqueId % 1 === 0)) {
  1866. id = parseInt(id);
  1867. } else if ((rowUniqueId === Number(rowUniqueId)) && (rowUniqueId !== 0)) {
  1868. id = parseFloat(id);
  1869. }
  1870. }
  1871. if (rowUniqueId === id) {
  1872. dataRow = row;
  1873. break;
  1874. }
  1875. }
  1876. return dataRow;
  1877. };
  1878. BootstrapTable.prototype.removeByUniqueId = function (id) {
  1879. var len = this.options.data.length,
  1880. row = this.getRowByUniqueId(id);
  1881. if (row) {
  1882. this.options.data.splice(this.options.data.indexOf(row), 1);
  1883. }
  1884. if (len === this.options.data.length) {
  1885. return;
  1886. }
  1887. this.initSearch();
  1888. this.initPagination();
  1889. this.initBody(true);
  1890. };
  1891. BootstrapTable.prototype.insertRow = function (params) {
  1892. if (!params.hasOwnProperty('index') || !params.hasOwnProperty('row')) {
  1893. return;
  1894. }
  1895. this.data.splice(params.index, 0, params.row);
  1896. this.initSearch();
  1897. this.initPagination();
  1898. this.initSort();
  1899. this.initBody(true);
  1900. };
  1901. BootstrapTable.prototype.updateRow = function (params) {
  1902. if (!params.hasOwnProperty('index') || !params.hasOwnProperty('row')) {
  1903. return;
  1904. }
  1905. $.extend(this.data[params.index], params.row);
  1906. this.initSort();
  1907. this.initBody(true);
  1908. };
  1909. BootstrapTable.prototype.showRow = function (params) {
  1910. if (!params.hasOwnProperty('index') || !params.hasOwnProperty('uniqueId')) {
  1911. return;
  1912. }
  1913. this.toggleRow(params.index, params.uniqueId, true);
  1914. };
  1915. BootstrapTable.prototype.hideRow = function (params) {
  1916. if (!params.hasOwnProperty('index') || !params.hasOwnProperty('uniqueId')) {
  1917. return;
  1918. }
  1919. this.toggleRow(params.index, params.uniqueId, false);
  1920. };
  1921. BootstrapTable.prototype.getRowsHidden = function (show) {
  1922. var rows = $(this.$body[0]).children().filter(':hidden'),
  1923. i = 0;
  1924. if (show) {
  1925. for (; i < rows.length; i++) {
  1926. $(rows[i]).show();
  1927. }
  1928. }
  1929. return rows;
  1930. };
  1931. BootstrapTable.prototype.mergeCells = function (options) {
  1932. var row = options.index,
  1933. col = $.inArray(options.field, this.getVisibleFields()),
  1934. rowspan = options.rowspan || 1,
  1935. colspan = options.colspan || 1,
  1936. i, j,
  1937. $tr = this.$body.find('>tr'),
  1938. $td;
  1939. if (this.options.detailView && !this.options.cardView) {
  1940. col += 1;
  1941. }
  1942. $td = $tr.eq(row).find('>td').eq(col);
  1943. if (row < 0 || col < 0 || row >= this.data.length) {
  1944. return;
  1945. }
  1946. for (i = row; i < row + rowspan; i++) {
  1947. for (j = col; j < col + colspan; j++) {
  1948. $tr.eq(i).find('>td').eq(j).hide();
  1949. }
  1950. }
  1951. $td.attr('rowspan', rowspan).attr('colspan', colspan).show();
  1952. };
  1953. BootstrapTable.prototype.updateCell = function (params) {
  1954. if (!params.hasOwnProperty('index') ||
  1955. !params.hasOwnProperty('field') ||
  1956. !params.hasOwnProperty('value')) {
  1957. return;
  1958. }
  1959. this.data[params.index][params.field] = params.value;
  1960. this.initSort();
  1961. this.initBody(true);
  1962. };
  1963. BootstrapTable.prototype.getOptions = function () {
  1964. return this.options;
  1965. };
  1966. BootstrapTable.prototype.getSelections = function () {
  1967. var that = this;
  1968. return $.grep(this.data, function (row) {
  1969. return row[that.header.stateField];
  1970. });
  1971. };
  1972. BootstrapTable.prototype.getAllSelections = function () {
  1973. var that = this;
  1974. return $.grep(this.options.data, function (row) {
  1975. return row[that.header.stateField];
  1976. });
  1977. };
  1978. BootstrapTable.prototype.checkAll = function () {
  1979. this.checkAll_(true);
  1980. };
  1981. BootstrapTable.prototype.uncheckAll = function () {
  1982. this.checkAll_(false);
  1983. };
  1984. BootstrapTable.prototype.checkAll_ = function (checked) {
  1985. var rows;
  1986. if (!checked) {
  1987. rows = this.getSelections();
  1988. }
  1989. this.$selectAll.add(this.$selectAll_).prop('checked', checked);
  1990. this.$selectItem.filter(':enabled').prop('checked', checked);
  1991. this.updateRows();
  1992. if (checked) {
  1993. rows = this.getSelections();
  1994. }
  1995. this.trigger(checked ? 'check-all' : 'uncheck-all', rows);
  1996. };
  1997. BootstrapTable.prototype.check = function (index) {
  1998. this.check_(true, index);
  1999. };
  2000. BootstrapTable.prototype.uncheck = function (index) {
  2001. this.check_(false, index);
  2002. };
  2003. BootstrapTable.prototype.check_ = function (checked, index) {
  2004. var $el = this.$selectItem.filter(sprintf('[data-index="%s"]', index)).prop('checked', checked);
  2005. this.data[index][this.header.stateField] = checked;
  2006. this.updateSelected();
  2007. this.trigger(checked ? 'check' : 'uncheck', this.data[index], $el);
  2008. };
  2009. BootstrapTable.prototype.checkBy = function (obj) {
  2010. this.checkBy_(true, obj);
  2011. };
  2012. BootstrapTable.prototype.uncheckBy = function (obj) {
  2013. this.checkBy_(false, obj);
  2014. };
  2015. BootstrapTable.prototype.checkBy_ = function (checked, obj) {
  2016. if (!obj.hasOwnProperty('field') || !obj.hasOwnProperty('values')) {
  2017. return;
  2018. }
  2019. var that = this,
  2020. rows = [];
  2021. $.each(this.options.data, function (index, row) {
  2022. if (!row.hasOwnProperty(obj.field)) {
  2023. return false;
  2024. }
  2025. if ($.inArray(row[obj.field], obj.values) !== -1) {
  2026. var $el = that.$selectItem.filter(':enabled')
  2027. .filter(sprintf('[data-index="%s"]', index)).prop('checked', checked);
  2028. row[that.header.stateField] = checked;
  2029. rows.push(row);
  2030. that.trigger(checked ? 'check' : 'uncheck', row, $el);
  2031. }
  2032. });
  2033. this.updateSelected();
  2034. this.trigger(checked ? 'check-some' : 'uncheck-some', rows);
  2035. };
  2036. BootstrapTable.prototype.destroy = function () {
  2037. this.$el.insertBefore(this.$container);
  2038. $(this.options.toolbar).insertBefore(this.$el);
  2039. this.$container.next().remove();
  2040. this.$container.remove();
  2041. this.$el.html(this.$el_.html())
  2042. .css('margin-top', '0')
  2043. .attr('class', this.$el_.attr('class') || ''); // reset the class
  2044. };
  2045. BootstrapTable.prototype.showLoading = function () {
  2046. this.$tableLoading.show();
  2047. };
  2048. BootstrapTable.prototype.hideLoading = function () {
  2049. this.$tableLoading.hide();
  2050. };
  2051. BootstrapTable.prototype.togglePagination = function () {
  2052. this.options.pagination = !this.options.pagination;
  2053. var button = this.$toolbar.find('button[name="paginationSwitch"] i');
  2054. if (this.options.pagination) {
  2055. button.attr("class", this.options.iconsPrefix + " " + this.options.icons.paginationSwitchDown);
  2056. } else {
  2057. button.attr("class", this.options.iconsPrefix + " " + this.options.icons.paginationSwitchUp);
  2058. }
  2059. this.updatePagination();
  2060. };
  2061. BootstrapTable.prototype.refresh = function (params) {
  2062. if (params && params.url) {
  2063. this.options.url = params.url;
  2064. this.options.pageNumber = 1;
  2065. }
  2066. this.initServer(params && params.silent, params && params.query);
  2067. };
  2068. BootstrapTable.prototype.resetWidth = function () {
  2069. if (this.options.showHeader && this.options.height) {
  2070. this.fitHeader();
  2071. }
  2072. if (this.options.showFooter) {
  2073. this.fitFooter();
  2074. }
  2075. };
  2076. BootstrapTable.prototype.showColumn = function (field) {
  2077. this.toggleColumn(getFieldIndex(this.columns, field), true, true);
  2078. };
  2079. BootstrapTable.prototype.hideColumn = function (field) {
  2080. this.toggleColumn(getFieldIndex(this.columns, field), false, true);
  2081. };
  2082. BootstrapTable.prototype.getHiddenColumns = function () {
  2083. return $.grep(this.columns, function (column) {
  2084. return !column.visible;
  2085. });
  2086. };
  2087. BootstrapTable.prototype.filterBy = function (columns) {
  2088. this.filterColumns = $.isEmptyObject(columns) ? {} : columns;
  2089. this.options.pageNumber = 1;
  2090. this.initSearch();
  2091. this.updatePagination();
  2092. };
  2093. BootstrapTable.prototype.scrollTo = function (value) {
  2094. if (typeof value === 'string') {
  2095. value = value === 'bottom' ? this.$tableBody[0].scrollHeight : 0;
  2096. }
  2097. if (typeof value === 'number') {
  2098. this.$tableBody.scrollTop(value);
  2099. }
  2100. if (typeof value === 'undefined') {
  2101. return this.$tableBody.scrollTop();
  2102. }
  2103. };
  2104. BootstrapTable.prototype.getScrollPosition = function () {
  2105. return this.scrollTo();
  2106. };
  2107. BootstrapTable.prototype.selectPage = function (page) {
  2108. if (page > 0 && page <= this.options.totalPages) {
  2109. this.options.pageNumber = page;
  2110. this.updatePagination();
  2111. }
  2112. };
  2113. BootstrapTable.prototype.prevPage = function () {
  2114. if (this.options.pageNumber > 1) {
  2115. this.options.pageNumber--;
  2116. this.updatePagination();
  2117. }
  2118. };
  2119. BootstrapTable.prototype.nextPage = function () {
  2120. if (this.options.pageNumber < this.options.totalPages) {
  2121. this.options.pageNumber++;
  2122. this.updatePagination();
  2123. }
  2124. };
  2125. BootstrapTable.prototype.toggleView = function () {
  2126. this.options.cardView = !this.options.cardView;
  2127. this.initHeader();
  2128. // Fixed remove toolbar when click cardView button.
  2129. //that.initToolbar();
  2130. this.initBody();
  2131. this.trigger('toggle', this.options.cardView);
  2132. };
  2133. BootstrapTable.prototype.refreshOptions = function (options) {
  2134. //If the objects are equivalent then avoid the call of destroy / init methods
  2135. if (compareObjects(this.options, options, false)) {
  2136. return;
  2137. }
  2138. this.options = $.extend(this.options, options);
  2139. this.trigger('refresh-options', this.options);
  2140. this.destroy();
  2141. this.init();
  2142. };
  2143. BootstrapTable.prototype.resetSearch = function (text) {
  2144. var $search = this.$toolbar.find('.search input');
  2145. $search.val(text || '');
  2146. this.onSearch({currentTarget: $search});
  2147. };
  2148. BootstrapTable.prototype.expandRow_ = function (expand, index) {
  2149. var $tr = this.$body.find(sprintf('> tr[data-index="%s"]', index));
  2150. if ($tr.next().is('tr.detail-view') === (expand ? false : true)) {
  2151. $tr.find('> td > .detail-icon').click();
  2152. }
  2153. };
  2154. BootstrapTable.prototype.expandRow = function (index) {
  2155. this.expandRow_(true, index);
  2156. };
  2157. BootstrapTable.prototype.collapseRow = function (index) {
  2158. this.expandRow_(false, index);
  2159. };
  2160. BootstrapTable.prototype.expandAllRows = function (isSubTable) {
  2161. if (isSubTable) {
  2162. var $tr = this.$body.find(sprintf('> tr[data-index="%s"]', 0)),
  2163. that = this,
  2164. detailIcon = null,
  2165. executeInterval = false,
  2166. idInterval = -1;
  2167. if (!$tr.next().is('tr.detail-view')) {
  2168. $tr.find('> td > .detail-icon').click();
  2169. executeInterval = true;
  2170. } else if (!$tr.next().next().is('tr.detail-view')) {
  2171. $tr.next().find(".detail-icon").click();
  2172. executeInterval = true;
  2173. }
  2174. if (executeInterval) {
  2175. try {
  2176. idInterval = setInterval(function () {
  2177. detailIcon = that.$body.find("tr.detail-view").last().find(".detail-icon");
  2178. if (detailIcon.length > 0) {
  2179. detailIcon.click();
  2180. } else {
  2181. clearInterval(idInterval);
  2182. }
  2183. }, 1);
  2184. } catch (ex) {
  2185. clearInterval(idInterval);
  2186. }
  2187. }
  2188. } else {
  2189. var trs = this.$body.children();
  2190. for (var i = 0; i < trs.length; i++) {
  2191. this.expandRow_(true, $(trs[i]).data("index"));
  2192. }
  2193. }
  2194. };
  2195. BootstrapTable.prototype.collapseAllRows = function (isSubTable) {
  2196. if (isSubTable) {
  2197. this.expandRow_(false, 0);
  2198. } else {
  2199. var trs = this.$body.children();
  2200. for (var i = 0; i < trs.length; i++) {
  2201. this.expandRow_(false, $(trs[i]).data("index"));
  2202. }
  2203. }
  2204. };
  2205. // BOOTSTRAP TABLE PLUGIN DEFINITION
  2206. // =======================
  2207. var allowedMethods = [
  2208. 'getOptions',
  2209. 'getSelections', 'getAllSelections', 'getData',
  2210. 'load', 'append', 'prepend', 'remove', 'removeAll',
  2211. 'insertRow', 'updateRow', 'updateCell', 'removeByUniqueId',
  2212. 'getRowByUniqueId', 'showRow', 'hideRow', 'getRowsHidden',
  2213. 'mergeCells',
  2214. 'checkAll', 'uncheckAll',
  2215. 'check', 'uncheck',
  2216. 'checkBy', 'uncheckBy',
  2217. 'refresh',
  2218. 'resetView',
  2219. 'resetWidth',
  2220. 'destroy',
  2221. 'showLoading', 'hideLoading',
  2222. 'showColumn', 'hideColumn', 'getHiddenColumns',
  2223. 'filterBy',
  2224. 'scrollTo',
  2225. 'getScrollPosition',
  2226. 'selectPage', 'prevPage', 'nextPage',
  2227. 'togglePagination',
  2228. 'toggleView',
  2229. 'refreshOptions',
  2230. 'resetSearch',
  2231. 'expandRow', 'collapseRow', 'expandAllRows', 'collapseAllRows'
  2232. ];
  2233. $.fn.bootstrapTable = function (option) {
  2234. var value,
  2235. args = Array.prototype.slice.call(arguments, 1);
  2236. this.each(function () {
  2237. var $this = $(this),
  2238. data = $this.data('bootstrap.table'),
  2239. options = $.extend({}, BootstrapTable.DEFAULTS, $this.data(),
  2240. typeof option === 'object' && option);
  2241. if (typeof option === 'string') {
  2242. if ($.inArray(option, allowedMethods) < 0) {
  2243. throw new Error("Unknown method: " + option);
  2244. }
  2245. if (!data) {
  2246. return;
  2247. }
  2248. value = data[option].apply(data, args);
  2249. if (option === 'destroy') {
  2250. $this.removeData('bootstrap.table');
  2251. }
  2252. }
  2253. if (!data) {
  2254. $this.data('bootstrap.table', (data = new BootstrapTable(this, options)));
  2255. }
  2256. });
  2257. return typeof value === 'undefined' ? this : value;
  2258. };
  2259. $.fn.bootstrapTable.Constructor = BootstrapTable;
  2260. $.fn.bootstrapTable.defaults = BootstrapTable.DEFAULTS;
  2261. $.fn.bootstrapTable.columnDefaults = BootstrapTable.COLUMN_DEFAULTS;
  2262. $.fn.bootstrapTable.locales = BootstrapTable.LOCALES;
  2263. $.fn.bootstrapTable.methods = allowedMethods;
  2264. $.fn.bootstrapTable.utils = {
  2265. sprintf: sprintf,
  2266. getFieldIndex: getFieldIndex,
  2267. compareObjects: compareObjects,
  2268. calculateObjectValue: calculateObjectValue
  2269. };
  2270. // BOOTSTRAP TABLE INIT
  2271. // =======================
  2272. $(function () {
  2273. $('[data-toggle="table"]').bootstrapTable();
  2274. });
  2275. }(jQuery);