bootstrap-table-reorder-rows.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @author: Dennis Hernández
  3. * @webSite: http://djhvscf.github.io/Blog
  4. * @update zhixin wen <wenzhixin2010@gmail.com>
  5. */
  6. const rowAttr = (row, index) => ({
  7. id: `customId_${index}`
  8. })
  9. $.extend($.fn.bootstrapTable.defaults, {
  10. reorderableRows: false,
  11. onDragStyle: null,
  12. onDropStyle: null,
  13. onDragClass: 'reorder_rows_onDragClass',
  14. dragHandle: '>tbody>tr>td',
  15. useRowAttrFunc: false,
  16. // eslint-disable-next-line no-unused-vars
  17. onReorderRowsDrag (row) {
  18. return false
  19. },
  20. // eslint-disable-next-line no-unused-vars
  21. onReorderRowsDrop (row) {
  22. return false
  23. },
  24. // eslint-disable-next-line no-unused-vars
  25. onReorderRow (newData) {
  26. return false
  27. }
  28. })
  29. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  30. 'reorder-row.bs.table': 'onReorderRow'
  31. })
  32. $.BootstrapTable = class extends $.BootstrapTable {
  33. init (...args) {
  34. if (!this.options.reorderableRows) {
  35. super.init(...args)
  36. return
  37. }
  38. if (this.options.useRowAttrFunc) {
  39. this.options.rowAttributes = rowAttr
  40. }
  41. const onPostBody = this.options.onPostBody
  42. this.options.onPostBody = () => {
  43. setTimeout(() => {
  44. this.makeRowsReorderable()
  45. onPostBody.call(this.options, this.options.data)
  46. }, 1)
  47. }
  48. super.init(...args)
  49. }
  50. makeRowsReorderable () {
  51. this.$el.tableDnD({
  52. onDragStyle: this.options.onDragStyle,
  53. onDropStyle: this.options.onDropStyle,
  54. onDragClass: this.options.onDragClass,
  55. onDragStart: (table, droppedRow) => this.onDropStart(table, droppedRow),
  56. onDrop: (table, droppedRow) => this.onDrop(table, droppedRow),
  57. dragHandle: this.options.dragHandle
  58. })
  59. }
  60. onDropStart (table, draggingTd) {
  61. this.$draggingTd = $(draggingTd).css('cursor', 'move')
  62. this.draggingIndex = $(this.$draggingTd.parent()).data('index')
  63. // Call the user defined function
  64. this.options.onReorderRowsDrag(this.data[this.draggingIndex])
  65. }
  66. onDrop (table) {
  67. this.$draggingTd.css('cursor', '')
  68. const newData = []
  69. for (let i = 0; i < table.tBodies[0].rows.length; i++) {
  70. const $tr = $(table.tBodies[0].rows[i])
  71. newData.push(this.data[$tr.data('index')])
  72. $tr.data('index', i)
  73. }
  74. const draggingRow = this.data[this.draggingIndex]
  75. const droppedIndex = newData.indexOf(this.data[this.draggingIndex])
  76. const droppedRow = this.data[droppedIndex]
  77. const index = this.options.data.indexOf(this.data[droppedIndex])
  78. this.options.data.splice(this.options.data.indexOf(draggingRow), 1)
  79. this.options.data.splice(index, 0, draggingRow)
  80. this.initSearch()
  81. // Call the user defined function
  82. this.options.onReorderRowsDrop(droppedRow)
  83. // Call the event reorder-row
  84. this.trigger('reorder-row', newData, draggingRow, droppedRow)
  85. }
  86. initSearch () {
  87. this.ignoreInitSort = true
  88. super.initSearch()
  89. }
  90. initSort () {
  91. if (this.ignoreInitSort) {
  92. this.ignoreInitSort = false
  93. return
  94. }
  95. super.initSort()
  96. }
  97. }