bootstrap-table.js 109 KB

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