bootstrap-table.js 73 KB

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