bootstrap-table.js 89 KB

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