bootstrap-table.js 107 KB

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