bootstrap-table.js 98 KB

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