bootstrap-table.js 97 KB

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