bootstrap-table-multiple-sort.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. sortPriority: null,
  410. onMultipleSort () {
  411. return false
  412. }
  413. })
  414. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  415. 'multiple-sort.bs.table': 'onMultipleSort'
  416. })
  417. $.extend($.fn.bootstrapTable.locales, {
  418. formatMultipleSort () {
  419. return 'Multiple Sort'
  420. },
  421. formatAddLevel () {
  422. return 'Add Level'
  423. },
  424. formatDeleteLevel () {
  425. return 'Delete Level'
  426. },
  427. formatColumn () {
  428. return 'Column'
  429. },
  430. formatOrder () {
  431. return 'Order'
  432. },
  433. formatSortBy () {
  434. return 'Sort by'
  435. },
  436. formatThenBy () {
  437. return 'Then by'
  438. },
  439. formatSort () {
  440. return 'Sort'
  441. },
  442. formatCancel () {
  443. return 'Cancel'
  444. },
  445. formatDuplicateAlertTitle () {
  446. return 'Duplicate(s) detected!'
  447. },
  448. formatDuplicateAlertDescription () {
  449. return 'Please remove or change any duplicate column.'
  450. },
  451. formatSortOrders () {
  452. return {
  453. asc: 'Ascending',
  454. desc: 'Descending'
  455. }
  456. }
  457. })
  458. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales)
  459. const BootstrapTable = $.fn.bootstrapTable.Constructor
  460. const _initToolbar = BootstrapTable.prototype.initToolbar
  461. BootstrapTable.prototype.initToolbar = function (...args) {
  462. this.showToolbar = this.showToolbar || this.options.showMultiSort
  463. const that = this
  464. const sortModalSelector = `sortModal_${this.$el.attr('id')}`
  465. const sortModalId = `#${sortModalSelector}`
  466. this.$sortModal = $(sortModalId)
  467. this.sortModalSelector = sortModalSelector
  468. _initToolbar.apply(this, Array.prototype.slice.apply(args))
  469. if (that.options.sidePagination === 'server' && !isSingleSort && that.options.sortPriority !== null) {
  470. const t = that.options.queryParams
  471. that.options.queryParams = params => {
  472. params.multiSort = that.options.sortPriority
  473. return t(params)
  474. }
  475. }
  476. if (this.options.showMultiSort) {
  477. const $btnGroup = this.$toolbar.find('>.' + that.constants.classes.buttonsGroup.split(' ').join('.')).first()
  478. let $multiSortBtn = this.$toolbar.find('div.multi-sort')
  479. const o = that.options
  480. if (!$multiSortBtn.length && this.options.showMultiSortButton) {
  481. $multiSortBtn = Utils.sprintf(that.constants.html.multipleSortButton, that.sortModalSelector, this.options.formatMultipleSort(), Utils.sprintf(that.constants.html.icon, o.iconsPrefix, o.icons.sort))
  482. $btnGroup.append($multiSortBtn)
  483. if ($.fn.bootstrapTable.theme === 'semantic') {
  484. this.$toolbar.find('.multi-sort').on('click', () => {
  485. $(sortModalId).modal('show')
  486. })
  487. } else if ($.fn.bootstrapTable.theme === 'materialize') {
  488. this.$toolbar.find('.multi-sort').on('click', () => {
  489. $(sortModalId).modal()
  490. })
  491. } else if ($.fn.bootstrapTable.theme === 'foundation') {
  492. this.$toolbar.find('.multi-sort').on('click', () => {
  493. if (!this.foundationModal) {
  494. // eslint-disable-next-line no-undef
  495. this.foundationModal = new Foundation.Reveal($(sortModalId))
  496. }
  497. this.foundationModal.open()
  498. })
  499. } else if ($.fn.bootstrapTable.theme === 'bulma') {
  500. this.$toolbar.find('.multi-sort').on('click', () => {
  501. $('html').toggleClass('is-clipped')
  502. $(sortModalId).toggleClass('is-active')
  503. $('button[data-close]').one('click', () => {
  504. $('html').toggleClass('is-clipped')
  505. $(sortModalId).toggleClass('is-active')
  506. })
  507. })
  508. }
  509. showSortModal(that)
  510. }
  511. this.$el.on('sort.bs.table', () => {
  512. isSingleSort = true
  513. })
  514. this.$el.on('multiple-sort.bs.table', () => {
  515. isSingleSort = false
  516. })
  517. this.$el.on('load-success.bs.table', () => {
  518. if (!isSingleSort && that.options.sortPriority !== null && typeof that.options.sortPriority === 'object' && that.options.sidePagination !== 'server') {
  519. that.onMultipleSort()
  520. }
  521. })
  522. this.$el.on('column-switch.bs.table', (field, checked) => {
  523. for (let i = 0; i < that.options.sortPriority.length; i++) {
  524. if (that.options.sortPriority[i].sortName === checked) {
  525. that.options.sortPriority.splice(i, 1)
  526. }
  527. }
  528. that.assignSortableArrows()
  529. that.$sortModal.remove()
  530. showSortModal(that)
  531. })
  532. this.$el.on('reset-view.bs.table', () => {
  533. if (!isSingleSort && that.options.sortPriority !== null && typeof that.options.sortPriority === 'object') {
  534. that.assignSortableArrows()
  535. }
  536. })
  537. }
  538. }
  539. BootstrapTable.prototype.multipleSort = function () {
  540. const that = this
  541. if (!isSingleSort && that.options.sortPriority !== null && typeof that.options.sortPriority === 'object' && that.options.sidePagination !== 'server') {
  542. that.onMultipleSort()
  543. }
  544. }
  545. BootstrapTable.prototype.onMultipleSort = function () {
  546. const that = this
  547. const cmp = (x, y) => x > y ? 1 : x < y ? -1 : 0
  548. const arrayCmp = (a, b) => {
  549. const arr1 = []
  550. const arr2 = []
  551. for (let i = 0; i < that.options.sortPriority.length; i++) {
  552. const order = that.options.sortPriority[i].sortOrder === 'desc' ? -1 : 1
  553. let aa = a[that.options.sortPriority[i].sortName]
  554. let bb = b[that.options.sortPriority[i].sortName]
  555. if (aa === undefined || aa === null) {
  556. aa = ''
  557. }
  558. if (bb === undefined || bb === null) {
  559. bb = ''
  560. }
  561. if ($.isNumeric(aa) && $.isNumeric(bb)) {
  562. aa = parseFloat(aa)
  563. bb = parseFloat(bb)
  564. }
  565. if (typeof aa !== 'string') {
  566. aa = aa.toString()
  567. }
  568. arr1.push(
  569. order * cmp(aa, bb))
  570. arr2.push(
  571. order * cmp(bb, aa))
  572. }
  573. return cmp(arr1, arr2)
  574. }
  575. this.data.sort((a, b) => arrayCmp(a, b))
  576. this.initBody()
  577. this.assignSortableArrows()
  578. this.trigger('multiple-sort')
  579. }
  580. BootstrapTable.prototype.addLevel = function (index, sortPriority) {
  581. const text = index === 0 ? this.options.formatSortBy() : this.options.formatThenBy()
  582. this.$sortModal.find('tbody')
  583. .append($('<tr>')
  584. .append($('<td>').text(text))
  585. .append($('<td>').append($(Utils.sprintf(this.constants.html.multipleSortSelect, this.constants.classes.paginationDropdown, 'multi-sort-name'))))
  586. .append($('<td>').append($(Utils.sprintf(this.constants.html.multipleSortSelect, this.constants.classes.paginationDropdown, 'multi-sort-order'))))
  587. )
  588. const $multiSortName = this.$sortModal.find('.multi-sort-name').last()
  589. const $multiSortOrder = this.$sortModal.find('.multi-sort-order').last()
  590. $.each(this.columns, (i, column) => {
  591. if (column.sortable === false || column.visible === false) {
  592. return true
  593. }
  594. $multiSortName.append(`<option value="${column.field}">${column.title}</option>`)
  595. })
  596. $.each(this.options.formatSortOrders(), (value, order) => {
  597. $multiSortOrder.append(`<option value="${value}">${order}</option>`)
  598. })
  599. if (sortPriority !== undefined) {
  600. $multiSortName.find(`option[value="${sortPriority.sortName}"]`).attr('selected', true)
  601. $multiSortOrder.find(`option[value="${sortPriority.sortOrder}"]`).attr('selected', true)
  602. }
  603. }
  604. BootstrapTable.prototype.assignSortableArrows = function () {
  605. const that = this
  606. const headers = that.$header.find('th')
  607. for (let i = 0; i < headers.length; i++) {
  608. for (let c = 0; c < that.options.sortPriority.length; c++) {
  609. if ($(headers[i]).data('field') === that.options.sortPriority[c].sortName) {
  610. $(headers[i]).find('.sortable').removeClass('desc asc').addClass(that.options.sortPriority[c].sortOrder)
  611. }
  612. }
  613. }
  614. }
  615. BootstrapTable.prototype.setButtonStates = function () {
  616. const total = this.$sortModal.find('.multi-sort-name:first option').length
  617. const current = this.$sortModal.find('tbody tr').length
  618. if (current === total) {
  619. this.$sortModal.find('#add').attr('disabled', 'disabled')
  620. }
  621. if (current > 1) {
  622. this.$sortModal.find('#delete').removeAttr('disabled')
  623. }
  624. if (current < total) {
  625. this.$sortModal.find('#add').removeAttr('disabled')
  626. }
  627. if (current === 1) {
  628. this.$sortModal.find('#delete').attr('disabled', 'disabled')
  629. }
  630. }