bootstrap-table.js 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124
  1. /**
  2. * @author zhixin wen <wenzhixin2010@gmail.com>
  3. * version: 1.6.0
  4. * https://github.com/wenzhixin/bootstrap-table/
  5. */
  6. !function ($) {
  7. 'use strict';
  8. var cellHeight = 37, // update css if changed
  9. idStateSave = '',
  10. idSortOrderStateSave = 'bs.table.sortOrder',
  11. idSortNameStateSave = 'bs.table.sortName',
  12. idPageNumberStateSave = 'bs.table.pageNumber',
  13. idPageListStateSave = 'bs.table.pageList',
  14. idsStateSaveList = {
  15. 'sortOrder': idSortOrderStateSave,
  16. 'sortName': idSortNameStateSave,
  17. 'pageNumber': idPageNumberStateSave,
  18. 'pageList': idPageListStateSave
  19. };
  20. // TOOLS DEFINITION
  21. // ======================
  22. // it only does '%s', and return '' when arguments are undefined
  23. var sprintf = function (str) {
  24. var args = arguments,
  25. flag = true,
  26. i = 1;
  27. str = str.replace(/%s/g, function () {
  28. var arg = args[i++];
  29. if (typeof arg === 'undefined') {
  30. flag = false;
  31. return '';
  32. }
  33. return arg;
  34. });
  35. return flag ? str : '';
  36. };
  37. var getPropertyFromOther = function (list, from, to, value) {
  38. var result = '';
  39. $.each(list, function (i, item) {
  40. if (item[from] === value) {
  41. result = item[to];
  42. return false;
  43. }
  44. return true;
  45. });
  46. return result;
  47. };
  48. var getFieldIndex = function (columns, field) {
  49. var index = -1;
  50. $.each(columns, function (i, column) {
  51. if (column.field === field) {
  52. index = i;
  53. return false;
  54. }
  55. return true;
  56. });
  57. return index;
  58. };
  59. var cachedWidth = null;
  60. var getScrollBarWidth = function () {
  61. if (cachedWidth === null) {
  62. var inner = $('<p/>').addClass('fixed-table-scroll-inner'),
  63. outer = $('<div/>').addClass('fixed-table-scroll-outer'),
  64. w1, w2;
  65. outer.append(inner);
  66. $('body').append(outer);
  67. w1 = inner[0].offsetWidth;
  68. outer.css('overflow', 'scroll');
  69. w2 = inner[0].offsetWidth;
  70. if (w1 === w2) {
  71. w2 = outer[0].clientWidth;
  72. }
  73. outer.remove();
  74. cachedWidth = w1 - w2;
  75. }
  76. return cachedWidth;
  77. };
  78. var calculateObjectValue = function (self, name, args, defaultValue) {
  79. if (typeof name === 'string') {
  80. // support obj.func1.func2
  81. var names = name.split('.');
  82. if (names.length > 1) {
  83. name = window;
  84. $.each(names, function (i, f) {
  85. name = name[f];
  86. });
  87. } else {
  88. name = window[name];
  89. }
  90. }
  91. if (typeof name === 'object') {
  92. return name;
  93. }
  94. if (typeof name === 'function') {
  95. return name.apply(self, args);
  96. }
  97. return defaultValue;
  98. };
  99. var escapeHTML = function (text) {
  100. if (typeof text === 'string') {
  101. return text
  102. .replace(/&/g, "&amp;")
  103. .replace(/</g, "&lt;")
  104. .replace(/>/g, "&gt;")
  105. .replace(/"/g, "&quot;")
  106. .replace(/'/g, "&#039;");
  107. }
  108. return text;
  109. };
  110. var cookieEnabled = function (){
  111. var cookieEnabled = (navigator.cookieEnabled) ? true : false;
  112. if (typeof navigator.cookieEnabled === "undefined" && !cookieEnabled)
  113. {
  114. document.cookie = "testcookie";
  115. cookieEnabled = (document.cookie.indexOf("testcookie") !== -1) ? true : false;
  116. }
  117. return (cookieEnabled);
  118. };
  119. var setCookie = function (cookieName, sValue, vEnd, sPath, sDomain, bSecure) {
  120. cookieName = idStateSave + cookieName;
  121. if (!cookieName || /^(?:expires|max\-age|path|domain|secure)$/i.test(cookieName)) {
  122. return false;
  123. }
  124. var sExpires = "",
  125. time = '';
  126. time = vEnd.replace(/[0-9]/,''); //s,mi,h,d,m,y
  127. vEnd = vEnd.replace(/[A-Za-z]/,''); //number
  128. switch (time.toLowerCase()) {
  129. case 's':
  130. vEnd = +vEnd;
  131. break;
  132. case 'mi':
  133. vEnd = vEnd * 60;
  134. break;
  135. case 'h':
  136. vEnd = vEnd * 60 * 60;
  137. case 'd':
  138. vEnd = vEnd * 24 * 60 * 60;
  139. break;
  140. case 'm':
  141. vEnd = vEnd * 30 * 24 * 60 * 60;
  142. break;
  143. case 'y':
  144. vEnd = vEnd * 365 * 30 * 24 * 60 * 60;
  145. break;
  146. }
  147. sExpires = "; max-age=" + vEnd;
  148. document.cookie = encodeURIComponent(cookieName) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
  149. return true;
  150. };
  151. var getCookie = function (cookieName) {
  152. cookieName = idStateSave + cookieName;
  153. if (!cookieName) {
  154. return null;
  155. }
  156. return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(cookieName).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
  157. };
  158. var hasCookie = function (cookieName) {
  159. if (!cookieName) {
  160. return false;
  161. }
  162. return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(cookieName).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
  163. };
  164. var deleteCookie = function (cookieName, sPath, sDomain) {
  165. cookieName = idStateSave + cookieName;
  166. if (!hasCookie(cookieName)) {
  167. return false;
  168. }
  169. document.cookie = encodeURIComponent(cookieName) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
  170. return true;
  171. };
  172. // BOOTSTRAP TABLE CLASS DEFINITION
  173. // ======================
  174. var BootstrapTable = function (el, options) {
  175. this.options = options;
  176. this.$el = $(el);
  177. this.$el_ = this.$el.clone();
  178. this.timeoutId_ = 0;
  179. this.timeoutFooter_ = 0;
  180. this.init();
  181. };
  182. BootstrapTable.DEFAULTS = {
  183. classes: 'table table-hover',
  184. height: undefined,
  185. undefinedText: '-',
  186. sortName: undefined,
  187. sortOrder: 'asc',
  188. striped: false,
  189. columns: [],
  190. data: [],
  191. method: 'get',
  192. url: undefined,
  193. cache: true,
  194. contentType: 'application/json',
  195. dataType: 'json',
  196. ajaxOptions: {},
  197. queryParams: function (params) {
  198. return params;
  199. },
  200. queryParamsType: 'limit', // undefined
  201. responseHandler: function (res) {
  202. return res;
  203. },
  204. pagination: false,
  205. sidePagination: 'client', // client or server
  206. totalRows: 0, // server side need to set
  207. pageNumber: 1,
  208. pageSize: 10,
  209. pageList: [10, 25, 50, 100],
  210. paginationHAlign: 'right', //right, left
  211. paginationVAlign: 'bottom', //bottom, top
  212. paginationDetailHAlign: 'left', //right, left
  213. paginationDetailVAlign: 'bottom', //bottom, top
  214. search: false,
  215. searchAlign: 'right',
  216. selectItemName: 'btSelectItem',
  217. showHeader: true,
  218. showFooter: false,
  219. showColumns: false,
  220. showPaginationSwitch: false,
  221. showRefresh: false,
  222. showToggle: false,
  223. buttonsAlign: 'right',
  224. smartDisplay: true,
  225. minimumCountColumns: 1,
  226. idField: undefined,
  227. cardView: false,
  228. trimOnSearch: true,
  229. clickToSelect: false,
  230. singleSelect: false,
  231. toolbar: undefined,
  232. toolbarAlign: 'left',
  233. checkboxHeader: true,
  234. sortable: true,
  235. maintainSelected: false,
  236. searchTimeOut: 500,
  237. keyEvents: false,
  238. searchText: '',
  239. stateSave: false,
  240. stateSaveExpire: '2h',
  241. stateSaveIdTable: '',
  242. iconSize: undefined,
  243. iconsPrefix: 'glyphicon', // glyphicon of fa (font awesome)
  244. icons: {
  245. paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
  246. paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
  247. refresh: 'glyphicon-refresh icon-refresh',
  248. toggle: 'glyphicon-list-alt icon-list-alt',
  249. columns: 'glyphicon-th icon-th'
  250. },
  251. rowStyle: function (row, index) {
  252. return {};
  253. },
  254. rowAttributes: function (row, index) {
  255. return {};
  256. },
  257. onAll: function (name, args) {
  258. return false;
  259. },
  260. onClickRow: function (item, $element) {
  261. return false;
  262. },
  263. onDblClickRow: function (item, $element) {
  264. return false;
  265. },
  266. onSort: function (name, order) {
  267. return false;
  268. },
  269. onCheck: function (row) {
  270. return false;
  271. },
  272. onUncheck: function (row) {
  273. return false;
  274. },
  275. onCheckAll: function () {
  276. return false;
  277. },
  278. onUncheckAll: function () {
  279. return false;
  280. },
  281. onLoadSuccess: function (data) {
  282. return false;
  283. },
  284. onLoadError: function (status) {
  285. return false;
  286. },
  287. onColumnSwitch: function (field, checked) {
  288. return false;
  289. },
  290. onPageChange: function (number, size) {
  291. return false;
  292. },
  293. onSearch: function (text) {
  294. return false;
  295. },
  296. onPreBody: function (data) {
  297. return false;
  298. },
  299. onPostBody: function () {
  300. return false;
  301. },
  302. onPostHeader: function () {
  303. return false;
  304. }
  305. };
  306. BootstrapTable.LOCALES = [];
  307. BootstrapTable.LOCALES['en-US'] = {
  308. formatLoadingMessage: function () {
  309. return 'Loading, please wait...';
  310. },
  311. formatRecordsPerPage: function (pageNumber) {
  312. return sprintf('%s records per page', pageNumber);
  313. },
  314. formatShowingRows: function (pageFrom, pageTo, totalRows) {
  315. return sprintf('Showing %s to %s of %s rows', pageFrom, pageTo, totalRows);
  316. },
  317. formatSearch: function () {
  318. return 'Search';
  319. },
  320. formatNoMatches: function () {
  321. return 'No matching records found';
  322. },
  323. formatPaginationSwitch: function () {
  324. return 'Hide/Show pagination';
  325. },
  326. formatRefresh: function () {
  327. return 'Refresh';
  328. },
  329. formatToggle: function () {
  330. return 'Toggle';
  331. },
  332. formatColumns: function () {
  333. return 'Columns';
  334. },
  335. formatAllRows: function () {
  336. return 'All';
  337. }
  338. };
  339. $.extend(BootstrapTable.DEFAULTS, BootstrapTable.LOCALES['en-US']);
  340. BootstrapTable.COLUMN_DEFAULTS = {
  341. radio: false,
  342. checkbox: false,
  343. checkboxEnabled: true,
  344. field: undefined,
  345. title: undefined,
  346. 'class': undefined,
  347. align: undefined, // left, right, center
  348. halign: undefined, // left, right, center
  349. falign: undefined, // left, right, center
  350. valign: undefined, // top, middle, bottom
  351. width: undefined,
  352. sortable: false,
  353. order: 'asc', // asc, desc
  354. visible: true,
  355. switchable: true,
  356. clickToSelect: true,
  357. formatter: undefined,
  358. footerFormatter: undefined,
  359. events: undefined,
  360. sorter: undefined,
  361. cellStyle: undefined,
  362. searchable: true,
  363. cardVisible: true
  364. };
  365. BootstrapTable.EVENTS = {
  366. 'all.bs.table': 'onAll',
  367. 'click-row.bs.table': 'onClickRow',
  368. 'dbl-click-row.bs.table': 'onDblClickRow',
  369. 'sort.bs.table': 'onSort',
  370. 'check.bs.table': 'onCheck',
  371. 'uncheck.bs.table': 'onUncheck',
  372. 'check-all.bs.table': 'onCheckAll',
  373. 'uncheck-all.bs.table': 'onUncheckAll',
  374. 'load-success.bs.table': 'onLoadSuccess',
  375. 'load-error.bs.table': 'onLoadError',
  376. 'column-switch.bs.table': 'onColumnSwitch',
  377. 'page-change.bs.table': 'onPageChange',
  378. 'search.bs.table': 'onSearch',
  379. 'pre-body.bs.table': 'onPreBody',
  380. 'post-body.bs.table': 'onPostBody',
  381. 'post-header.bs.table': 'onPostHeader'
  382. };
  383. BootstrapTable.prototype.init = function () {
  384. this.initStateSave();
  385. this.initContainer();
  386. this.initTable();
  387. this.initHeader();
  388. this.initData();
  389. this.initFooter();
  390. this.initToolbar();
  391. this.initPagination();
  392. this.initBody();
  393. this.initServer();
  394. this.initKeyEvents();
  395. };
  396. BootstrapTable.prototype.initContainer = function () {
  397. this.$container = $([
  398. '<div class="bootstrap-table">',
  399. '<div class="fixed-table-toolbar"></div>',
  400. '<div class="fixed-table-container">',
  401. '<div class="fixed-table-header"><table></table></div>',
  402. '<div class="fixed-table-body">',
  403. '<div class="fixed-table-loading">',
  404. this.options.formatLoadingMessage(),
  405. '</div>',
  406. '</div>',
  407. '<div class="fixed-table-footer"><table><tr></tr></table></div>',
  408. '<div class="fixed-table-pagination"></div>',
  409. '</div>',
  410. '</div>'].join(''));
  411. this.$container.insertAfter(this.$el);
  412. this.$container.find('.fixed-table-body').append(this.$el);
  413. this.$container.after('<div class="clearfix"></div>');
  414. this.$loading = this.$container.find('.fixed-table-loading');
  415. this.$el.addClass(this.options.classes);
  416. if (this.options.striped) {
  417. this.$el.addClass('table-striped');
  418. }
  419. };
  420. BootstrapTable.prototype.initTable = function () {
  421. var that = this,
  422. columns = [],
  423. data = [];
  424. this.$header = this.$el.find('thead');
  425. if (!this.$header.length) {
  426. this.$header = $('<thead></thead>').appendTo(this.$el);
  427. }
  428. if (!this.$header.find('tr').length) {
  429. this.$header.append('<tr></tr>');
  430. }
  431. this.$header.find('th').each(function () {
  432. var column = $.extend({}, {
  433. title: $(this).html(),
  434. 'class': $(this).attr('class')
  435. }, $(this).data());
  436. columns.push(column);
  437. });
  438. this.options.columns = $.extend([], columns, this.options.columns);
  439. $.each(this.options.columns, function (i, column) {
  440. that.options.columns[i] = $.extend({}, BootstrapTable.COLUMN_DEFAULTS,
  441. {field: i}, column); // when field is undefined, use index instead
  442. });
  443. // if options.data is setting, do not process tbody data
  444. if (this.options.data.length) {
  445. return;
  446. }
  447. this.$el.find('tbody tr').each(function () {
  448. var row = {};
  449. // save tr's id and class
  450. row._id = $(this).attr('id');
  451. row._class = $(this).attr('class');
  452. $(this).find('td').each(function (i) {
  453. var field = that.options.columns[i].field;
  454. row[field] = $(this).html();
  455. // save td's id and class
  456. row['_' + field + '_id'] = $(this).attr('id');
  457. row['_' + field + '_class'] = $(this).attr('class');
  458. row['_' + field + '_data'] = $(this).data();
  459. });
  460. data.push(row);
  461. });
  462. this.options.data = data;
  463. };
  464. BootstrapTable.prototype.initHeader = function () {
  465. var that = this,
  466. visibleColumns = [],
  467. html = [];
  468. this.header = {
  469. fields: [],
  470. styles: [],
  471. classes: [],
  472. formatters: [],
  473. events: [],
  474. sorters: [],
  475. cellStyles: [],
  476. clickToSelects: [],
  477. searchables: []
  478. };
  479. $.each(this.options.columns, function (i, column) {
  480. var text = '',
  481. halign = '', // header align style
  482. align = '', // body align style
  483. style = '',
  484. class_ = sprintf(' class="%s"', column['class']),
  485. order = that.options.sortOrder || column.order,
  486. searchable = true;
  487. if (!column.visible) {
  488. return;
  489. }
  490. if (that.options.cardView && (!column.cardVisible)) {
  491. return;
  492. }
  493. halign = sprintf('text-align: %s; ', column.halign ? column.halign : column.align);
  494. align = sprintf('text-align: %s; ', column.align);
  495. style = sprintf('vertical-align: %s; ', column.valign);
  496. style += sprintf('width: %spx; ', column.checkbox || column.radio ? 36 : column.width);
  497. visibleColumns.push(column);
  498. that.header.fields.push(column.field);
  499. that.header.styles.push(align + style);
  500. that.header.classes.push(class_);
  501. that.header.formatters.push(column.formatter);
  502. that.header.events.push(column.events);
  503. that.header.sorters.push(column.sorter);
  504. that.header.cellStyles.push(column.cellStyle);
  505. that.header.clickToSelects.push(column.clickToSelect);
  506. that.header.searchables.push(column.searchable);
  507. html.push('<th',
  508. column.checkbox || column.radio ?
  509. sprintf(' class="bs-checkbox %s"', column['class'] || '') :
  510. class_,
  511. sprintf(' style="%s"', halign + style),
  512. '>');
  513. html.push(sprintf('<div class="th-inner %s">', that.options.sortable && column.sortable ?
  514. 'sortable' : ''));
  515. text = column.title;
  516. if (that.options.sortName === column.field && that.options.sortable && column.sortable) {
  517. text += that.getCaretHtml();
  518. }
  519. if (column.checkbox) {
  520. if (!that.options.singleSelect && that.options.checkboxHeader) {
  521. text = '<input name="btSelectAll" type="checkbox" />';
  522. }
  523. that.header.stateField = column.field;
  524. }
  525. if (column.radio) {
  526. text = '';
  527. that.header.stateField = column.field;
  528. that.options.singleSelect = true;
  529. }
  530. html.push(text);
  531. html.push('</div>');
  532. html.push('<div class="fht-cell"></div>');
  533. html.push('</th>');
  534. });
  535. this.$header.find('tr').html(html.join(''));
  536. this.$header.find('th').each(function (i) {
  537. $(this).data(visibleColumns[i]);
  538. });
  539. this.$container.off('click', 'th').on('click', 'th', function (event) {
  540. if (that.options.sortable && $(this).data().sortable) {
  541. that.onSort(event);
  542. }
  543. });
  544. if (!this.options.showHeader || this.options.cardView) {
  545. this.$header.hide();
  546. this.$container.find('.fixed-table-header').hide();
  547. this.$loading.css('top', 0);
  548. } else {
  549. this.$header.show();
  550. this.$container.find('.fixed-table-header').show();
  551. this.$loading.css('top', cellHeight + 'px');
  552. }
  553. this.$selectAll = this.$header.find('[name="btSelectAll"]');
  554. this.$container.off('click', '[name="btSelectAll"]')
  555. .on('click', '[name="btSelectAll"]', function () {
  556. var checked = $(this).prop('checked');
  557. that[checked ? 'checkAll' : 'uncheckAll']();
  558. });
  559. };
  560. BootstrapTable.prototype.initFooter = function () {
  561. this.$footer = this.$container.find('.fixed-table-footer');
  562. if (!this.options.showFooter || this.options.cardView) {
  563. this.$footer.hide();
  564. } else {
  565. this.$footer.show();
  566. }
  567. };
  568. BootstrapTable.prototype.resetFooter = function () {
  569. var bt = this,
  570. data = bt.getData(),
  571. html = [];
  572. if (!this.options.showFooter || this.options.cardView) { //do nothing
  573. return;
  574. }
  575. $.each(bt.options.columns, function (i, column) {
  576. var falign = '', // footer align style
  577. style = '',
  578. class_ = sprintf(' class="%s"', column['class']);
  579. if (!column.visible) {
  580. return;
  581. }
  582. if (that.options.cardView && (!column.cardVisible)) {
  583. return;
  584. }
  585. falign = sprintf('text-align: %s; ', column.falign ? column.falign : column.align);
  586. style = sprintf('vertical-align: %s; ', column.valign);
  587. html.push('<td', class_, sprintf(' style="%s"', falign + style), '>');
  588. html.push(calculateObjectValue(column, column.footerFormatter, [data], '&nbsp;') || '&nbsp;');
  589. html.push('</td>');
  590. });
  591. bt.$footer.find('tr').html(html.join(''));
  592. clearTimeout(bt.timeoutFooter_);
  593. bt.timeoutFooter_ = setTimeout($.proxy(bt.fitFooter, bt), bt.$el.is(':hidden') ? 100: 0);
  594. return;
  595. };
  596. BootstrapTable.prototype.fitFooter = function () {
  597. var bt = this,
  598. $fixedBody,
  599. $footerTd,
  600. elWidth,
  601. scrollWidth;
  602. clearTimeout(bt.timeoutFooter_);
  603. if (bt.$el.is(':hidden')) {
  604. bt.timeoutFooter_ = setTimeout($.proxy(bt.fitFooter, bt), 100);
  605. return;
  606. }
  607. $fixedBody = bt.$container.find('.fixed-table-body');
  608. elWidth = bt.$el.css('width');
  609. scrollWidth = elWidth > $fixedBody.width() ? getScrollBarWidth() : 0;
  610. bt.$footer.css({
  611. 'margin-right': scrollWidth
  612. }).find('table').css('width', elWidth)
  613. .attr('class', bt.$el.attr('class'));
  614. $footerTd = bt.$footer.find('td');
  615. $fixedBody.find('tbody tr:first-child:not(.no-records-found) > td').each(function(i) {
  616. $footerTd.eq(i).outerWidth($(this).outerWidth());
  617. });
  618. };
  619. /**
  620. * @param data
  621. * @param type: append / prepend
  622. */
  623. BootstrapTable.prototype.initData = function (data, type) {
  624. if (type === 'append') {
  625. this.data = this.data.concat(data);
  626. } else if (type === 'prepend') {
  627. this.data = [].concat(data).concat(this.data);
  628. } else {
  629. this.data = data || this.options.data;
  630. }
  631. this.options.data = this.data;
  632. if (this.options.sidePagination === 'server') {
  633. return;
  634. }
  635. this.initSort();
  636. };
  637. BootstrapTable.prototype.initSort = function () {
  638. var that = this,
  639. name = this.options.sortName,
  640. order = this.options.sortOrder === 'desc' ? -1 : 1,
  641. index = $.inArray(this.options.sortName, this.header.fields);
  642. if (index !== -1) {
  643. this.data.sort(function (a, b) {
  644. var aa = a[name],
  645. bb = b[name],
  646. value = calculateObjectValue(that.header, that.header.sorters[index], [aa, bb]);
  647. if (value !== undefined) {
  648. return order * value;
  649. }
  650. if (value !== undefined) {
  651. return order * value;
  652. }
  653. // Fix #161: undefined or null string sort bug.
  654. if (aa === undefined || aa === null) {
  655. aa = '';
  656. }
  657. if (bb === undefined || bb === null) {
  658. bb = '';
  659. }
  660. // IF both values are numeric, do a numeric comparison
  661. if ($.isNumeric(aa) && $.isNumeric(bb)) {
  662. // Convert numerical values form string to float.
  663. aa = parseFloat(aa);
  664. bb = parseFloat(bb);
  665. if (aa < bb) {
  666. return order * -1;
  667. }
  668. return order;
  669. }
  670. if (aa === bb) {
  671. return 0;
  672. }
  673. // If value is not a string, convert to string
  674. if (typeof aa !== 'string') {
  675. aa = aa.toString();
  676. }
  677. if (aa.localeCompare(bb) === -1) {
  678. return order * -1;
  679. }
  680. return order;
  681. });
  682. }
  683. };
  684. BootstrapTable.prototype.onSort = function (event) {
  685. var $this = $(event.currentTarget),
  686. $this_ = this.$header.find('th').eq($this.index());
  687. this.$header.add(this.$header_).find('span.order').remove();
  688. if (this.options.sortName === $this.data('field')) {
  689. this.options.sortOrder = this.options.sortOrder === 'asc' ? 'desc' : 'asc';
  690. } else {
  691. this.options.sortName = $this.data('field');
  692. this.options.sortOrder = $this.data('order') === 'asc' ? 'desc' : 'asc';
  693. }
  694. this.trigger('sort', this.options.sortName, this.options.sortOrder);
  695. $this.add($this_).data('order', this.options.sortOrder)
  696. .find('.th-inner').append(this.getCaretHtml());
  697. if (this.options.sidePagination === 'server') {
  698. this.initServer();
  699. return;
  700. }
  701. if (this.options.stateSave && cookieEnabled()) {
  702. setCookie(idSortOrderStateSave, this.options.sortOrder, this.options.stateSaveExpire);
  703. setCookie(idSortNameStateSave, this.options.sortName, this.options.stateSaveExpire);
  704. }
  705. this.initSort();
  706. this.initBody();
  707. };
  708. BootstrapTable.prototype.initToolbar = function () {
  709. var that = this,
  710. html = [],
  711. timeoutId = 0,
  712. $keepOpen,
  713. $search,
  714. switchableCount = 0;
  715. this.$toolbar = this.$container.find('.fixed-table-toolbar').html('');
  716. if (typeof this.options.toolbar === 'string') {
  717. $(sprintf('<div class="bars pull-%s"></div>', this.options.toolbarAlign))
  718. .appendTo(this.$toolbar)
  719. .append($(this.options.toolbar));
  720. }
  721. // showColumns, showToggle, showRefresh
  722. html = [sprintf('<div class="columns columns-%s btn-group pull-%s">',
  723. this.options.buttonsAlign, this.options.buttonsAlign)];
  724. if (typeof this.options.icons === 'string') {
  725. this.options.icons = calculateObjectValue(null, this.options.icons);
  726. }
  727. if (this.options.showPaginationSwitch) {
  728. html.push(sprintf('<button class="btn btn-default" type="button" name="paginationSwitch" title="%s">',
  729. this.options.formatPaginationSwitch()),
  730. sprintf('<i class="%s %s"></i>', this.options.iconsPrefix, this.options.icons.paginationSwitchDown),
  731. '</button>');
  732. }
  733. if (this.options.showRefresh) {
  734. html.push(sprintf('<button class="btn btn-default' + (this.options.iconSize === undefined ? '' : ' btn-' + this.options.iconSize) + '" type="button" name="refresh" title="%s">',
  735. this.options.formatRefresh()),
  736. sprintf('<i class="%s %s"></i>', this.options.iconsPrefix, this.options.icons.refresh),
  737. '</button>');
  738. }
  739. if (this.options.showToggle) {
  740. html.push(sprintf('<button class="btn btn-default' + (this.options.iconSize === undefined ? '' : ' btn-' + this.options.iconSize) + '" type="button" name="toggle" title="%s">',
  741. this.options.formatToggle()),
  742. sprintf('<i class="%s %s"></i>', this.options.iconsPrefix, this.options.icons.toggle),
  743. '</button>');
  744. }
  745. if (this.options.showColumns) {
  746. html.push(sprintf('<div class="keep-open btn-group" title="%s">',
  747. this.options.formatColumns()),
  748. '<button type="button" class="btn btn-default' + (this.options.iconSize == undefined ? '' : ' btn-' + this.options.iconSize) + ' dropdown-toggle" data-toggle="dropdown">',
  749. sprintf('<i class="%s %s"></i>', this.options.iconsPrefix, this.options.icons.columns),
  750. ' <span class="caret"></span>',
  751. '</button>',
  752. '<ul class="dropdown-menu" role="menu">');
  753. $.each(this.options.columns, function (i, column) {
  754. if (column.radio || column.checkbox) {
  755. return;
  756. }
  757. if (that.options.cardView && (!column.cardVisible)) {
  758. return;
  759. }
  760. var checked = column.visible ? ' checked="checked"' : '';
  761. if (column.switchable) {
  762. html.push(sprintf('<li>' +
  763. '<label><input type="checkbox" data-field="%s" value="%s"%s> %s</label>' +
  764. '</li>', column.field, i, checked, column.title));
  765. switchableCount++;
  766. }
  767. });
  768. html.push('</ul>',
  769. '</div>');
  770. }
  771. html.push('</div>');
  772. // Fix #188: this.showToolbar is for extentions
  773. if (this.showToolbar || html.length > 2) {
  774. this.$toolbar.append(html.join(''));
  775. }
  776. if (this.options.showPaginationSwitch) {
  777. this.$toolbar.find('button[name="paginationSwitch"]')
  778. .off('click').on('click', $.proxy(this.togglePagination, this));
  779. }
  780. if (this.options.showRefresh) {
  781. this.$toolbar.find('button[name="refresh"]')
  782. .off('click').on('click', $.proxy(this.refresh, this));
  783. }
  784. if (this.options.showToggle) {
  785. this.$toolbar.find('button[name="toggle"]')
  786. .off('click').on('click', function () {
  787. that.options.cardView = !that.options.cardView;
  788. that.initHeader();
  789. that.initToolbar();
  790. that.initBody();
  791. });
  792. }
  793. if (this.options.showColumns) {
  794. $keepOpen = this.$toolbar.find('.keep-open');
  795. if (switchableCount <= this.options.minimumCountColumns) {
  796. $keepOpen.find('input').prop('disabled', true);
  797. }
  798. $keepOpen.find('li').off('click').on('click', function (event) {
  799. event.stopImmediatePropagation();
  800. });
  801. $keepOpen.find('input').off('click').on('click', function () {
  802. var $this = $(this);
  803. that.toggleColumn($this.val(), $this.prop('checked'), false);
  804. that.trigger('column-switch', $(this).data('field'), $this.prop('checked'));
  805. });
  806. }
  807. if (this.options.search) {
  808. html = [];
  809. html.push(
  810. '<div class="pull-' + this.options.searchAlign + ' search">',
  811. sprintf('<input class="form-control' + (this.options.iconSize === undefined ? '' : ' input-' + this.options.iconSize) + '" type="text" placeholder="%s">',
  812. this.options.formatSearch()),
  813. '</div>');
  814. this.$toolbar.append(html.join(''));
  815. $search = this.$toolbar.find('.search input');
  816. $search.off('keyup').on('keyup', function (event) {
  817. clearTimeout(timeoutId); // doesn't matter if it's 0
  818. timeoutId = setTimeout(function () {
  819. that.onSearch(event);
  820. }, that.options.searchTimeOut);
  821. });
  822. if (this.options.searchText !== '') {
  823. $search.val(this.options.searchText);
  824. clearTimeout(timeoutId); // doesn't matter if it's 0
  825. timeoutId = setTimeout(function () {
  826. $search.trigger('keyup');
  827. }, that.options.searchTimeOut);
  828. }
  829. }
  830. };
  831. BootstrapTable.prototype.onSearch = function (event) {
  832. var text = $.trim($(event.currentTarget).val());
  833. // trim search input
  834. if (this.options.trimOnSearch) {
  835. $(event.currentTarget).val(text);
  836. }
  837. if (text === this.searchText) {
  838. return;
  839. }
  840. this.searchText = text;
  841. this.options.pageNumber = 1;
  842. this.initSearch();
  843. this.updatePagination();
  844. this.trigger('search', text);
  845. };
  846. BootstrapTable.prototype.initSearch = function () {
  847. var that = this;
  848. if (this.options.sidePagination !== 'server') {
  849. var s = this.searchText && this.searchText.toLowerCase();
  850. var f = $.isEmptyObject(this.filterColumns) ? null : this.filterColumns;
  851. // Check filter
  852. this.data = f ? $.grep(this.options.data, function (item, i) {
  853. for (var key in f) {
  854. if (item[key] !== f[key]) {
  855. return false;
  856. }
  857. }
  858. return true;
  859. }) : this.options.data;
  860. this.data = s ? $.grep(this.data, function (item, i) {
  861. for (var key in item) {
  862. key = $.isNumeric(key) ? parseInt(key, 10) : key;
  863. var value = item[key];
  864. // Fix #142: search use formated data
  865. value = calculateObjectValue(that.header,
  866. that.header.formatters[$.inArray(key, that.header.fields)],
  867. [value, item, i], value);
  868. var index = $.inArray(key, that.header.fields);
  869. if (index !== -1 && that.header.searchables[index] &&
  870. (typeof value === 'string' ||
  871. typeof value === 'number') &&
  872. (value + '').toLowerCase().indexOf(s) !== -1) {
  873. return true;
  874. }
  875. }
  876. return false;
  877. }) : this.data;
  878. }
  879. };
  880. BootstrapTable.prototype.initPagination = function () {
  881. this.$pagination = this.$container.find('.fixed-table-pagination');
  882. if (!this.options.pagination) {
  883. this.$pagination.hide();
  884. return;
  885. } else {
  886. this.$pagination.show();
  887. }
  888. var that = this,
  889. html = [],
  890. $allSelected = false,
  891. i, from, to,
  892. $pageList,
  893. $first, $pre,
  894. $next, $last,
  895. $number,
  896. data = this.getData();
  897. if (this.options.sidePagination !== 'server') {
  898. this.options.totalRows = data.length;
  899. }
  900. this.totalPages = 0;
  901. if (this.options.totalRows) {
  902. if (this.options.pageSize === this.options.formatAllRows()) {
  903. this.options.pageSize = this.options.totalRows;
  904. $allSelected = true;
  905. }
  906. this.totalPages = ~~((this.options.totalRows - 1) / this.options.pageSize) + 1;
  907. this.options.totalPages = this.totalPages;
  908. }
  909. if (this.totalPages > 0 && this.options.pageNumber > this.totalPages) {
  910. this.options.pageNumber = this.totalPages;
  911. }
  912. this.pageFrom = (this.options.pageNumber - 1) * this.options.pageSize + 1;
  913. this.pageTo = this.options.pageNumber * this.options.pageSize;
  914. if (this.pageTo > this.options.totalRows) {
  915. this.pageTo = this.options.totalRows;
  916. }
  917. html.push(
  918. '<div class="pull-' + this.options.paginationDetailHAlign + ' pagination-detail">',
  919. '<span class="pagination-info">',
  920. this.options.formatShowingRows(this.pageFrom, this.pageTo, this.options.totalRows),
  921. '</span>');
  922. html.push('<span class="page-list">');
  923. var pageNumber = [
  924. '<span class="btn-group dropup">',
  925. '<button type="button" class="btn btn-default ' + (this.options.iconSize === undefined ? '' : ' btn-' + this.options.iconSize) + ' dropdown-toggle" data-toggle="dropdown">',
  926. '<span class="page-size">',
  927. $allSelected ? this.options.formatAllRows() : this.options.pageSize,
  928. '</span>',
  929. ' <span class="caret"></span>',
  930. '</button>',
  931. '<ul class="dropdown-menu" role="menu">'],
  932. pageList = this.options.pageList;
  933. if (typeof this.options.pageList === 'string') {
  934. var list = this.options.pageList.replace('[', '').replace(']', '').replace(/ /g, '').split(',');
  935. pageList = [];
  936. $.each(list, function (i, value) {
  937. pageList.push(value.toUpperCase() === that.options.formatAllRows().toUpperCase() ?
  938. that.options.formatAllRows() : +value);
  939. });
  940. }
  941. $.each(pageList, function (i, page) {
  942. if (!that.options.smartDisplay || i === 0 || pageList[i - 1] <= that.options.totalRows) {
  943. var active;
  944. if ($allSelected) {
  945. active = page === that.options.formatAllRows() ? ' class="active"' : '';
  946. } else{
  947. active = page === that.options.pageSize ? ' class="active"' : '';
  948. }
  949. pageNumber.push(sprintf('<li%s><a href="javascript:void(0)">%s</a></li>', active, page));
  950. }
  951. });
  952. pageNumber.push('</ul></span>');
  953. html.push(this.options.formatRecordsPerPage(pageNumber.join('')));
  954. html.push('</span>');
  955. html.push('</div>',
  956. '<div class="pull-' + this.options.paginationHAlign + ' pagination">',
  957. '<ul class="pagination' + (this.options.iconSize === undefined ? '' : ' pagination-' + this.options.iconSize) + '">',
  958. '<li class="page-first"><a href="javascript:void(0)">&lt;&lt;</a></li>',
  959. '<li class="page-pre"><a href="javascript:void(0)">&lt;</a></li>');
  960. if (this.totalPages < 5) {
  961. from = 1;
  962. to = this.totalPages;
  963. } else {
  964. from = this.options.pageNumber - 2;
  965. to = from + 4;
  966. if (from < 1) {
  967. from = 1;
  968. to = 5;
  969. }
  970. if (to > this.totalPages) {
  971. to = this.totalPages;
  972. from = to - 4;
  973. }
  974. }
  975. for (i = from; i <= to; i++) {
  976. html.push('<li class="page-number' + (i === this.options.pageNumber ? ' active' : '') + '">',
  977. '<a href="javascript:void(0)">', i, '</a>',
  978. '</li>');
  979. }
  980. html.push(
  981. '<li class="page-next"><a href="javascript:void(0)">&gt;</a></li>',
  982. '<li class="page-last"><a href="javascript:void(0)">&gt;&gt;</a></li>',
  983. '</ul>',
  984. '</div>');
  985. this.$pagination.html(html.join(''));
  986. $pageList = this.$pagination.find('.page-list a');
  987. $first = this.$pagination.find('.page-first');
  988. $pre = this.$pagination.find('.page-pre');
  989. $next = this.$pagination.find('.page-next');
  990. $last = this.$pagination.find('.page-last');
  991. $number = this.$pagination.find('.page-number');
  992. if (this.options.pageNumber <= 1) {
  993. $first.addClass('disabled');
  994. $pre.addClass('disabled');
  995. }
  996. if (this.options.pageNumber >= this.totalPages) {
  997. $next.addClass('disabled');
  998. $last.addClass('disabled');
  999. }
  1000. if (this.options.smartDisplay) {
  1001. if (this.totalPages <= 1) {
  1002. this.$pagination.find('div.pagination').hide();
  1003. }
  1004. if (pageList.length < 2 || this.options.totalRows <= pageList[0]) {
  1005. this.$pagination.find('span.page-list').hide();
  1006. }
  1007. // when data is empty, hide the pagination
  1008. this.$pagination[this.getData().length ? 'show' : 'hide']();
  1009. }
  1010. if ($allSelected) {
  1011. this.options.pageSize = this.options.formatAllRows();
  1012. }
  1013. $pageList.off('click').on('click', $.proxy(this.onPageListChange, this));
  1014. $first.off('click').on('click', $.proxy(this.onPageFirst, this));
  1015. $pre.off('click').on('click', $.proxy(this.onPagePre, this));
  1016. $next.off('click').on('click', $.proxy(this.onPageNext, this));
  1017. $last.off('click').on('click', $.proxy(this.onPageLast, this));
  1018. $number.off('click').on('click', $.proxy(this.onPageNumber, this));
  1019. };
  1020. BootstrapTable.prototype.updatePagination = function (event) {
  1021. // Fix #171: IE disabled button can be clicked bug.
  1022. if (event && $(event.currentTarget).hasClass('disabled')) {
  1023. return;
  1024. }
  1025. if (!this.options.maintainSelected) {
  1026. this.resetRows();
  1027. }
  1028. this.initPagination();
  1029. if (this.options.sidePagination === 'server') {
  1030. this.initServer();
  1031. } else {
  1032. this.initBody();
  1033. }
  1034. this.trigger('page-change', this.options.pageNumber, this.options.pageSize);
  1035. };
  1036. BootstrapTable.prototype.onPageListChange = function (event) {
  1037. var $this = $(event.currentTarget);
  1038. $this.parent().addClass('active').siblings().removeClass('active');
  1039. this.options.pageSize = $this.text().toUpperCase() === this.options.formatAllRows().toUpperCase() ?
  1040. this.options.formatAllRows() : +$this.text();
  1041. this.$toolbar.find('.page-size').text(this.options.pageSize);
  1042. if (this.options.stateSave && cookieEnabled()) {
  1043. setCookie(idPageListStateSave, this.options.pageSize, this.options.stateSaveExpire);
  1044. }
  1045. this.updatePagination(event);
  1046. };
  1047. BootstrapTable.prototype.onPageFirst = function (event) {
  1048. this.options.pageNumber = 1;
  1049. this.updatePagination(event);
  1050. };
  1051. BootstrapTable.prototype.onPagePre = function (event) {
  1052. this.options.pageNumber--;
  1053. this.updatePagination(event);
  1054. };
  1055. BootstrapTable.prototype.onPageNext = function (event) {
  1056. this.options.pageNumber++;
  1057. this.updatePagination(event);
  1058. };
  1059. BootstrapTable.prototype.onPageLast = function (event) {
  1060. this.options.pageNumber = this.totalPages;
  1061. this.updatePagination(event);
  1062. };
  1063. BootstrapTable.prototype.onPageNumber = function (event) {
  1064. if (this.options.pageNumber === +$(event.currentTarget).text()) {
  1065. return;
  1066. }
  1067. this.options.pageNumber = +$(event.currentTarget).text();
  1068. if (this.options.stateSave && cookieEnabled()) {
  1069. setCookie(idPageNumberStateSave, this.options.pageNumber, this.options.stateSaveExpire);
  1070. }
  1071. this.updatePagination(event);
  1072. };
  1073. BootstrapTable.prototype.initBody = function (fixedScroll) {
  1074. var that = this,
  1075. html = [],
  1076. data = this.getData();
  1077. this.trigger('pre-body', data);
  1078. this.$body = this.$el.find('tbody');
  1079. if (!this.$body.length) {
  1080. this.$body = $('<tbody></tbody>').appendTo(this.$el);
  1081. }
  1082. //Fix #389 Bootstrap-table-flatJSON is not working
  1083. if (!this.options.pagination || this.options.sidePagination === 'server') {
  1084. this.pageFrom = 1;
  1085. this.pageTo = data.length;
  1086. }
  1087. for (var i = this.pageFrom - 1; i < this.pageTo; i++) {
  1088. var key,
  1089. item = data[i],
  1090. style = {},
  1091. csses = [],
  1092. attributes = {},
  1093. htmlAttributes = [];
  1094. style = calculateObjectValue(this.options, this.options.rowStyle, [item, i], style);
  1095. if (style && style.css) {
  1096. for (key in style.css) {
  1097. csses.push(key + ': ' + style.css[key]);
  1098. }
  1099. }
  1100. attributes = calculateObjectValue(this.options,
  1101. this.options.rowAttributes, [item, i], attributes);
  1102. if (attributes) {
  1103. for (key in attributes) {
  1104. htmlAttributes.push(sprintf('%s="%s"', key, escapeHTML(attributes[key])));
  1105. }
  1106. }
  1107. html.push('<tr',
  1108. sprintf(' %s', htmlAttributes.join(' ')),
  1109. sprintf(' id="%s"', $.isArray(item) ? undefined : item._id),
  1110. sprintf(' class="%s"', style.classes || ($.isArray(item) ? undefined : item._class)),
  1111. sprintf(' data-index="%s"', i),
  1112. '>'
  1113. );
  1114. if (this.options.cardView) {
  1115. html.push(sprintf('<td colspan="%s">', this.header.fields.length));
  1116. }
  1117. $.each(this.header.fields, function (j, field) {
  1118. var text = '',
  1119. value = item[field],
  1120. type = '',
  1121. cellStyle = {},
  1122. id_ = '',
  1123. class_ = that.header.classes[j],
  1124. data_ = '',
  1125. column = that.options.columns[getFieldIndex(that.options.columns, field)];
  1126. style = sprintf('style="%s"', csses.concat(that.header.styles[j]).join('; '));
  1127. value = calculateObjectValue(that.header,
  1128. that.header.formatters[j], [value, item, i], value);
  1129. // handle td's id and class
  1130. if (item['_' + field + '_id']) {
  1131. id_ = sprintf(' id="%s"', item['_' + field + '_id']);
  1132. }
  1133. if (item['_' + field + '_class']) {
  1134. class_ = sprintf(' class="%s"', item['_' + field + '_class']);
  1135. }
  1136. cellStyle = calculateObjectValue(that.header,
  1137. that.header.cellStyles[j], [value, item, i], cellStyle);
  1138. if (cellStyle.classes) {
  1139. class_ = sprintf(' class="%s"', cellStyle.classes);
  1140. }
  1141. if (cellStyle.css) {
  1142. var csses_ = [];
  1143. for (var key in cellStyle.css) {
  1144. csses_.push(key + ': ' + cellStyle.css[key]);
  1145. }
  1146. style = sprintf('style="%s"', csses_.concat(that.header.styles[j]).join('; '));
  1147. }
  1148. if (item['_' + field + '_data'] && !$.isEmptyObject(item['_' + field + '_data'])) {
  1149. $.each(item['_' + field + '_data'], function (k, v) {
  1150. // ignore data-index
  1151. if (k === 'index') {
  1152. return;
  1153. }
  1154. data_ += sprintf(' data-%s="%s"', k, v);
  1155. });
  1156. }
  1157. if (column.checkbox || column.radio) {
  1158. type = column.checkbox ? 'checkbox' : type;
  1159. type = column.radio ? 'radio' : type;
  1160. text = [that.options.cardView ?
  1161. '<div class="card-view">' : '<td class="bs-checkbox">',
  1162. '<input' +
  1163. sprintf(' data-index="%s"', i) +
  1164. sprintf(' name="%s"', that.options.selectItemName) +
  1165. sprintf(' type="%s"', type) +
  1166. sprintf(' value="%s"', item[that.options.idField]) +
  1167. sprintf(' checked="%s"', value === true ||
  1168. (value && value.checked) ? 'checked' : undefined) +
  1169. sprintf(' disabled="%s"', !column.checkboxEnabled ||
  1170. (value && value.disabled) ? 'disabled' : undefined) +
  1171. ' />',
  1172. that.options.cardView ? '</div>' : '</td>'].join('');
  1173. } else {
  1174. value = typeof value === 'undefined' || value === null ?
  1175. that.options.undefinedText : value;
  1176. text = that.options.cardView ?
  1177. ['<div class="card-view">',
  1178. that.options.showHeader ? sprintf('<span class="title" %s>%s</span>', style,
  1179. getPropertyFromOther(that.options.columns, 'field', 'title', field)) : '',
  1180. sprintf('<span class="value">%s</span>', value),
  1181. '</div>'].join('') :
  1182. [sprintf('<td%s %s %s %s>', id_, class_, style, data_),
  1183. value,
  1184. '</td>'].join('');
  1185. // Hide empty data on Card view when smartDisplay is set to true.
  1186. if (that.options.cardView && that.options.smartDisplay && value === '') {
  1187. text = '';
  1188. }
  1189. }
  1190. html.push(text);
  1191. });
  1192. if (this.options.cardView) {
  1193. html.push('</td>');
  1194. }
  1195. html.push('</tr>');
  1196. }
  1197. // show no records
  1198. if (!html.length) {
  1199. html.push('<tr class="no-records-found">',
  1200. sprintf('<td colspan="%s">%s</td>', this.header.fields.length, this.options.formatNoMatches()),
  1201. '</tr>');
  1202. }
  1203. this.$body.html(html.join(''));
  1204. if (!fixedScroll) {
  1205. this.scrollTo(0);
  1206. }
  1207. // click to select by column
  1208. this.$body.find('> tr > td').off('click').on('click', function () {
  1209. var $tr = $(this).parent();
  1210. that.trigger('click-row', that.data[$tr.data('index')], $tr);
  1211. // if click to select - then trigger the checkbox/radio click
  1212. if (that.options.clickToSelect) {
  1213. if (that.header.clickToSelects[$tr.children().index($(this))]) {
  1214. $tr.find(sprintf('[name="%s"]',
  1215. that.options.selectItemName))[0].click(); // #144: .trigger('click') bug
  1216. }
  1217. }
  1218. });
  1219. this.$body.find('tr').off('dblclick').on('dblclick', function () {
  1220. that.trigger('dbl-click-row', that.data[$(this).data('index')], $(this));
  1221. });
  1222. this.$selectItem = this.$body.find(sprintf('[name="%s"]', this.options.selectItemName));
  1223. this.$selectItem.off('click').on('click', function (event) {
  1224. event.stopImmediatePropagation();
  1225. var checked = $(this).prop('checked'),
  1226. row = that.data[$(this).data('index')];
  1227. row[that.header.stateField] = checked;
  1228. that.trigger(checked ? 'check' : 'uncheck', row);
  1229. if (that.options.singleSelect) {
  1230. that.$selectItem.not(this).each(function () {
  1231. that.data[$(this).data('index')][that.header.stateField] = false;
  1232. });
  1233. that.$selectItem.filter(':checked').not(this).prop('checked', false);
  1234. }
  1235. that.updateSelected();
  1236. });
  1237. $.each(this.header.events, function (i, events) {
  1238. if (!events) {
  1239. return;
  1240. }
  1241. // fix bug, if events is defined with namespace
  1242. if (typeof events === 'string') {
  1243. events = calculateObjectValue(null, events);
  1244. }
  1245. for (var key in events) {
  1246. that.$body.find('tr').each(function () {
  1247. var $tr = $(this),
  1248. $td = $tr.find(that.options.cardView ? '.card-view' : 'td').eq(i),
  1249. index = key.indexOf(' '),
  1250. name = key.substring(0, index),
  1251. el = key.substring(index + 1),
  1252. func = events[key];
  1253. $td.find(el).off(name).on(name, function (e) {
  1254. var index = $tr.data('index'),
  1255. row = that.data[index],
  1256. value = row[that.header.fields[i]];
  1257. func.apply(this, [e, value, row, index]);
  1258. });
  1259. });
  1260. }
  1261. });
  1262. this.updateSelected();
  1263. this.resetView();
  1264. this.trigger('post-body');
  1265. };
  1266. BootstrapTable.prototype.initServer = function (silent, query) {
  1267. var that = this,
  1268. data = {},
  1269. params = {
  1270. pageSize: this.options.pageSize === this.options.formatAllRows() ? this.options.totalRows : this.options.pageSize,
  1271. pageNumber: this.options.pageNumber,
  1272. searchText: this.searchText,
  1273. sortName: this.options.sortName,
  1274. sortOrder: this.options.sortOrder
  1275. };
  1276. if (!this.options.url) {
  1277. return;
  1278. }
  1279. if (this.options.queryParamsType === 'limit') {
  1280. params = {
  1281. search: params.searchText,
  1282. sort: params.sortName,
  1283. order: params.sortOrder
  1284. };
  1285. if (this.options.pagination) {
  1286. params.limit = this.options.pageSize === this.options.formatAllRows() ? this.options.totalRows : this.options.pageSize;
  1287. params.offset = (this.options.pageSize === this.options.formatAllRows() ? this.options.totalRows : this.options.pageSize)
  1288. * (this.options.pageNumber - 1);
  1289. }
  1290. }
  1291. data = calculateObjectValue(this.options, this.options.queryParams, [params], data);
  1292. $.extend(data, query || {});
  1293. // false to stop request
  1294. if (data === false) {
  1295. return;
  1296. }
  1297. if (!silent) {
  1298. this.$loading.show();
  1299. }
  1300. $.ajax($.extend({}, calculateObjectValue(null, this.options.ajaxOptions), {
  1301. type: this.options.method,
  1302. url: this.options.url,
  1303. data: this.options.contentType === 'application/json' && this.options.method === 'post' ?
  1304. JSON.stringify(data) : data,
  1305. cache: this.options.cache,
  1306. contentType: this.options.contentType,
  1307. dataType: this.options.dataType,
  1308. success: function (res) {
  1309. res = calculateObjectValue(that.options, that.options.responseHandler, [res], res);
  1310. that.load(res);
  1311. that.trigger('load-success', res);
  1312. },
  1313. error: function (res) {
  1314. that.trigger('load-error', res.status);
  1315. },
  1316. complete: function () {
  1317. if (!silent) {
  1318. that.$loading.hide();
  1319. }
  1320. }
  1321. }));
  1322. };
  1323. BootstrapTable.prototype.initKeyEvents = function () {
  1324. if (this.options.keyEvents) {
  1325. var that = this;
  1326. $(document).off('keypress').on('keypress', function (e) {
  1327. if (!that.options.search) {
  1328. return;
  1329. }
  1330. switch (e.keyCode) {
  1331. case 115://s
  1332. case 83://S
  1333. var $search = that.$toolbar.find('.search input');
  1334. if(document.activeElement === $search.get(0)){
  1335. return true;
  1336. }
  1337. $search.focus();
  1338. return false;
  1339. }
  1340. });
  1341. }
  1342. };
  1343. BootstrapTable.prototype.initStateSave = function () {
  1344. if (!this.options.stateSave) {
  1345. return;
  1346. }
  1347. if (!cookieEnabled()) {
  1348. return;
  1349. }
  1350. if (this.options.stateSaveIdTable === '') {
  1351. return;
  1352. }
  1353. idStateSave = this.options.stateSaveIdTable + '.';
  1354. var sortOrderStateSave = getCookie(idSortOrderStateSave),
  1355. sortOrderStateName = getCookie(idSortNameStateSave),
  1356. pageNumberStateSave = getCookie(idPageNumberStateSave),
  1357. pageListStateSave = getCookie(idPageListStateSave);
  1358. if (sortOrderStateSave !== undefined && sortOrderStateSave !== null) {
  1359. this.options.sortOrder = sortOrderStateSave,
  1360. this.options.sortName = sortOrderStateName;
  1361. }
  1362. if (pageNumberStateSave !== undefined && pageNumberStateSave !== null) {
  1363. this.options.pageNumber = +pageNumberStateSave;
  1364. }
  1365. if (pageListStateSave !== undefined && pageListStateSave !== null) {
  1366. this.options.pageSize = pageListStateSave === this.options.formatAllRows() ? pageListStateSave : +pageListStateSave;
  1367. }
  1368. };
  1369. BootstrapTable.prototype.getCaretHtml = function () {
  1370. return ['<span class="order' + (this.options.sortOrder === 'desc' ? '' : ' dropup') + '">',
  1371. '<span class="caret" style="margin: 10px 5px;"></span>',
  1372. '</span>'].join('');
  1373. };
  1374. BootstrapTable.prototype.updateSelected = function () {
  1375. var checkAll = this.$selectItem.filter(':enabled').length ===
  1376. this.$selectItem.filter(':enabled').filter(':checked').length;
  1377. this.$selectAll.add(this.$selectAll_).prop('checked', checkAll);
  1378. this.$selectItem.each(function () {
  1379. $(this).parents('tr')[$(this).prop('checked') ? 'addClass' : 'removeClass']('selected');
  1380. });
  1381. };
  1382. BootstrapTable.prototype.updateRows = function (checked) {
  1383. var that = this;
  1384. this.$selectItem.each(function () {
  1385. that.data[$(this).data('index')][that.header.stateField] = checked;
  1386. });
  1387. };
  1388. BootstrapTable.prototype.resetRows = function () {
  1389. var that = this;
  1390. $.each(this.data, function (i, row) {
  1391. that.$selectAll.prop('checked', false);
  1392. that.$selectItem.prop('checked', false);
  1393. row[that.header.stateField] = false;
  1394. });
  1395. };
  1396. BootstrapTable.prototype.trigger = function (name) {
  1397. var args = Array.prototype.slice.call(arguments, 1);
  1398. name += '.bs.table';
  1399. this.options[BootstrapTable.EVENTS[name]].apply(this.options, args);
  1400. this.$el.trigger($.Event(name), args);
  1401. this.options.onAll(name, args);
  1402. this.$el.trigger($.Event('all.bs.table'), [name, args]);
  1403. };
  1404. BootstrapTable.prototype.resetHeader = function () {
  1405. this.$el.css('margin-top', -this.$header.height());
  1406. // fix #61: the hidden table reset header bug.
  1407. // fix bug: get $el.css('width') error sometime (height = 500)
  1408. clearTimeout(this.timeoutId_);
  1409. this.timeoutId_ = setTimeout($.proxy(this.fitHeader, this), this.$el.is(':hidden') ? 100 : 0);
  1410. return;
  1411. };
  1412. BootstrapTable.prototype.fitHeader = function () {
  1413. var that = this,
  1414. $fixedHeader,
  1415. $fixedBody,
  1416. scrollWidth;
  1417. if (that.$el.is(':hidden')) {
  1418. that.timeoutFooter_ = setTimeout($.proxy(that.fitHeader, that), 100);
  1419. return;
  1420. }
  1421. $fixedHeader = that.$container.find('.fixed-table-header'),
  1422. $fixedBody = that.$container.find('.fixed-table-body'),
  1423. scrollWidth = that.$el.width() > $fixedBody.width() ? getScrollBarWidth() : 0;
  1424. that.$header_ = that.$header.clone(true, true);
  1425. that.$selectAll_ = that.$header_.find('[name="btSelectAll"]');
  1426. $fixedHeader.css({
  1427. 'margin-right': scrollWidth
  1428. }).find('table').css('width', that.$el.css('width'))
  1429. .html('').attr('class', that.$el.attr('class'))
  1430. .append(that.$header_);
  1431. // fix bug: $.data() is not working as expected after $.append()
  1432. that.$header.find('th').each(function (i) {
  1433. that.$header_.find('th').eq(i).data($(this).data());
  1434. });
  1435. that.$body.find('tr:first-child:not(.no-records-found) > *').each(function (i) {
  1436. that.$header_.find('div.fht-cell').eq(i).width($(this).innerWidth());
  1437. });
  1438. // horizontal scroll event
  1439. // TODO: it's probably better improving the layout than binding to scroll event
  1440. $fixedBody.off('scroll').on('scroll', function () {
  1441. $fixedHeader.scrollLeft($(this).scrollLeft());
  1442. });
  1443. that.trigger('post-header');
  1444. };
  1445. BootstrapTable.prototype.toggleColumn = function (index, checked, needUpdate) {
  1446. if (index === -1) {
  1447. return;
  1448. }
  1449. this.options.columns[index].visible = checked;
  1450. this.initHeader();
  1451. this.initSearch();
  1452. this.initPagination();
  1453. this.initBody();
  1454. if (this.options.showColumns) {
  1455. var $items = this.$toolbar.find('.keep-open input').prop('disabled', false);
  1456. if (needUpdate) {
  1457. $items.filter(sprintf('[value="%s"]', index)).prop('checked', checked);
  1458. }
  1459. if ($items.filter(':checked').length <= this.options.minimumCountColumns) {
  1460. $items.filter(':checked').prop('disabled', true);
  1461. }
  1462. }
  1463. };
  1464. BootstrapTable.prototype.toggleRow = function (index, visible) {
  1465. if (index === -1) {
  1466. return;
  1467. }
  1468. this.$selectItem.filter(sprintf('[data-index="%s"]', index))
  1469. .parents('tr')[visible ? 'show' : 'hide']();
  1470. };
  1471. // PUBLIC FUNCTION DEFINITION
  1472. // =======================
  1473. BootstrapTable.prototype.resetView = function (params) {
  1474. var that = this,
  1475. padding = 0,
  1476. $tableContainer = that.$container.find('.fixed-table-container');
  1477. if (params && params.height) {
  1478. this.options.height = params.height;
  1479. }
  1480. this.$selectAll.prop('checked', this.$selectItem.length > 0 &&
  1481. this.$selectItem.length === this.$selectItem.filter(':checked').length);
  1482. if (this.options.height) {
  1483. var toolbarHeight = +this.$toolbar.children().outerHeight(true),
  1484. paginationHeight = +this.$pagination.children().outerHeight(true),
  1485. height = this.options.height - toolbarHeight - paginationHeight;
  1486. $tableContainer.css('height', height + 'px');
  1487. }
  1488. if (this.options.cardView) {
  1489. // remove the element css
  1490. that.$el.css('margin-top', '0');
  1491. $tableContainer.css('padding-bottom', '0');
  1492. return;
  1493. }
  1494. if (this.options.showHeader && this.options.height) {
  1495. this.resetHeader();
  1496. padding += cellHeight;
  1497. } else {
  1498. this.trigger('post-header');
  1499. }
  1500. if (this.options.showFooter) {
  1501. this.resetFooter();
  1502. if (this.options.height) {
  1503. padding += cellHeight;
  1504. }
  1505. }
  1506. $tableContainer.css('padding-bottom', padding + 'px');
  1507. };
  1508. BootstrapTable.prototype.getData = function () {
  1509. return (this.searchText || !$.isEmptyObject(this.filterColumns)) ? this.data : this.options.data;
  1510. };
  1511. BootstrapTable.prototype.load = function (data) {
  1512. var fixedScroll = false;
  1513. // #431: support pagination
  1514. if (this.options.sidePagination === 'server') {
  1515. this.options.totalRows = data.total;
  1516. fixedScroll = data.fixedScroll;
  1517. data = data.rows;
  1518. } else if (!$.isArray(data)) { // support fixedScroll
  1519. fixedScroll = data.fixedScroll;
  1520. data = data.data;
  1521. }
  1522. this.initData(data);
  1523. this.initSearch();
  1524. this.initPagination();
  1525. this.initBody(fixedScroll);
  1526. };
  1527. BootstrapTable.prototype.append = function (data) {
  1528. this.initData(data, 'append');
  1529. this.initSearch();
  1530. this.initPagination();
  1531. this.initBody(true);
  1532. };
  1533. BootstrapTable.prototype.prepend = function (data) {
  1534. this.initData(data, 'prepend');
  1535. this.initSearch();
  1536. this.initPagination();
  1537. this.initBody(true);
  1538. };
  1539. BootstrapTable.prototype.remove = function (params) {
  1540. var len = this.options.data.length,
  1541. i, row;
  1542. if (!params.hasOwnProperty('field') || !params.hasOwnProperty('values')) {
  1543. return;
  1544. }
  1545. for (i = len - 1; i >= 0; i--) {
  1546. row = this.options.data[i];
  1547. if (!row.hasOwnProperty(params.field)) {
  1548. continue;
  1549. }
  1550. if ($.inArray(row[params.field], params.values) !== -1) {
  1551. this.options.data.splice(i, 1);
  1552. }
  1553. }
  1554. if (len === this.options.data.length) {
  1555. return;
  1556. }
  1557. this.initSearch();
  1558. this.initPagination();
  1559. this.initBody(true);
  1560. };
  1561. BootstrapTable.prototype.insertRow = function (params) {
  1562. if (!params.hasOwnProperty('index') || !params.hasOwnProperty('row')) {
  1563. return;
  1564. }
  1565. this.data.splice(params.index, 0, params.row);
  1566. this.initBody(true);
  1567. };
  1568. BootstrapTable.prototype.updateRow = function (params) {
  1569. if (!params.hasOwnProperty('index') || !params.hasOwnProperty('row')) {
  1570. return;
  1571. }
  1572. $.extend(this.data[params.index], params.row);
  1573. this.initBody(true);
  1574. };
  1575. BootstrapTable.prototype.showRow = function (index) {
  1576. this.toggleRow(index, true);
  1577. };
  1578. BootstrapTable.prototype.hideRow = function (index) {
  1579. this.toggleRow(index, false);
  1580. };
  1581. BootstrapTable.prototype.mergeCells = function (options) {
  1582. var row = options.index,
  1583. col = $.inArray(options.field, this.header.fields),
  1584. rowspan = options.rowspan || 1,
  1585. colspan = options.colspan || 1,
  1586. i, j,
  1587. $tr = this.$body.find('tr'),
  1588. $td = $tr.eq(row).find('td').eq(col);
  1589. if (row < 0 || col < 0 || row >= this.data.length) {
  1590. return;
  1591. }
  1592. for (i = row; i < row + rowspan; i++) {
  1593. for (j = col; j < col + colspan; j++) {
  1594. $tr.eq(i).find('td').eq(j).hide();
  1595. }
  1596. }
  1597. $td.attr('rowspan', rowspan).attr('colspan', colspan).show();
  1598. };
  1599. BootstrapTable.prototype.getOptions = function () {
  1600. return this.options;
  1601. };
  1602. BootstrapTable.prototype.getSelections = function () {
  1603. var that = this;
  1604. return $.grep(this.data, function (row) {
  1605. return row[that.header.stateField];
  1606. });
  1607. };
  1608. BootstrapTable.prototype.checkAll = function () {
  1609. this.checkAll_(true);
  1610. };
  1611. BootstrapTable.prototype.uncheckAll = function () {
  1612. this.checkAll_(false);
  1613. };
  1614. BootstrapTable.prototype.checkAll_ = function (checked) {
  1615. var rows;
  1616. if (!checked) {
  1617. rows = this.getSelections();
  1618. }
  1619. this.$selectItem.filter(':enabled').prop('checked', checked);
  1620. this.updateRows(checked);
  1621. this.updateSelected();
  1622. if (checked) {
  1623. rows = this.getSelections();
  1624. }
  1625. this.trigger(checked ? 'check-all' : 'uncheck-all', rows);
  1626. };
  1627. BootstrapTable.prototype.check = function (index) {
  1628. this.check_(true, index);
  1629. };
  1630. BootstrapTable.prototype.uncheck = function (index) {
  1631. this.check_(false, index);
  1632. };
  1633. BootstrapTable.prototype.check_ = function (checked, index) {
  1634. this.$selectItem.filter(sprintf('[data-index="%s"]', index)).prop('checked', checked);
  1635. this.data[index][this.header.stateField] = checked;
  1636. this.updateSelected();
  1637. this.trigger(checked ? 'check' : 'uncheck', this.data[index]);
  1638. };
  1639. BootstrapTable.prototype.checkBy = function (obj) {
  1640. this.checkBy_(true, obj);
  1641. };
  1642. BootstrapTable.prototype.uncheckBy = function (obj) {
  1643. this.checkBy_(false, obj);
  1644. };
  1645. BootstrapTable.prototype.checkBy_ = function (checked, obj) {
  1646. if (!obj.hasOwnProperty('field') || !obj.hasOwnProperty('values')) {
  1647. return;
  1648. }
  1649. var that = this;
  1650. $.each(this.options.data, function (index, row) {
  1651. if (!row.hasOwnProperty(obj.field)) {
  1652. return false;
  1653. }
  1654. if ($.inArray(row[obj.field], obj.values) !== -1) {
  1655. that.$selectItem.filter(sprintf('[data-index="%s"]', index)).prop('checked', checked);
  1656. row[that.header.stateField] = checked;
  1657. that.trigger(checked ? 'check' : 'uncheck', row);
  1658. }
  1659. });
  1660. this.updateSelected();
  1661. };
  1662. BootstrapTable.prototype.destroy = function () {
  1663. this.$el.insertBefore(this.$container);
  1664. $(this.options.toolbar).insertBefore(this.$el);
  1665. this.$container.next().remove();
  1666. this.$container.remove();
  1667. this.$el.html(this.$el_.html())
  1668. .css('margin-top', '0')
  1669. .attr('class', this.$el_.attr('class') || ''); // reset the class
  1670. };
  1671. BootstrapTable.prototype.showLoading = function () {
  1672. this.$loading.show();
  1673. };
  1674. BootstrapTable.prototype.hideLoading = function () {
  1675. this.$loading.hide();
  1676. };
  1677. BootstrapTable.prototype.togglePagination = function () {
  1678. this.options.pagination = !this.options.pagination;
  1679. var button = this.$toolbar.find('button[name="paginationSwitch"] i');
  1680. if (this.options.pagination) {
  1681. button.attr("class", this.options.iconsPrefix + " " + this.options.icons.paginationSwitchDown);
  1682. } else {
  1683. button.attr("class", this.options.iconsPrefix + " " + this.options.icons.paginationSwitchUp);
  1684. }
  1685. this.updatePagination();
  1686. };
  1687. BootstrapTable.prototype.refresh = function (params) {
  1688. if (params && params.url) {
  1689. this.options.url = params.url;
  1690. this.options.pageNumber = 1;
  1691. }
  1692. this.initServer(params && params.silent, params && params.query);
  1693. };
  1694. BootstrapTable.prototype.showColumn = function (field) {
  1695. this.toggleColumn(getFieldIndex(this.options.columns, field), true, true);
  1696. };
  1697. BootstrapTable.prototype.hideColumn = function (field) {
  1698. this.toggleColumn(getFieldIndex(this.options.columns, field), false, true);
  1699. };
  1700. BootstrapTable.prototype.filterBy = function (columns) {
  1701. this.filterColumns = $.isEmptyObject(columns) ? {} : columns;
  1702. this.options.pageNumber = 1;
  1703. this.initSearch();
  1704. this.updatePagination();
  1705. };
  1706. BootstrapTable.prototype.scrollTo = function (value) {
  1707. var $tbody = this.$container.find('.fixed-table-body');
  1708. if (typeof value === 'string') {
  1709. value = value === 'bottom' ? $tbody[0].scrollHeight : 0;
  1710. }
  1711. if (typeof value === 'number') {
  1712. $tbody.scrollTop(value);
  1713. }
  1714. };
  1715. BootstrapTable.prototype.selectPage = function (page) {
  1716. if (page > 0 && page <= this.options.totalPages) {
  1717. this.options.pageNumber = page;
  1718. this.updatePagination();
  1719. }
  1720. };
  1721. BootstrapTable.prototype.prevPage = function () {
  1722. if (this.options.pageNumber > 1) {
  1723. this.options.pageNumber--;
  1724. this.updatePagination();
  1725. }
  1726. };
  1727. BootstrapTable.prototype.nextPage = function () {
  1728. if (this.options.pageNumber < this.options.totalPages) {
  1729. this.options.pageNumber++;
  1730. this.updatePagination();
  1731. }
  1732. };
  1733. BootstrapTable.prototype.toggleView = function () {
  1734. this.options.cardView = !this.options.cardView;
  1735. this.initHeader();
  1736. this.initToolbar();
  1737. this.initBody();
  1738. };
  1739. BootstrapTable.prototype.deleteCookie = function (cookieName) {
  1740. if (cookieName === '') {
  1741. return;
  1742. }
  1743. if (!cookieEnabled()) {
  1744. return;
  1745. }
  1746. deleteCookie(idsStateSaveList[cookieName]);
  1747. }
  1748. // BOOTSTRAP TABLE PLUGIN DEFINITION
  1749. // =======================
  1750. var allowedMethods = [
  1751. 'getOptions',
  1752. 'getSelections', 'getData',
  1753. 'load', 'append', 'prepend', 'remove',
  1754. 'insertRow', 'updateRow',
  1755. 'showRow', 'hideRow',
  1756. 'mergeCells',
  1757. 'checkAll', 'uncheckAll',
  1758. 'check', 'uncheck',
  1759. 'checkBy', 'uncheckBy',
  1760. 'refresh',
  1761. 'resetView',
  1762. 'destroy',
  1763. 'showLoading', 'hideLoading',
  1764. 'showColumn', 'hideColumn',
  1765. 'filterBy',
  1766. 'scrollTo',
  1767. 'selectPage', 'prevPage', 'nextPage',
  1768. 'togglePagination',
  1769. 'toggleView',
  1770. 'deleteCookie'
  1771. ];
  1772. $.fn.bootstrapTable = function (option, _relatedTarget) {
  1773. var value;
  1774. this.each(function () {
  1775. var $this = $(this),
  1776. data = $this.data('bootstrap.table'),
  1777. options = $.extend({}, BootstrapTable.DEFAULTS, $this.data(),
  1778. typeof option === 'object' && option);
  1779. if (typeof option === 'string') {
  1780. if ($.inArray(option, allowedMethods) < 0) {
  1781. throw "Unknown method: " + option;
  1782. }
  1783. if (!data) {
  1784. return;
  1785. }
  1786. value = data[option](_relatedTarget);
  1787. if (option === 'destroy') {
  1788. $this.removeData('bootstrap.table');
  1789. }
  1790. }
  1791. if (!data) {
  1792. $this.data('bootstrap.table', (data = new BootstrapTable(this, options)));
  1793. }
  1794. });
  1795. return typeof value === 'undefined' ? this : value;
  1796. };
  1797. $.fn.bootstrapTable.Constructor = BootstrapTable;
  1798. $.fn.bootstrapTable.defaults = BootstrapTable.DEFAULTS;
  1799. $.fn.bootstrapTable.columnDefaults = BootstrapTable.COLUMN_DEFAULTS;
  1800. $.fn.bootstrapTable.locales = BootstrapTable.LOCALES;
  1801. $.fn.bootstrapTable.methods = allowedMethods;
  1802. // BOOTSTRAP TABLE INIT
  1803. // =======================
  1804. $(function () {
  1805. $('[data-toggle="table"]').bootstrapTable();
  1806. });
  1807. }(jQuery);