bootstrap-table.js 106 KB

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