bootstrap-table.js 76 KB

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