bootstrap-table.js 106 KB

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