Geen omschrijving

bootstrap-table.js 107KB

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