bootstrap-table.js 75 KB

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