bootstrap-table.js 105 KB

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