bootstrap-table.js 105 KB

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