bootstrap-table.js 76 KB

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