bootstrap-table.js 110 KB

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