bootstrap-table.js 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206
  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. 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 && $(event.currentTarget).val() !== text) {
  757. $(event.currentTarget).val(text);
  758. }
  759. if (text === this.searchText) {
  760. return;
  761. }
  762. this.searchText = text;
  763. this.options.pageNumber = 1;
  764. this.initSearch();
  765. this.updatePagination();
  766. this.trigger('search', text);
  767. };
  768. BootstrapTable.prototype.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. // Fix #667 Table with pagination, multiple pages and a search that matches to one page throws exception
  864. var pageLst = typeof this.options.pageList === 'string' ?
  865. this.options.pageList.replace('[', '').replace(']', '').replace(/ /g, '').toLowerCase().split(',') :
  866. this.options.pageList;
  867. if (pageLst.indexOf(this.options.formatAllRows().toLowerCase()) > -1) {
  868. $allSelected = true;
  869. }
  870. }
  871. this.totalPages = ~~((this.options.totalRows - 1) / this.options.pageSize) + 1;
  872. this.options.totalPages = this.totalPages;
  873. }
  874. if (this.totalPages > 0 && this.options.pageNumber > this.totalPages) {
  875. this.options.pageNumber = this.totalPages;
  876. }
  877. this.pageFrom = (this.options.pageNumber - 1) * this.options.pageSize + 1;
  878. this.pageTo = this.options.pageNumber * this.options.pageSize;
  879. if (this.pageTo > this.options.totalRows) {
  880. this.pageTo = this.options.totalRows;
  881. }
  882. html.push(
  883. '<div class="pull-' + this.options.paginationDetailHAlign + ' pagination-detail">',
  884. '<span class="pagination-info">',
  885. this.options.formatShowingRows(this.pageFrom, this.pageTo, this.options.totalRows),
  886. '</span>');
  887. html.push('<span class="page-list">');
  888. var pageNumber = [
  889. sprintf('<span class="btn-group %s">', this.options.paginationVAlign === 'top' || this.options.paginationVAlign === 'both' ?
  890. 'dropdown' : 'dropup'),
  891. '<button type="button" class="btn btn-default ' + (this.options.iconSize === undefined ? '' : ' btn-' + this.options.iconSize) + ' dropdown-toggle" data-toggle="dropdown">',
  892. '<span class="page-size">',
  893. $allSelected ? this.options.formatAllRows() : this.options.pageSize,
  894. '</span>',
  895. ' <span class="caret"></span>',
  896. '</button>',
  897. '<ul class="dropdown-menu" role="menu">'],
  898. pageList = this.options.pageList;
  899. if (typeof this.options.pageList === 'string') {
  900. var list = this.options.pageList.replace('[', '').replace(']', '').replace(/ /g, '').split(',');
  901. pageList = [];
  902. $.each(list, function (i, value) {
  903. pageList.push(value.toUpperCase() === that.options.formatAllRows().toUpperCase() ?
  904. that.options.formatAllRows() : +value);
  905. });
  906. }
  907. $.each(pageList, function (i, page) {
  908. if (!that.options.smartDisplay || i === 0 || pageList[i - 1] <= that.options.totalRows) {
  909. var active;
  910. if ($allSelected) {
  911. active = page === that.options.formatAllRows() ? ' class="active"' : '';
  912. } else{
  913. active = page === that.options.pageSize ? ' class="active"' : '';
  914. }
  915. pageNumber.push(sprintf('<li%s><a href="javascript:void(0)">%s</a></li>', active, page));
  916. }
  917. });
  918. pageNumber.push('</ul></span>');
  919. html.push(this.options.formatRecordsPerPage(pageNumber.join('')));
  920. html.push('</span>');
  921. // Fixed #611 vertical-align between pagination block and pagination-detail block. Remove class pagination.
  922. html.push('</div>',
  923. '<div class="pull-' + this.options.paginationHAlign + '">',
  924. '<ul class="pagination' + (this.options.iconSize === undefined ? '' : ' pagination-' + this.options.iconSize) + '">',
  925. '<li class="page-first"><a href="javascript:void(0)">&lt;&lt;</a></li>',
  926. '<li class="page-pre"><a href="javascript:void(0)">&lt;</a></li>');
  927. if (this.totalPages < 5) {
  928. from = 1;
  929. to = this.totalPages;
  930. } else {
  931. from = this.options.pageNumber - 2;
  932. to = from + 4;
  933. if (from < 1) {
  934. from = 1;
  935. to = 5;
  936. }
  937. if (to > this.totalPages) {
  938. to = this.totalPages;
  939. from = to - 4;
  940. }
  941. }
  942. for (i = from; i <= to; i++) {
  943. html.push('<li class="page-number' + (i === this.options.pageNumber ? ' active' : '') + '">',
  944. '<a href="javascript:void(0)">', i, '</a>',
  945. '</li>');
  946. }
  947. html.push(
  948. '<li class="page-next"><a href="javascript:void(0)">&gt;</a></li>',
  949. '<li class="page-last"><a href="javascript:void(0)">&gt;&gt;</a></li>',
  950. '</ul>',
  951. '</div>');
  952. this.$pagination.html(html.join(''));
  953. $pageList = this.$pagination.find('.page-list a');
  954. $first = this.$pagination.find('.page-first');
  955. $pre = this.$pagination.find('.page-pre');
  956. $next = this.$pagination.find('.page-next');
  957. $last = this.$pagination.find('.page-last');
  958. $number = this.$pagination.find('.page-number');
  959. if (this.options.pageNumber <= 1) {
  960. $first.addClass('disabled');
  961. $pre.addClass('disabled');
  962. }
  963. if (this.options.pageNumber >= this.totalPages) {
  964. $next.addClass('disabled');
  965. $last.addClass('disabled');
  966. }
  967. if (this.options.smartDisplay) {
  968. if (this.totalPages <= 1) {
  969. this.$pagination.find('div.pagination').hide();
  970. }
  971. if (pageList.length < 2 || this.options.totalRows <= pageList[0]) {
  972. this.$pagination.find('span.page-list').hide();
  973. }
  974. // when data is empty, hide the pagination
  975. this.$pagination[this.getData().length ? 'show' : 'hide']();
  976. }
  977. if ($allSelected) {
  978. this.options.pageSize = this.options.formatAllRows();
  979. }
  980. $pageList.off('click').on('click', $.proxy(this.onPageListChange, this));
  981. $first.off('click').on('click', $.proxy(this.onPageFirst, this));
  982. $pre.off('click').on('click', $.proxy(this.onPagePre, this));
  983. $next.off('click').on('click', $.proxy(this.onPageNext, this));
  984. $last.off('click').on('click', $.proxy(this.onPageLast, this));
  985. $number.off('click').on('click', $.proxy(this.onPageNumber, this));
  986. };
  987. BootstrapTable.prototype.updatePagination = function (event) {
  988. // Fix #171: IE disabled button can be clicked bug.
  989. if (event && $(event.currentTarget).hasClass('disabled')) {
  990. return;
  991. }
  992. if (!this.options.maintainSelected) {
  993. this.resetRows();
  994. }
  995. this.initPagination();
  996. if (this.options.sidePagination === 'server') {
  997. this.initServer();
  998. } else {
  999. this.initBody();
  1000. }
  1001. this.trigger('page-change', this.options.pageNumber, this.options.pageSize);
  1002. };
  1003. BootstrapTable.prototype.onPageListChange = function (event) {
  1004. var $this = $(event.currentTarget);
  1005. $this.parent().addClass('active').siblings().removeClass('active');
  1006. this.options.pageSize = $this.text().toUpperCase() === this.options.formatAllRows().toUpperCase() ?
  1007. this.options.formatAllRows() : +$this.text();
  1008. this.$toolbar.find('.page-size').text(this.options.pageSize);
  1009. this.updatePagination(event);
  1010. };
  1011. BootstrapTable.prototype.onPageFirst = function (event) {
  1012. this.options.pageNumber = 1;
  1013. this.updatePagination(event);
  1014. };
  1015. BootstrapTable.prototype.onPagePre = function (event) {
  1016. this.options.pageNumber--;
  1017. this.updatePagination(event);
  1018. };
  1019. BootstrapTable.prototype.onPageNext = function (event) {
  1020. this.options.pageNumber++;
  1021. this.updatePagination(event);
  1022. };
  1023. BootstrapTable.prototype.onPageLast = function (event) {
  1024. this.options.pageNumber = this.totalPages;
  1025. this.updatePagination(event);
  1026. };
  1027. BootstrapTable.prototype.onPageNumber = function (event) {
  1028. if (this.options.pageNumber === +$(event.currentTarget).text()) {
  1029. return;
  1030. }
  1031. this.options.pageNumber = +$(event.currentTarget).text();
  1032. this.updatePagination(event);
  1033. };
  1034. BootstrapTable.prototype.initBody = function (fixedScroll) {
  1035. var that = this,
  1036. html = [],
  1037. data = this.getData();
  1038. this.trigger('pre-body', data);
  1039. this.$body = this.$el.find('tbody');
  1040. if (!this.$body.length) {
  1041. this.$body = $('<tbody></tbody>').appendTo(this.$el);
  1042. }
  1043. //Fix #389 Bootstrap-table-flatJSON is not working
  1044. if (!this.options.pagination || this.options.sidePagination === 'server') {
  1045. this.pageFrom = 1;
  1046. this.pageTo = data.length;
  1047. }
  1048. for (var i = this.pageFrom - 1; i < this.pageTo; i++) {
  1049. var key,
  1050. item = data[i],
  1051. style = {},
  1052. csses = [],
  1053. attributes = {},
  1054. htmlAttributes = [];
  1055. style = calculateObjectValue(this.options, this.options.rowStyle, [item, i], style);
  1056. if (style && style.css) {
  1057. for (key in style.css) {
  1058. csses.push(key + ': ' + style.css[key]);
  1059. }
  1060. }
  1061. attributes = calculateObjectValue(this.options,
  1062. this.options.rowAttributes, [item, i], attributes);
  1063. if (attributes) {
  1064. for (key in attributes) {
  1065. htmlAttributes.push(sprintf('%s="%s"', key, escapeHTML(attributes[key])));
  1066. }
  1067. }
  1068. html.push('<tr',
  1069. sprintf(' %s', htmlAttributes.join(' ')),
  1070. sprintf(' id="%s"', $.isArray(item) ? undefined : item._id),
  1071. sprintf(' class="%s"', style.classes || ($.isArray(item) ? undefined : item._class)),
  1072. sprintf(' data-index="%s"', i),
  1073. sprintf(' data-unique-id="%s"', item[this.options.uniqueId]),
  1074. '>'
  1075. );
  1076. if (this.options.cardView) {
  1077. html.push(sprintf('<td colspan="%s">', this.header.fields.length));
  1078. }
  1079. $.each(this.header.fields, function (j, field) {
  1080. var text = '',
  1081. value = item[field],
  1082. type = '',
  1083. cellStyle = {},
  1084. id_ = '',
  1085. class_ = that.header.classes[j],
  1086. data_ = '',
  1087. column = that.options.columns[getFieldIndex(that.options.columns, field)];
  1088. style = sprintf('style="%s"', csses.concat(that.header.styles[j]).join('; '));
  1089. value = calculateObjectValue(that.header,
  1090. that.header.formatters[j], [value, item, i], value);
  1091. // handle td's id and class
  1092. if (item['_' + field + '_id']) {
  1093. id_ = sprintf(' id="%s"', item['_' + field + '_id']);
  1094. }
  1095. if (item['_' + field + '_class']) {
  1096. class_ = sprintf(' class="%s"', item['_' + field + '_class']);
  1097. }
  1098. cellStyle = calculateObjectValue(that.header,
  1099. that.header.cellStyles[j], [value, item, i], cellStyle);
  1100. if (cellStyle.classes) {
  1101. class_ = sprintf(' class="%s"', cellStyle.classes);
  1102. }
  1103. if (cellStyle.css) {
  1104. var csses_ = [];
  1105. for (var key in cellStyle.css) {
  1106. csses_.push(key + ': ' + cellStyle.css[key]);
  1107. }
  1108. style = sprintf('style="%s"', csses_.concat(that.header.styles[j]).join('; '));
  1109. }
  1110. if (item['_' + field + '_data'] && !$.isEmptyObject(item['_' + field + '_data'])) {
  1111. $.each(item['_' + field + '_data'], function (k, v) {
  1112. // ignore data-index
  1113. if (k === 'index') {
  1114. return;
  1115. }
  1116. data_ += sprintf(' data-%s="%s"', k, v);
  1117. });
  1118. }
  1119. if (column.checkbox || column.radio) {
  1120. type = column.checkbox ? 'checkbox' : type;
  1121. type = column.radio ? 'radio' : type;
  1122. text = [that.options.cardView ?
  1123. '<div class="card-view">' : '<td class="bs-checkbox">',
  1124. '<input' +
  1125. sprintf(' data-index="%s"', i) +
  1126. sprintf(' name="%s"', that.options.selectItemName) +
  1127. sprintf(' type="%s"', type) +
  1128. sprintf(' value="%s"', item[that.options.idField]) +
  1129. sprintf(' checked="%s"', value === true ||
  1130. (value && value.checked) ? 'checked' : undefined) +
  1131. sprintf(' disabled="%s"', !column.checkboxEnabled ||
  1132. (value && value.disabled) ? 'disabled' : undefined) +
  1133. ' />',
  1134. that.options.cardView ? '</div>' : '</td>'].join('');
  1135. } else {
  1136. value = typeof value === 'undefined' || value === null ?
  1137. that.options.undefinedText : value;
  1138. text = that.options.cardView ?
  1139. ['<div class="card-view">',
  1140. that.options.showHeader ? sprintf('<span class="title" %s>%s</span>', style,
  1141. getPropertyFromOther(that.options.columns, 'field', 'title', field)) : '',
  1142. sprintf('<span class="value">%s</span>', value),
  1143. '</div>'].join('') :
  1144. [sprintf('<td%s %s %s %s>', id_, class_, style, data_),
  1145. value,
  1146. '</td>'].join('');
  1147. if (column.filterControl !== undefined && column.filterControl.toLowerCase() === 'select'
  1148. && column.searchable) {
  1149. var selectControl = $('.' + column.field),
  1150. iOpt = 0,
  1151. exitsOpt = false,
  1152. options;
  1153. if (selectControl !== undefined) {
  1154. options = selectControl.get(0).options;
  1155. if (options.length === 0) {
  1156. //Added the default option
  1157. selectControl.append($("<option></option>")
  1158. .attr("value", '')
  1159. .text(''));
  1160. selectControl.append($("<option></option>")
  1161. .attr("value",value)
  1162. .text(value));
  1163. } else {
  1164. for (; iOpt < options.length; iOpt++ ) {
  1165. if (options[iOpt].value === value) {
  1166. exitsOpt = true;
  1167. break;
  1168. }
  1169. }
  1170. if (!exitsOpt) {
  1171. selectControl.append($("<option></option>")
  1172. .attr("value",value)
  1173. .text(value));
  1174. }
  1175. }
  1176. }
  1177. }
  1178. // Hide empty data on Card view when smartDisplay is set to true.
  1179. if (that.options.cardView && that.options.smartDisplay && value === '') {
  1180. text = '';
  1181. }
  1182. }
  1183. html.push(text);
  1184. });
  1185. if (this.options.cardView) {
  1186. html.push('</td>');
  1187. }
  1188. html.push('</tr>');
  1189. }
  1190. // show no records
  1191. if (!html.length) {
  1192. html.push('<tr class="no-records-found">',
  1193. sprintf('<td colspan="%s">%s</td>', this.header.fields.length, this.options.formatNoMatches()),
  1194. '</tr>');
  1195. }
  1196. this.$body.html(html.join(''));
  1197. if (!fixedScroll) {
  1198. this.scrollTo(0);
  1199. }
  1200. // click to select by column
  1201. this.$body.find('> tr > td').off('click').on('click', function () {
  1202. var $tr = $(this).parent();
  1203. that.trigger('click-row', that.data[$tr.data('index')], $tr);
  1204. // if click to select - then trigger the checkbox/radio click
  1205. if (that.options.clickToSelect) {
  1206. if (that.header.clickToSelects[$tr.children().index($(this))]) {
  1207. $tr.find(sprintf('[name="%s"]',
  1208. that.options.selectItemName))[0].click(); // #144: .trigger('click') bug
  1209. }
  1210. }
  1211. });
  1212. this.$body.find('tr').off('dblclick').on('dblclick', function () {
  1213. that.trigger('dbl-click-row', that.data[$(this).data('index')], $(this));
  1214. });
  1215. this.$selectItem = this.$body.find(sprintf('[name="%s"]', this.options.selectItemName));
  1216. this.$selectItem.off('click').on('click', function (event) {
  1217. event.stopImmediatePropagation();
  1218. var checked = $(this).prop('checked'),
  1219. row = that.data[$(this).data('index')];
  1220. row[that.header.stateField] = checked;
  1221. that.trigger(checked ? 'check' : 'uncheck', row);
  1222. if (that.options.singleSelect) {
  1223. that.$selectItem.not(this).each(function () {
  1224. that.data[$(this).data('index')][that.header.stateField] = false;
  1225. });
  1226. that.$selectItem.filter(':checked').not(this).prop('checked', false);
  1227. }
  1228. that.updateSelected();
  1229. });
  1230. $.each(this.header.events, function (i, events) {
  1231. if (!events) {
  1232. return;
  1233. }
  1234. // fix bug, if events is defined with namespace
  1235. if (typeof events === 'string') {
  1236. events = calculateObjectValue(null, events);
  1237. }
  1238. for (var key in events) {
  1239. that.$body.find('tr').each(function () {
  1240. var $tr = $(this),
  1241. $td = $tr.find(that.options.cardView ? '.card-view' : 'td').eq(i),
  1242. index = key.indexOf(' '),
  1243. name = key.substring(0, index),
  1244. el = key.substring(index + 1),
  1245. func = events[key];
  1246. $td.find(el).off(name).on(name, function (e) {
  1247. var index = $tr.data('index'),
  1248. row = that.data[index],
  1249. value = row[that.header.fields[i]];
  1250. func.apply(this, [e, value, row, index]);
  1251. });
  1252. });
  1253. }
  1254. });
  1255. this.updateSelected();
  1256. this.resetView();
  1257. this.trigger('post-body');
  1258. };
  1259. BootstrapTable.prototype.initServer = function (silent, query) {
  1260. var that = this,
  1261. data = {},
  1262. params = {
  1263. pageSize: this.options.pageSize === this.options.formatAllRows() ? this.options.totalRows : this.options.pageSize,
  1264. pageNumber: this.options.pageNumber,
  1265. searchText: this.searchText,
  1266. sortName: this.options.sortName,
  1267. sortOrder: this.options.sortOrder
  1268. };
  1269. if (!this.options.url) {
  1270. return;
  1271. }
  1272. if (this.options.queryParamsType === 'limit') {
  1273. params = {
  1274. search: params.searchText,
  1275. sort: params.sortName,
  1276. order: params.sortOrder
  1277. };
  1278. if (this.options.pagination) {
  1279. params.limit = this.options.pageSize === this.options.formatAllRows() ?
  1280. this.options.totalRows : this.options.pageSize;
  1281. params.offset = this.options.pageSize === this.options.formatAllRows() ?
  1282. 0 : this.options.pageSize * (this.options.pageNumber - 1);
  1283. }
  1284. }
  1285. if (!($.isEmptyObject(this.filterColumnsPartial))) {
  1286. params['filter'] = JSON.stringify(this.filterColumnsPartial, null);
  1287. }
  1288. data = calculateObjectValue(this.options, this.options.queryParams, [params], data);
  1289. $.extend(data, query || {});
  1290. // false to stop request
  1291. if (data === false) {
  1292. return;
  1293. }
  1294. if (!silent) {
  1295. this.$loading.show();
  1296. }
  1297. $.ajax($.extend({}, calculateObjectValue(null, this.options.ajaxOptions), {
  1298. type: this.options.method,
  1299. url: this.options.url,
  1300. data: this.options.contentType === 'application/json' && this.options.method === 'post' ?
  1301. JSON.stringify(data) : data,
  1302. cache: this.options.cache,
  1303. contentType: this.options.contentType,
  1304. dataType: this.options.dataType,
  1305. success: function (res) {
  1306. res = calculateObjectValue(that.options, that.options.responseHandler, [res], res);
  1307. that.load(res);
  1308. that.trigger('load-success', res);
  1309. },
  1310. error: function (res) {
  1311. that.trigger('load-error', res.status);
  1312. },
  1313. complete: function () {
  1314. if (!silent) {
  1315. that.$loading.hide();
  1316. }
  1317. }
  1318. }));
  1319. };
  1320. BootstrapTable.prototype.initKeyEvents = function () {
  1321. if (this.options.keyEvents) {
  1322. var that = this;
  1323. $(document).off('keypress').on('keypress', function (e) {
  1324. var $search = that.$toolbar.find('.search input'),
  1325. $refresh = that.$toolbar.find('button[name="refresh"]'),
  1326. $toggle= that.$toolbar.find('button[name="toggle"]'),
  1327. $paginationSwitch = that.$toolbar.find('button[name="paginationSwitch"]');
  1328. switch (e.keyCode) {
  1329. case 115://s
  1330. case 83://S
  1331. if (!that.options.search) {
  1332. return;
  1333. }
  1334. if(document.activeElement === $search.get(0)){
  1335. return true;
  1336. }
  1337. $search.focus();
  1338. return false;
  1339. case 114: //r
  1340. case 82: //R
  1341. if (!that.options.showRefresh) {
  1342. return;
  1343. }
  1344. if(document.activeElement === $search.get(0)){
  1345. return true;
  1346. }
  1347. $refresh.click();
  1348. return false;
  1349. case 116: //t
  1350. case 84: //T
  1351. if (!that.options.showToggle) {
  1352. return;
  1353. }
  1354. if(document.activeElement === $search.get(0)){
  1355. return true;
  1356. }
  1357. $toggle.click();
  1358. return false;
  1359. case 112: //p
  1360. case 80: //p
  1361. if (!that.options.showPaginationSwitch) {
  1362. return;
  1363. }
  1364. if(document.activeElement === $search.get(0)){
  1365. return true;
  1366. }
  1367. $paginationSwitch.click();
  1368. return false;
  1369. }
  1370. });
  1371. }
  1372. };
  1373. BootstrapTable.prototype.getCaretHtml = function () {
  1374. return ['<span class="order' + (this.options.sortOrder === 'desc' ? '' : ' dropup') + '">',
  1375. '<span class="caret" style="margin: 10px 5px;"></span>',
  1376. '</span>'].join('');
  1377. };
  1378. BootstrapTable.prototype.updateSelected = function () {
  1379. var checkAll = this.$selectItem.filter(':enabled').length ===
  1380. this.$selectItem.filter(':enabled').filter(':checked').length;
  1381. this.$selectAll.add(this.$selectAll_).prop('checked', checkAll);
  1382. this.$selectItem.each(function () {
  1383. $(this).parents('tr')[$(this).prop('checked') ? 'addClass' : 'removeClass']('selected');
  1384. });
  1385. };
  1386. BootstrapTable.prototype.updateRows = function (checked) {
  1387. var that = this;
  1388. this.$selectItem.each(function () {
  1389. that.data[$(this).data('index')][that.header.stateField] = checked;
  1390. });
  1391. };
  1392. BootstrapTable.prototype.resetRows = function () {
  1393. var that = this;
  1394. $.each(this.data, function (i, row) {
  1395. that.$selectAll.prop('checked', false);
  1396. that.$selectItem.prop('checked', false);
  1397. row[that.header.stateField] = false;
  1398. });
  1399. };
  1400. BootstrapTable.prototype.trigger = function (name) {
  1401. var args = Array.prototype.slice.call(arguments, 1);
  1402. name += '.bs.table';
  1403. this.options[BootstrapTable.EVENTS[name]].apply(this.options, args);
  1404. this.$el.trigger($.Event(name), args);
  1405. this.options.onAll(name, args);
  1406. this.$el.trigger($.Event('all.bs.table'), [name, args]);
  1407. };
  1408. BootstrapTable.prototype.resetHeader = function () {
  1409. this.$el.css('margin-top', -this.$header.height());
  1410. // fix #61: the hidden table reset header bug.
  1411. // fix bug: get $el.css('width') error sometime (height = 500)
  1412. clearTimeout(this.timeoutId_);
  1413. this.timeoutId_ = setTimeout($.proxy(this.fitHeader, this), this.$el.is(':hidden') ? 100 : 0);
  1414. return;
  1415. };
  1416. BootstrapTable.prototype.fitHeader = function () {
  1417. var that = this,
  1418. $fixedHeader,
  1419. $fixedBody,
  1420. scrollWidth;
  1421. if (that.$el.is(':hidden')) {
  1422. that.timeoutFooter_ = setTimeout($.proxy(that.fitHeader, that), 100);
  1423. return;
  1424. }
  1425. $fixedHeader = that.$container.find('.fixed-table-header'),
  1426. $fixedBody = that.$container.find('.fixed-table-body'),
  1427. scrollWidth = that.$el.width() > $fixedBody.width() ? getScrollBarWidth() : 0;
  1428. that.$header_ = that.$header.clone(true, true);
  1429. that.$selectAll_ = that.$header_.find('[name="btSelectAll"]');
  1430. $fixedHeader.css({
  1431. 'margin-right': scrollWidth
  1432. }).find('table').css('width', that.$el.css('width'))
  1433. .html('').attr('class', that.$el.attr('class'))
  1434. .append(that.$header_);
  1435. // fix bug: $.data() is not working as expected after $.append()
  1436. that.$header.find('th').each(function (i) {
  1437. that.$header_.find('th').eq(i).data($(this).data());
  1438. });
  1439. that.$body.find('tr:first-child:not(.no-records-found) > *').each(function (i) {
  1440. that.$header_.find('div.fht-cell').eq(i).width($(this).innerWidth());
  1441. });
  1442. // horizontal scroll event
  1443. // TODO: it's probably better improving the layout than binding to scroll event
  1444. $fixedBody.off('scroll').on('scroll', function () {
  1445. $fixedHeader.scrollLeft($(this).scrollLeft());
  1446. });
  1447. that.trigger('post-header');
  1448. };
  1449. BootstrapTable.prototype.resetFooter = function () {
  1450. var that = this,
  1451. data = that.getData(),
  1452. html = [];
  1453. if (!this.options.showFooter || this.options.cardView) { //do nothing
  1454. return;
  1455. }
  1456. $.each(this.options.columns, function (i, column) {
  1457. var falign = '', // footer align style
  1458. style = '',
  1459. class_ = sprintf(' class="%s"', column['class']);
  1460. if (!column.visible) {
  1461. return;
  1462. }
  1463. if (that.options.cardView && (!column.cardVisible)) {
  1464. return;
  1465. }
  1466. falign = sprintf('text-align: %s; ', column.falign ? column.falign : column.align);
  1467. style = sprintf('vertical-align: %s; ', column.valign);
  1468. html.push('<td', class_, sprintf(' style="%s"', falign + style), '>');
  1469. html.push(calculateObjectValue(column, column.footerFormatter, [data], '&nbsp;') || '&nbsp;');
  1470. html.push('</td>');
  1471. });
  1472. this.$footer.find('tr').html(html.join(''));
  1473. clearTimeout(this.timeoutFooter_);
  1474. this.timeoutFooter_ = setTimeout($.proxy(this.fitFooter, this),
  1475. this.$el.is(':hidden') ? 100: 0);
  1476. };
  1477. BootstrapTable.prototype.fitFooter = function () {
  1478. var that = this,
  1479. $fixedBody,
  1480. $footerTd,
  1481. elWidth,
  1482. scrollWidth;
  1483. clearTimeout(this.timeoutFooter_);
  1484. if (this.$el.is(':hidden')) {
  1485. this.timeoutFooter_ = setTimeout($.proxy(this.fitFooter, this), 100);
  1486. return;
  1487. }
  1488. $fixedBody = this.$container.find('.fixed-table-body');
  1489. elWidth = this.$el.css('width');
  1490. scrollWidth = elWidth > $fixedBody.width() ? getScrollBarWidth() : 0;
  1491. this.$footer.css({
  1492. 'margin-right': scrollWidth
  1493. }).find('table').css('width', elWidth)
  1494. .attr('class', this.$el.attr('class'));
  1495. $footerTd = this.$footer.find('td');
  1496. $fixedBody.find('tbody tr:first-child:not(.no-records-found) > td').each(function(i) {
  1497. $footerTd.eq(i).outerWidth($(this).outerWidth());
  1498. });
  1499. };
  1500. BootstrapTable.prototype.toggleColumn = function (index, checked, needUpdate) {
  1501. if (index === -1) {
  1502. return;
  1503. }
  1504. this.options.columns[index].visible = checked;
  1505. this.initHeader();
  1506. this.initSearch();
  1507. this.initPagination();
  1508. this.initBody();
  1509. if (this.options.showColumns) {
  1510. var $items = this.$toolbar.find('.keep-open input').prop('disabled', false);
  1511. if (needUpdate) {
  1512. $items.filter(sprintf('[value="%s"]', index)).prop('checked', checked);
  1513. }
  1514. if ($items.filter(':checked').length <= this.options.minimumCountColumns) {
  1515. $items.filter(':checked').prop('disabled', true);
  1516. }
  1517. }
  1518. };
  1519. BootstrapTable.prototype.toggleRow = function (index, isIdField, visible) {
  1520. if (index === -1) {
  1521. return;
  1522. }
  1523. $(this.$body[0]).children().filter(sprintf( isIdField ? '[value="%s"]' : '[data-index="%s"]', index))
  1524. [visible ? 'show' : 'hide']();
  1525. };
  1526. // PUBLIC FUNCTION DEFINITION
  1527. // =======================
  1528. BootstrapTable.prototype.resetView = function (params) {
  1529. var that = this,
  1530. padding = 0,
  1531. $tableContainer = that.$container.find('.fixed-table-container');
  1532. if (params && params.height) {
  1533. this.options.height = params.height;
  1534. }
  1535. this.$selectAll.prop('checked', this.$selectItem.length > 0 &&
  1536. this.$selectItem.length === this.$selectItem.filter(':checked').length);
  1537. if (this.options.height) {
  1538. var toolbarHeight = +this.$toolbar.children().outerHeight(true),
  1539. paginationHeight = +this.$pagination.children().outerHeight(true),
  1540. height = this.options.height - toolbarHeight - paginationHeight;
  1541. $tableContainer.css('height', height + 'px');
  1542. }
  1543. if (this.options.cardView) {
  1544. // remove the element css
  1545. that.$el.css('margin-top', '0');
  1546. $tableContainer.css('padding-bottom', '0');
  1547. return;
  1548. }
  1549. if (this.options.showHeader && this.options.height) {
  1550. this.$container.find('.fixed-table-header').show();
  1551. this.resetHeader();
  1552. padding += cellHeight;
  1553. } else {
  1554. this.$container.find('.fixed-table-header').hide();
  1555. this.trigger('post-header');
  1556. }
  1557. if (this.options.showFooter) {
  1558. this.resetFooter();
  1559. if (this.options.height) {
  1560. padding += cellHeight;
  1561. }
  1562. }
  1563. $tableContainer.css('padding-bottom', padding + 'px');
  1564. };
  1565. BootstrapTable.prototype.getData = function () {
  1566. return (this.searchText
  1567. || !$.isEmptyObject(this.filterColumns)
  1568. || !$.isEmptyObject(this.filterColumnsPartial)) ? this.data : this.options.data;
  1569. };
  1570. BootstrapTable.prototype.load = function (data) {
  1571. var fixedScroll = false;
  1572. // #431: support pagination
  1573. if (this.options.sidePagination === 'server') {
  1574. this.options.totalRows = data.total;
  1575. fixedScroll = data.fixedScroll;
  1576. data = data.rows;
  1577. } else if (!$.isArray(data)) { // support fixedScroll
  1578. fixedScroll = data.fixedScroll;
  1579. data = data.data;
  1580. }
  1581. this.initData(data);
  1582. this.initSearch();
  1583. this.initPagination();
  1584. this.initBody(fixedScroll);
  1585. };
  1586. BootstrapTable.prototype.append = function (data) {
  1587. this.initData(data, 'append');
  1588. this.initSearch();
  1589. this.initPagination();
  1590. this.initBody(true);
  1591. };
  1592. BootstrapTable.prototype.prepend = function (data) {
  1593. this.initData(data, 'prepend');
  1594. this.initSearch();
  1595. this.initPagination();
  1596. this.initBody(true);
  1597. };
  1598. BootstrapTable.prototype.remove = function (params) {
  1599. var len = this.options.data.length,
  1600. i, row;
  1601. if (!params.hasOwnProperty('field') || !params.hasOwnProperty('values')) {
  1602. return;
  1603. }
  1604. for (i = len - 1; i >= 0; i--) {
  1605. row = this.options.data[i];
  1606. if (!row.hasOwnProperty(params.field)) {
  1607. continue;
  1608. }
  1609. if ($.inArray(row[params.field], params.values) !== -1) {
  1610. this.options.data.splice(i, 1);
  1611. }
  1612. }
  1613. if (len === this.options.data.length) {
  1614. return;
  1615. }
  1616. this.initSearch();
  1617. this.initPagination();
  1618. this.initBody(true);
  1619. };
  1620. BootstrapTable.prototype.insertRow = function (params) {
  1621. if (!params.hasOwnProperty('index') || !params.hasOwnProperty('row')) {
  1622. return;
  1623. }
  1624. this.data.splice(params.index, 0, params.row);
  1625. this.initSearch();
  1626. this.initPagination();
  1627. this.initBody(true);
  1628. };
  1629. BootstrapTable.prototype.updateRow = function (params) {
  1630. if (!params.hasOwnProperty('index') || !params.hasOwnProperty('row')) {
  1631. return;
  1632. }
  1633. $.extend(this.data[params.index], params.row);
  1634. this.initBody(true);
  1635. };
  1636. BootstrapTable.prototype.showRow = function (params) {
  1637. if (!params.hasOwnProperty('index')) {
  1638. return;
  1639. }
  1640. this.toggleRow(params.index, params.isIdField === undefined ? false : true, true);
  1641. };
  1642. BootstrapTable.prototype.hideRow = function (params) {
  1643. if (!params.hasOwnProperty('index')) {
  1644. return;
  1645. }
  1646. this.toggleRow(params.index, params.isIdField === undefined ? false : true, false);
  1647. };
  1648. BootstrapTable.prototype.getRowsHidden = function (show) {
  1649. var rows = $(this.$body[0]).children().filter(':hidden'),
  1650. i = 0;
  1651. if (show) {
  1652. for (; i < rows.length; i++) {
  1653. $(rows[i]).show();
  1654. }
  1655. }
  1656. return rows;
  1657. }
  1658. BootstrapTable.prototype.mergeCells = function (options) {
  1659. var row = options.index,
  1660. col = $.inArray(options.field, this.header.fields),
  1661. rowspan = options.rowspan || 1,
  1662. colspan = options.colspan || 1,
  1663. i, j,
  1664. $tr = this.$body.find('tr'),
  1665. $td = $tr.eq(row).find('td').eq(col);
  1666. if (row < 0 || col < 0 || row >= this.data.length) {
  1667. return;
  1668. }
  1669. for (i = row; i < row + rowspan; i++) {
  1670. for (j = col; j < col + colspan; j++) {
  1671. $tr.eq(i).find('td').eq(j).hide();
  1672. }
  1673. }
  1674. $td.attr('rowspan', rowspan).attr('colspan', colspan).show();
  1675. };
  1676. BootstrapTable.prototype.getOptions = function () {
  1677. return this.options;
  1678. };
  1679. BootstrapTable.prototype.getSelections = function () {
  1680. var that = this;
  1681. return $.grep(this.data, function (row) {
  1682. return row[that.header.stateField];
  1683. });
  1684. };
  1685. BootstrapTable.prototype.checkAll = function () {
  1686. this.checkAll_(true);
  1687. };
  1688. BootstrapTable.prototype.uncheckAll = function () {
  1689. this.checkAll_(false);
  1690. };
  1691. BootstrapTable.prototype.checkAll_ = function (checked) {
  1692. var rows;
  1693. if (!checked) {
  1694. rows = this.getSelections();
  1695. }
  1696. this.$selectItem.filter(':enabled').prop('checked', checked);
  1697. this.updateRows(checked);
  1698. this.updateSelected();
  1699. if (checked) {
  1700. rows = this.getSelections();
  1701. }
  1702. this.trigger(checked ? 'check-all' : 'uncheck-all', rows);
  1703. };
  1704. BootstrapTable.prototype.check = function (index) {
  1705. this.check_(true, index);
  1706. };
  1707. BootstrapTable.prototype.uncheck = function (index) {
  1708. this.check_(false, index);
  1709. };
  1710. BootstrapTable.prototype.check_ = function (checked, index) {
  1711. this.$selectItem.filter(sprintf('[data-index="%s"]', index)).prop('checked', checked);
  1712. this.data[index][this.header.stateField] = checked;
  1713. this.updateSelected();
  1714. this.trigger(checked ? 'check' : 'uncheck', this.data[index]);
  1715. };
  1716. BootstrapTable.prototype.checkBy = function (obj) {
  1717. this.checkBy_(true, obj);
  1718. };
  1719. BootstrapTable.prototype.uncheckBy = function (obj) {
  1720. this.checkBy_(false, obj);
  1721. };
  1722. BootstrapTable.prototype.checkBy_ = function (checked, obj) {
  1723. if (!obj.hasOwnProperty('field') || !obj.hasOwnProperty('values')) {
  1724. return;
  1725. }
  1726. var that = this;
  1727. $.each(this.options.data, function (index, row) {
  1728. if (!row.hasOwnProperty(obj.field)) {
  1729. return false;
  1730. }
  1731. if ($.inArray(row[obj.field], obj.values) !== -1) {
  1732. that.$selectItem.filter(sprintf('[data-index="%s"]', index)).prop('checked', checked);
  1733. row[that.header.stateField] = checked;
  1734. that.trigger(checked ? 'check' : 'uncheck', row);
  1735. }
  1736. });
  1737. this.updateSelected();
  1738. };
  1739. BootstrapTable.prototype.destroy = function () {
  1740. this.$el.insertBefore(this.$container);
  1741. $(this.options.toolbar).insertBefore(this.$el);
  1742. this.$container.next().remove();
  1743. this.$container.remove();
  1744. this.$el.html(this.$el_.html())
  1745. .css('margin-top', '0')
  1746. .attr('class', this.$el_.attr('class') || ''); // reset the class
  1747. };
  1748. BootstrapTable.prototype.showLoading = function () {
  1749. this.$loading.show();
  1750. };
  1751. BootstrapTable.prototype.hideLoading = function () {
  1752. this.$loading.hide();
  1753. };
  1754. BootstrapTable.prototype.togglePagination = function () {
  1755. this.options.pagination = !this.options.pagination;
  1756. var button = this.$toolbar.find('button[name="paginationSwitch"] i');
  1757. if (this.options.pagination) {
  1758. button.attr("class", this.options.iconsPrefix + " " + this.options.icons.paginationSwitchDown);
  1759. } else {
  1760. button.attr("class", this.options.iconsPrefix + " " + this.options.icons.paginationSwitchUp);
  1761. }
  1762. this.updatePagination();
  1763. };
  1764. BootstrapTable.prototype.refresh = function (params) {
  1765. if (params && params.url) {
  1766. this.options.url = params.url;
  1767. this.options.pageNumber = 1;
  1768. }
  1769. this.initServer(params && params.silent, params && params.query);
  1770. };
  1771. BootstrapTable.prototype.showColumn = function (field) {
  1772. this.toggleColumn(getFieldIndex(this.options.columns, field), true, true);
  1773. };
  1774. BootstrapTable.prototype.hideColumn = function (field) {
  1775. this.toggleColumn(getFieldIndex(this.options.columns, field), false, true);
  1776. };
  1777. BootstrapTable.prototype.filterBy = function (columns) {
  1778. this.filterColumns = $.isEmptyObject(columns) ? {} : columns;
  1779. this.options.pageNumber = 1;
  1780. this.initSearch();
  1781. this.updatePagination();
  1782. };
  1783. BootstrapTable.prototype.scrollTo = function (value) {
  1784. var $tbody = this.$container.find('.fixed-table-body');
  1785. if (typeof value === 'string') {
  1786. value = value === 'bottom' ? $tbody[0].scrollHeight : 0;
  1787. }
  1788. if (typeof value === 'number') {
  1789. $tbody.scrollTop(value);
  1790. }
  1791. };
  1792. BootstrapTable.prototype.selectPage = function (page) {
  1793. if (page > 0 && page <= this.options.totalPages) {
  1794. this.options.pageNumber = page;
  1795. this.updatePagination();
  1796. }
  1797. };
  1798. BootstrapTable.prototype.prevPage = function () {
  1799. if (this.options.pageNumber > 1) {
  1800. this.options.pageNumber--;
  1801. this.updatePagination();
  1802. }
  1803. };
  1804. BootstrapTable.prototype.nextPage = function () {
  1805. if (this.options.pageNumber < this.options.totalPages) {
  1806. this.options.pageNumber++;
  1807. this.updatePagination();
  1808. }
  1809. };
  1810. BootstrapTable.prototype.toggleView = function () {
  1811. this.options.cardView = !this.options.cardView;
  1812. this.initHeader();
  1813. // Fixed remove toolbar when click cardView button.
  1814. //that.initToolbar();
  1815. this.initBody();
  1816. };
  1817. // BOOTSTRAP TABLE PLUGIN DEFINITION
  1818. // =======================
  1819. var allowedMethods = [
  1820. 'getOptions',
  1821. 'getSelections', 'getData',
  1822. 'load', 'append', 'prepend', 'remove',
  1823. 'insertRow', 'updateRow',
  1824. 'showRow', 'hideRow', 'getRowsHidden',
  1825. 'mergeCells',
  1826. 'checkAll', 'uncheckAll',
  1827. 'check', 'uncheck',
  1828. 'checkBy', 'uncheckBy',
  1829. 'refresh',
  1830. 'resetView',
  1831. 'destroy',
  1832. 'showLoading', 'hideLoading',
  1833. 'showColumn', 'hideColumn',
  1834. 'filterBy',
  1835. 'scrollTo',
  1836. 'selectPage', 'prevPage', 'nextPage',
  1837. 'togglePagination',
  1838. 'toggleView'
  1839. ];
  1840. $.fn.bootstrapTable = function (option, _relatedTarget) {
  1841. var value;
  1842. this.each(function () {
  1843. var $this = $(this),
  1844. data = $this.data('bootstrap.table'),
  1845. options = $.extend({}, BootstrapTable.DEFAULTS, $this.data(),
  1846. typeof option === 'object' && option);
  1847. if (typeof option === 'string') {
  1848. if ($.inArray(option, allowedMethods) < 0) {
  1849. throw "Unknown method: " + option;
  1850. }
  1851. if (!data) {
  1852. return;
  1853. }
  1854. value = data[option](_relatedTarget);
  1855. if (option === 'destroy') {
  1856. $this.removeData('bootstrap.table');
  1857. }
  1858. }
  1859. if (!data) {
  1860. $this.data('bootstrap.table', (data = new BootstrapTable(this, options)));
  1861. }
  1862. });
  1863. return typeof value === 'undefined' ? this : value;
  1864. };
  1865. $.fn.bootstrapTable.Constructor = BootstrapTable;
  1866. $.fn.bootstrapTable.defaults = BootstrapTable.DEFAULTS;
  1867. $.fn.bootstrapTable.columnDefaults = BootstrapTable.COLUMN_DEFAULTS;
  1868. $.fn.bootstrapTable.locales = BootstrapTable.LOCALES;
  1869. $.fn.bootstrapTable.methods = allowedMethods;
  1870. // BOOTSTRAP TABLE INIT
  1871. // =======================
  1872. $(function () {
  1873. $('[data-toggle="table"]').bootstrapTable();
  1874. });
  1875. }(jQuery);