bootstrap-table.js 102 KB

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