bootstrap-table-multiple-sort.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /**
  2. * @author Nadim Basalamah <dimbslmh@gmail.com>
  3. * @version: v1.1.0
  4. * https://github.com/dimbslmh/bootstrap-table/tree/master/src/extensions/multiple-sort/bootstrap-table-multiple-sort.js
  5. * Modification: ErwannNevou <https://github.com/ErwannNevou>
  6. */
  7. let isSingleSort = false
  8. const Utils = $.fn.bootstrapTable.utils
  9. const bootstrap = {
  10. bootstrap3: {
  11. icons: {
  12. plus: 'glyphicon-plus',
  13. minus: 'glyphicon-minus',
  14. sort: 'glyphicon-sort'
  15. },
  16. html: {
  17. multipleSortModal: `
  18. <div class="modal fade" id="%s" tabindex="-1" role="dialog" aria-labelledby="%sLabel" aria-hidden="true">
  19. <div class="modal-dialog">
  20. <div class="modal-content">
  21. <div class="modal-header">
  22. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  23. <h4 class="modal-title" id="%sLabel">%s</h4>
  24. </div>
  25. <div class="modal-body">
  26. <div class="bootstrap-table">
  27. <div class="fixed-table-toolbar">
  28. <div class="bars">
  29. <div id="toolbar">
  30. <button id="add" type="button" class="btn btn-default">%s %s</button>
  31. <button id="delete" type="button" class="btn btn-default" disabled>%s %s</button>
  32. </div>
  33. </div>
  34. </div>
  35. <div class="fixed-table-container">
  36. <table id="multi-sort" class="table">
  37. <thead>
  38. <tr>
  39. <th></th>
  40. <th><div class="th-inner">%s</div></th>
  41. <th><div class="th-inner">%s</div></th>
  42. </tr>
  43. </thead>
  44. <tbody></tbody>
  45. </table>
  46. </div>
  47. </div>
  48. </div>
  49. <div class="modal-footer">
  50. <button type="button" class="btn btn-default" data-dismiss="modal">%s</button>
  51. <button type="button" class="btn btn-primary multi-sort-order-button">%s</button>
  52. </div>
  53. </div>
  54. </div>
  55. </div>
  56. `,
  57. multipleSortButton: '<button class="multi-sort btn btn-default" type="button" data-toggle="modal" data-target="#%s" title="%s">%s</button>',
  58. multipleSortSelect: '<select class="%s %s form-control">'
  59. }
  60. },
  61. bootstrap4: {
  62. icons: {
  63. 'plus': 'fa-plus',
  64. 'minus': 'fa-minus',
  65. 'sort': 'fa-sort'
  66. },
  67. html: {
  68. multipleSortModal: `
  69. <div class="modal fade" id="%s" tabindex="-1" role="dialog" aria-labelledby="%sLabel" aria-hidden="true">
  70. <div class="modal-dialog" role="document">
  71. <div class="modal-content">
  72. <div class="modal-header">
  73. <h5 class="modal-title" id="%sLabel">%s</h5>
  74. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  75. <span aria-hidden="true">&times;</span>
  76. </button>
  77. </div>
  78. <div class="modal-body">
  79. <div class="bootstrap-table">
  80. <div class="fixed-table-toolbar">
  81. <div class="bars">
  82. <div id="toolbar" class="pb-3">
  83. <button id="add" type="button" class="btn btn-secondary">%s %s</button>
  84. <button id="delete" type="button" class="btn btn-secondary" disabled>%s %s</button>
  85. </div>
  86. </div>
  87. </div>
  88. <div class="fixed-table-container">
  89. <table id="multi-sort" class="table">
  90. <thead>
  91. <tr>
  92. <th></th>
  93. <th><div class="th-inner">%s</div></th>
  94. <th><div class="th-inner">%s</div></th>
  95. </tr>
  96. </thead>
  97. <tbody></tbody>
  98. </table>
  99. </div>
  100. </div>
  101. </div>
  102. <div class="modal-footer">
  103. <button type="button" class="btn btn-secondary" data-dismiss="modal">%s</button>
  104. <button type="button" class="btn btn-primary multi-sort-order-button">%s</button>
  105. </div>
  106. </div>
  107. </div>
  108. </div>
  109. `,
  110. multipleSortButton: '<button class="multi-sort btn btn-secondary" type="button" data-toggle="modal" data-target="#%s" title="%s">%s</button>',
  111. multipleSortSelect: '<select class="%s %s form-control">'
  112. }
  113. },
  114. semantic: {
  115. icons: {
  116. 'plus': 'fa-plus',
  117. 'minus': 'fa-minus',
  118. 'sort': 'fa-sort'
  119. },
  120. html: {
  121. multipleSortModal: `
  122. <div class="ui modal tiny" id="%s" aria-labelledby="%sLabel" aria-hidden="true">
  123. <i class="close icon"></i>
  124. <div class="header" id="%sLabel">
  125. %s
  126. </div>
  127. <div class="image content">
  128. <div class="bootstrap-table">
  129. <div class="fixed-table-toolbar">
  130. <div class="bars">
  131. <div id="toolbar" class="pb-3">
  132. <button id="add" type="button" class="ui button">%s %s</button>
  133. <button id="delete" type="button" class="ui button" disabled>%s %s</button>
  134. </div>
  135. </div>
  136. </div>
  137. <div class="fixed-table-container">
  138. <table id="multi-sort" class="table">
  139. <thead>
  140. <tr>
  141. <th></th>
  142. <th><div class="th-inner">%s</div></th>
  143. <th><div class="th-inner">%s</div></th>
  144. </tr>
  145. </thead>
  146. <tbody></tbody>
  147. </table>
  148. </div>
  149. </div>
  150. </div>
  151. <div class="actions">
  152. <div class="ui button deny">%s</div>
  153. <div class="ui button approve multi-sort-order-button">%s</div>
  154. </div>
  155. </div>
  156. `,
  157. multipleSortButton: '<button class="multi-sort ui button" type="button" data-toggle="modal" data-target="#%s" title="%s">%s</button>',
  158. multipleSortSelect: '<select class="%s %s">'
  159. }
  160. },
  161. materialize: {
  162. icons: {
  163. 'plus': 'plus',
  164. 'minus': 'minus',
  165. 'sort': 'sort'
  166. },
  167. html: {
  168. multipleSortModal: `
  169. <div id="%s" class="modal" aria-labelledby="%sLabel" aria-hidden="true">
  170. <div class="modal-content" id="%sLabel">
  171. <h4>%s</h4>
  172. <div class="bootstrap-table">
  173. <div class="fixed-table-toolbar">
  174. <div class="bars">
  175. <div id="toolbar" class="pb-3">
  176. <button id="add" type="button" class="waves-effect waves-light btn">%s %s</button>
  177. <button id="delete" type="button" class="waves-effect waves-light btn" disabled>%s %s</button>
  178. </div>
  179. </div>
  180. </div>
  181. <div class="fixed-table-container">
  182. <table id="multi-sort" class="table">
  183. <thead>
  184. <tr>
  185. <th></th>
  186. <th><div class="th-inner">%s</div></th>
  187. <th><div class="th-inner">%s</div></th>
  188. </tr>
  189. </thead>
  190. <tbody></tbody>
  191. </table>
  192. </div>
  193. </div>
  194. <div class="modal-footer">
  195. <a href="javascript:void(0)" class="modal-close waves-effect waves-light btn">%s</a>
  196. <a href="javascript:void(0)" class="modal-close waves-effect waves-light btn multi-sort-order-button">%s</a>
  197. </div>
  198. </div>
  199. </div>
  200. `,
  201. multipleSortButton: '<a href="#%s" class="multi-sort waves-effect waves-light btn modal-trigger" type="button" data-toggle="modal" title="%s">%s</a>',
  202. multipleSortSelect: '<select class="%s %s browser-default">'
  203. }
  204. },
  205. foundation: {
  206. icons: {
  207. 'plus': 'fa-plus',
  208. 'minus': 'fa-minus',
  209. 'sort': 'fa-sort'
  210. },
  211. html: {
  212. multipleSortModal: `
  213. <div class="reveal" id="%s" data-reveal aria-labelledby="%sLabel" aria-hidden="true">
  214. <div id="%sLabel">
  215. <h1>%s</h1>
  216. <div class="bootstrap-table">
  217. <div class="fixed-table-toolbar">
  218. <div class="bars">
  219. <div id="toolbar" class="padding-bottom-2">
  220. <button id="add" type="button" class="waves-effect waves-light button">%s %s</button>
  221. <button id="delete" type="button" class="waves-effect waves-light button" disabled>%s %s</button>
  222. </div>
  223. </div>
  224. </div>
  225. <div class="fixed-table-container">
  226. <table id="multi-sort" class="table">
  227. <thead>
  228. <tr>
  229. <th></th>
  230. <th><div class="th-inner">%s</div></th>
  231. <th><div class="th-inner">%s</div></th>
  232. </tr>
  233. </thead>
  234. <tbody></tbody>
  235. </table>
  236. </div>
  237. </div>
  238. <button class="waves-effect waves-light button" data-close aria-label="Close modal" type="button">
  239. <span aria-hidden="true">%s</span>
  240. </button>
  241. <button class="waves-effect waves-light button multi-sort-order-button" data-close aria-label="Order" type="button">
  242. <span aria-hidden="true">%s</span>
  243. </button>
  244. </div>
  245. </div>
  246. `,
  247. multipleSortButton: '<button class="button multi-sort" data-open="%s" title="%s">%s</button>',
  248. multipleSortSelect: '<select class="%s %s browser-default">'
  249. }
  250. },
  251. bulma: {
  252. icons: {
  253. 'plus': 'fa-plus',
  254. 'minus': 'fa-minus',
  255. 'sort': 'fa-sort'
  256. },
  257. html: {
  258. multipleSortModal: `
  259. <div class="modal" id="%s" aria-labelledby="%sLabel" aria-hidden="true">
  260. <div class="modal-background"></div>
  261. <div class="modal-content" id="%sLabel">
  262. <div class="box">
  263. <h2>%s</h2>
  264. <div class="bootstrap-table">
  265. <div class="fixed-table-toolbar">
  266. <div class="bars">
  267. <div id="toolbar" class="padding-bottom-2">
  268. <button id="add" type="button" class="waves-effect waves-light button">%s %s</button>
  269. <button id="delete" type="button" class="waves-effect waves-light button" disabled>%s %s</button>
  270. </div>
  271. </div>
  272. </div>
  273. <div class="fixed-table-container">
  274. <table id="multi-sort" class="table">
  275. <thead>
  276. <tr>
  277. <th></th>
  278. <th><div class="th-inner">%s</div></th>
  279. <th><div class="th-inner">%s</div></th>
  280. </tr>
  281. </thead>
  282. <tbody></tbody>
  283. </table>
  284. </div>
  285. </div>
  286. <button type="button" class="waves-effect waves-light button" data-close>%s</button>
  287. <button type="button" class="waves-effect waves-light button multi-sort-order-button" data-close>%s</button>
  288. </div>
  289. </div>
  290. </div>
  291. `,
  292. multipleSortButton: '<button class="button multi-sort" data-target="%s" title="%s">%s</button>',
  293. multipleSortSelect: '<select class="%s %s browser-default">'
  294. }
  295. }
  296. }[$.fn.bootstrapTable.theme]
  297. $.extend($.fn.bootstrapTable.defaults.icons, bootstrap.icons)
  298. $.extend($.fn.bootstrapTable.defaults.html, bootstrap.html)
  299. const showSortModal = that => {
  300. const _selector = that.sortModalSelector
  301. const _id = `#${_selector}`
  302. const o = that.options
  303. if (!$(_id).hasClass('modal')) {
  304. const sModal = Utils.sprintf(
  305. that.constants.html.multipleSortModal,
  306. _selector, _selector, _selector,
  307. that.options.formatMultipleSort(),
  308. Utils.sprintf(that.constants.html.icon, o.iconsPrefix, that.constants.icons.plus),
  309. that.options.formatAddLevel(),
  310. Utils.sprintf(that.constants.html.icon, o.iconsPrefix, that.constants.icons.minus),
  311. that.options.formatDeleteLevel(),
  312. that.options.formatColumn(),
  313. that.options.formatOrder(),
  314. that.options.formatCancel(),
  315. that.options.formatSort()
  316. )
  317. $('body').append($(sModal))
  318. that.$sortModal = $(_id)
  319. const $rows = that.$sortModal.find('tbody > tr')
  320. that.$sortModal.off('click', '#add').on('click', '#add', () => {
  321. const total = that.$sortModal.find('.multi-sort-name:first option').length
  322. let current = that.$sortModal.find('tbody tr').length
  323. if (current < total) {
  324. current++
  325. that.addLevel()
  326. that.setButtonStates()
  327. }
  328. })
  329. that.$sortModal.off('click', '#delete').on('click', '#delete', () => {
  330. const total = that.$sortModal.find('.multi-sort-name:first option').length
  331. let current = that.$sortModal.find('tbody tr').length
  332. if (current > 1 && current <= total) {
  333. current--
  334. that.$sortModal.find('tbody tr:last').remove()
  335. that.setButtonStates()
  336. }
  337. })
  338. that.$sortModal.off('click', '.multi-sort-order-button').on('click', '.multi-sort-order-button', () => {
  339. const $rows = that.$sortModal.find('tbody > tr')
  340. let $alert = that.$sortModal.find('div.alert')
  341. const fields = []
  342. const results = []
  343. that.options.sortPriority = $.map($rows, row => {
  344. const $row = $(row)
  345. const name = $row.find('.multi-sort-name').val()
  346. const order = $row.find('.multi-sort-order').val()
  347. fields.push(name)
  348. return {
  349. sortName: name,
  350. sortOrder: order
  351. }
  352. })
  353. const sorted_fields = fields.sort()
  354. for (let i = 0; i < fields.length - 1; i++) {
  355. if (sorted_fields[i + 1] === sorted_fields[i]) {
  356. results.push(sorted_fields[i])
  357. }
  358. }
  359. if (results.length > 0) {
  360. if ($alert.length === 0) {
  361. $alert = `<div class="alert alert-danger" role="alert"><strong>${that.options.formatDuplicateAlertTitle()}</strong> ${that.options.formatDuplicateAlertDescription()}</div>`
  362. $($alert).insertBefore(that.$sortModal.find('.bars'))
  363. }
  364. } else {
  365. if ($alert.length === 1) {
  366. $($alert).remove()
  367. }
  368. if ($.inArray($.fn.bootstrapTable.theme, ['bootstrap3', 'bootstrap4']) !== -1) {
  369. that.$sortModal.modal('hide')
  370. }
  371. that.options.sortName = ''
  372. if (that.options.sidePagination === 'server') {
  373. const t = that.options.queryParams
  374. that.options.queryParams = params => {
  375. params.multiSort = that.options.sortPriority
  376. return $.fn.bootstrapTable.utils.calculateObjectValue(that.options, t, [params])
  377. }
  378. isSingleSort = false
  379. that.initServer(that.options.silentSort)
  380. return
  381. }
  382. that.onMultipleSort()
  383. }
  384. })
  385. if (that.options.sortPriority === null || that.options.sortPriority.length === 0) {
  386. if (that.options.sortName) {
  387. that.options.sortPriority = [{
  388. sortName: that.options.sortName,
  389. sortOrder: that.options.sortOrder
  390. }]
  391. }
  392. }
  393. if (that.options.sortPriority !== null && that.options.sortPriority.length > 0) {
  394. if ($rows.length < that.options.sortPriority.length && typeof that.options.sortPriority === 'object') {
  395. for (let i = 0; i < that.options.sortPriority.length; i++) {
  396. that.addLevel(i, that.options.sortPriority[i])
  397. }
  398. }
  399. } else {
  400. that.addLevel(0)
  401. }
  402. that.setButtonStates()
  403. }
  404. }
  405. $.fn.bootstrapTable.methods.push('multipleSort')
  406. $.extend($.fn.bootstrapTable.defaults, {
  407. showMultiSort: false,
  408. showMultiSortButton: true,
  409. multiSortStrictSort: false,
  410. sortPriority: null,
  411. onMultipleSort () {
  412. return false
  413. }
  414. })
  415. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  416. 'multiple-sort.bs.table': 'onMultipleSort'
  417. })
  418. $.extend($.fn.bootstrapTable.locales, {
  419. formatMultipleSort () {
  420. return 'Multiple Sort'
  421. },
  422. formatAddLevel () {
  423. return 'Add Level'
  424. },
  425. formatDeleteLevel () {
  426. return 'Delete Level'
  427. },
  428. formatColumn () {
  429. return 'Column'
  430. },
  431. formatOrder () {
  432. return 'Order'
  433. },
  434. formatSortBy () {
  435. return 'Sort by'
  436. },
  437. formatThenBy () {
  438. return 'Then by'
  439. },
  440. formatSort () {
  441. return 'Sort'
  442. },
  443. formatCancel () {
  444. return 'Cancel'
  445. },
  446. formatDuplicateAlertTitle () {
  447. return 'Duplicate(s) detected!'
  448. },
  449. formatDuplicateAlertDescription () {
  450. return 'Please remove or change any duplicate column.'
  451. },
  452. formatSortOrders () {
  453. return {
  454. asc: 'Ascending',
  455. desc: 'Descending'
  456. }
  457. }
  458. })
  459. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales)
  460. const BootstrapTable = $.fn.bootstrapTable.Constructor
  461. const _initToolbar = BootstrapTable.prototype.initToolbar
  462. const _destroy = BootstrapTable.prototype.destroy
  463. BootstrapTable.prototype.initToolbar = function (...args) {
  464. this.showToolbar = this.showToolbar || this.options.showMultiSort
  465. const that = this
  466. const sortModalSelector = `sortModal_${this.$el.attr('id')}`
  467. const sortModalId = `#${sortModalSelector}`
  468. this.$sortModal = $(sortModalId)
  469. this.sortModalSelector = sortModalSelector
  470. if (that.options.sortPriority !== null) {
  471. that.onMultipleSort()
  472. }
  473. _initToolbar.apply(this, Array.prototype.slice.apply(args))
  474. if (that.options.sidePagination === 'server' && !isSingleSort && that.options.sortPriority !== null) {
  475. const t = that.options.queryParams
  476. that.options.queryParams = params => {
  477. params.multiSort = that.options.sortPriority
  478. return t(params)
  479. }
  480. }
  481. if (this.options.showMultiSort) {
  482. const $btnGroup = this.$toolbar.find('>.' + that.constants.classes.buttonsGroup.split(' ').join('.')).first()
  483. let $multiSortBtn = this.$toolbar.find('div.multi-sort')
  484. const o = that.options
  485. if (!$multiSortBtn.length && this.options.showMultiSortButton) {
  486. $multiSortBtn = Utils.sprintf(that.constants.html.multipleSortButton, that.sortModalSelector, this.options.formatMultipleSort(), Utils.sprintf(that.constants.html.icon, o.iconsPrefix, o.icons.sort))
  487. $btnGroup.append($multiSortBtn)
  488. if ($.fn.bootstrapTable.theme === 'semantic') {
  489. this.$toolbar.find('.multi-sort').on('click', () => {
  490. $(sortModalId).modal('show')
  491. })
  492. } else if ($.fn.bootstrapTable.theme === 'materialize') {
  493. this.$toolbar.find('.multi-sort').on('click', () => {
  494. $(sortModalId).modal()
  495. })
  496. } else if ($.fn.bootstrapTable.theme === 'foundation') {
  497. this.$toolbar.find('.multi-sort').on('click', () => {
  498. if (!this.foundationModal) {
  499. // eslint-disable-next-line no-undef
  500. this.foundationModal = new Foundation.Reveal($(sortModalId))
  501. }
  502. this.foundationModal.open()
  503. })
  504. } else if ($.fn.bootstrapTable.theme === 'bulma') {
  505. this.$toolbar.find('.multi-sort').on('click', () => {
  506. $('html').toggleClass('is-clipped')
  507. $(sortModalId).toggleClass('is-active')
  508. $('button[data-close]').one('click', () => {
  509. $('html').toggleClass('is-clipped')
  510. $(sortModalId).toggleClass('is-active')
  511. })
  512. })
  513. }
  514. showSortModal(that)
  515. }
  516. this.$el.on('sort.bs.table', () => {
  517. isSingleSort = true
  518. })
  519. this.$el.on('multiple-sort.bs.table', () => {
  520. isSingleSort = false
  521. })
  522. this.$el.on('load-success.bs.table', () => {
  523. if (!isSingleSort && that.options.sortPriority !== null && typeof that.options.sortPriority === 'object' && that.options.sidePagination !== 'server') {
  524. that.onMultipleSort()
  525. }
  526. })
  527. this.$el.on('column-switch.bs.table', (field, checked) => {
  528. for (let i = 0; i < that.options.sortPriority.length; i++) {
  529. if (that.options.sortPriority[i].sortName === checked) {
  530. that.options.sortPriority.splice(i, 1)
  531. }
  532. }
  533. that.assignSortableArrows()
  534. that.$sortModal.remove()
  535. showSortModal(that)
  536. })
  537. this.$el.on('reset-view.bs.table', () => {
  538. if (!isSingleSort && that.options.sortPriority !== null && typeof that.options.sortPriority === 'object') {
  539. that.assignSortableArrows()
  540. }
  541. })
  542. }
  543. }
  544. BootstrapTable.prototype.destroy = function (...args) {
  545. _destroy.apply(this, Array.prototype.slice.apply(args))
  546. if (this.options.showMultiSort) {
  547. this.$sortModal.remove()
  548. }
  549. }
  550. BootstrapTable.prototype.multipleSort = function () {
  551. const that = this
  552. if (!isSingleSort && that.options.sortPriority !== null && typeof that.options.sortPriority === 'object' && that.options.sidePagination !== 'server') {
  553. that.onMultipleSort()
  554. }
  555. }
  556. BootstrapTable.prototype.onMultipleSort = function () {
  557. const that = this
  558. const cmp = (x, y) => x > y ? 1 : x < y ? -1 : 0
  559. const arrayCmp = (a, b) => {
  560. const arr1 = []
  561. const arr2 = []
  562. for (let i = 0; i < that.options.sortPriority.length; i++) {
  563. const order = that.options.sortPriority[i].sortOrder === 'desc' ? -1 : 1
  564. let aa = a[that.options.sortPriority[i].sortName]
  565. let bb = b[that.options.sortPriority[i].sortName]
  566. if (aa === undefined || aa === null) {
  567. aa = ''
  568. }
  569. if (bb === undefined || bb === null) {
  570. bb = ''
  571. }
  572. if ($.isNumeric(aa) && $.isNumeric(bb)) {
  573. aa = parseFloat(aa)
  574. bb = parseFloat(bb)
  575. } else {
  576. aa = aa.toString()
  577. bb = bb.toString()
  578. if (that.options.multiSortStrictSort) {
  579. aa = aa.toLowerCase()
  580. bb = bb.toLowerCase()
  581. }
  582. }
  583. arr1.push(
  584. order * cmp(aa, bb))
  585. arr2.push(
  586. order * cmp(bb, aa))
  587. }
  588. return cmp(arr1, arr2)
  589. }
  590. this.data.sort((a, b) => arrayCmp(a, b))
  591. this.initBody()
  592. this.assignSortableArrows()
  593. this.trigger('multiple-sort')
  594. }
  595. BootstrapTable.prototype.addLevel = function (index, sortPriority) {
  596. const text = index === 0 ? this.options.formatSortBy() : this.options.formatThenBy()
  597. this.$sortModal.find('tbody')
  598. .append($('<tr>')
  599. .append($('<td>').text(text))
  600. .append($('<td>').append($(Utils.sprintf(this.constants.html.multipleSortSelect, this.constants.classes.paginationDropdown, 'multi-sort-name'))))
  601. .append($('<td>').append($(Utils.sprintf(this.constants.html.multipleSortSelect, this.constants.classes.paginationDropdown, 'multi-sort-order'))))
  602. )
  603. const $multiSortName = this.$sortModal.find('.multi-sort-name').last()
  604. const $multiSortOrder = this.$sortModal.find('.multi-sort-order').last()
  605. $.each(this.columns, (i, column) => {
  606. if (column.sortable === false || column.visible === false) {
  607. return true
  608. }
  609. $multiSortName.append(`<option value="${column.field}">${column.title}</option>`)
  610. })
  611. $.each(this.options.formatSortOrders(), (value, order) => {
  612. $multiSortOrder.append(`<option value="${value}">${order}</option>`)
  613. })
  614. if (sortPriority !== undefined) {
  615. $multiSortName.find(`option[value="${sortPriority.sortName}"]`).attr('selected', true)
  616. $multiSortOrder.find(`option[value="${sortPriority.sortOrder}"]`).attr('selected', true)
  617. }
  618. }
  619. BootstrapTable.prototype.assignSortableArrows = function () {
  620. const that = this
  621. const headers = that.$header.find('th')
  622. for (let i = 0; i < headers.length; i++) {
  623. for (let c = 0; c < that.options.sortPriority.length; c++) {
  624. if ($(headers[i]).data('field') === that.options.sortPriority[c].sortName) {
  625. $(headers[i]).find('.sortable').removeClass('desc asc').addClass(that.options.sortPriority[c].sortOrder)
  626. }
  627. }
  628. }
  629. }
  630. BootstrapTable.prototype.setButtonStates = function () {
  631. const total = this.$sortModal.find('.multi-sort-name:first option').length
  632. const current = this.$sortModal.find('tbody tr').length
  633. if (current === total) {
  634. this.$sortModal.find('#add').attr('disabled', 'disabled')
  635. }
  636. if (current > 1) {
  637. this.$sortModal.find('#delete').removeAttr('disabled')
  638. }
  639. if (current < total) {
  640. this.$sortModal.find('#add').removeAttr('disabled')
  641. }
  642. if (current === 1) {
  643. this.$sortModal.find('#delete').attr('disabled', 'disabled')
  644. }
  645. }