bootstrap-table.js 108 KB

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