bootstrap-table.js 111 KB

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