bootstrap-table.js 77 KB

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