bootstrap-table.js 104 KB

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