bootstrap-table.js 97 KB

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