bootstrap-table.js 89 KB

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