bootstrap-table.js 74 KB

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