sortableTable.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. {{define "component/sortableTableTrigger"}}
  2. <a-icon type="drag"
  3. class="sortable-icon"
  4. style="cursor: move;"
  5. @mouseup="mouseUpHandler"
  6. @mousedown="mouseDownHandler"
  7. @click="clickHandler" />
  8. {{end}}
  9. {{define "component/sortableTable"}}
  10. <script>
  11. const DRAGGABLE_ROW_CLASS = 'draggable-row';
  12. const findParentRowElement = (el) => {
  13. if (!el || !el.tagName) {
  14. return null;
  15. } else if (el.classList.contains(DRAGGABLE_ROW_CLASS)) {
  16. return el;
  17. } else if (el.parentNode) {
  18. return findParentRowElement(el.parentNode);
  19. } else {
  20. return null;
  21. }
  22. }
  23. Vue.component('a-table-sortable', {
  24. data() {
  25. return {
  26. sortingElementIndex: null,
  27. newElementIndex: null,
  28. };
  29. },
  30. props: ['data-source', 'customRow'],
  31. inheritAttrs: false,
  32. provide() {
  33. const sortable = {}
  34. Object.defineProperty(sortable, "setSortableIndex", {
  35. enumerable: true,
  36. get: () => this.setCurrentSortableIndex,
  37. });
  38. Object.defineProperty(sortable, "resetSortableIndex", {
  39. enumerable: true,
  40. get: () => this.resetSortableIndex,
  41. });
  42. return {
  43. sortable,
  44. }
  45. },
  46. render: function (createElement) {
  47. return createElement('a-table', {
  48. class: {
  49. 'ant-table-is-sorting': this.isDragging(),
  50. },
  51. props: {
  52. ...this.$attrs,
  53. 'data-source': this.records,
  54. customRow: (record, index) => this.customRowRender(record, index),
  55. },
  56. on: this.$listeners,
  57. nativeOn: {
  58. drop: (e) => this.dropHandler(e),
  59. },
  60. scopedSlots: this.$scopedSlots,
  61. }, this.$slots.default, )
  62. },
  63. created() {
  64. this.$memoSort = {};
  65. },
  66. methods: {
  67. isDragging() {
  68. const currentIndex = this.sortingElementIndex;
  69. return currentIndex !== null && currentIndex !== undefined;
  70. },
  71. resetSortableIndex(e, index) {
  72. this.sortingElementIndex = null;
  73. this.newElementIndex = null;
  74. this.$memoSort = {};
  75. },
  76. setCurrentSortableIndex(e, index) {
  77. this.sortingElementIndex = index;
  78. },
  79. dragStartHandler(e, index) {
  80. if (!this.isDragging()) {
  81. e.preventDefault();
  82. return;
  83. }
  84. const hideDragImage = this.$el.cloneNode(true);
  85. hideDragImage.id = "hideDragImage-hide";
  86. hideDragImage.style.opacity = 0;
  87. document.body.appendChild(hideDragImage);
  88. e.dataTransfer.setDragImage(hideDragImage, 0, 0);
  89. },
  90. dragStopHandler(e, index) {
  91. const hideDragImage = document.getElementById('hideDragImage-hide');
  92. if (hideDragImage) hideDragImage.remove();
  93. this.resetSortableIndex(e, index);
  94. },
  95. dragOverHandler(e, index) {
  96. if (!this.isDragging()) {
  97. return;
  98. }
  99. e.preventDefault();
  100. const currentIndex = this.sortingElementIndex;
  101. if (index === currentIndex) {
  102. this.newElementIndex = null;
  103. return;
  104. }
  105. const row = findParentRowElement(e.target);
  106. if (!row) {
  107. return;
  108. }
  109. const rect = row.getBoundingClientRect();
  110. const offsetTop = e.pageY - rect.top;
  111. if (offsetTop < rect.height / 2) {
  112. this.newElementIndex = Math.max(index - 1, 0);
  113. } else {
  114. this.newElementIndex = index;
  115. }
  116. },
  117. dropHandler(e) {
  118. if (this.isDragging()) {
  119. this.$emit('onsort', this.sortingElementIndex, this.newElementIndex);
  120. }
  121. },
  122. customRowRender(record, index) {
  123. const parentMethodResult = this.customRow?.(record, index) || {};
  124. const newIndex = this.newElementIndex;
  125. const currentIndex = this.sortingElementIndex;
  126. return {
  127. ...parentMethodResult,
  128. attrs: {
  129. ...(parentMethodResult?.attrs || {}),
  130. draggable: true,
  131. },
  132. on: {
  133. ...(parentMethodResult?.on || {}),
  134. dragstart: (e) => this.dragStartHandler(e, index),
  135. dragend: (e) => this.dragStopHandler(e, index),
  136. dragover: (e) => this.dragOverHandler(e, index),
  137. },
  138. class: {
  139. ...(parentMethodResult?.class || {}),
  140. [DRAGGABLE_ROW_CLASS]: true,
  141. ['dragging']: this.isDragging()
  142. ? (newIndex === null ? index === currentIndex : index === newIndex)
  143. : false,
  144. },
  145. };
  146. }
  147. },
  148. computed: {
  149. records() {
  150. const newIndex = this.newElementIndex;
  151. const currentIndex = this.sortingElementIndex;
  152. if (!this.isDragging() || newIndex === null || currentIndex === newIndex) {
  153. return this.dataSource;
  154. }
  155. if (this.$memoSort.newIndex === newIndex) {
  156. return this.$memoSort.list;
  157. }
  158. let list = [...this.dataSource];
  159. list.splice(newIndex, 0, list.splice(currentIndex, 1)[0]);
  160. this.$memoSort = {
  161. newIndex,
  162. list,
  163. };
  164. return list;
  165. }
  166. }
  167. });
  168. Vue.component('table-sort-trigger', {
  169. template: `{{template "component/sortableTableTrigger"}}`,
  170. props: ['item-index'],
  171. inject: ['sortable'],
  172. methods: {
  173. mouseDownHandler(e) {
  174. if (this.sortable) {
  175. this.sortable.setSortableIndex(e, this.itemIndex);
  176. }
  177. },
  178. mouseUpHandler(e) {
  179. if (this.sortable) {
  180. this.sortable.resetSortableIndex(e, this.itemIndex);
  181. }
  182. },
  183. clickHandler(e) {
  184. e.preventDefault();
  185. },
  186. }
  187. })
  188. </script>
  189. <style>
  190. @media only screen and (max-width: 767px) {
  191. .sortable-icon {
  192. display: none;
  193. }
  194. }
  195. .ant-table-is-sorting .draggable-row td {
  196. background-color: #ffffff !important;
  197. }
  198. .dark .ant-table-is-sorting .draggable-row td {
  199. background-color: var(--dark-color-surface-100) !important;
  200. }
  201. .ant-table-is-sorting .dragging td {
  202. background-color: rgb(232 244 242) !important;
  203. color: rgba(0, 0, 0, 0.3);
  204. }
  205. .dark .ant-table-is-sorting .dragging td {
  206. background-color: var(--dark-color-table-hover) !important;
  207. color: rgba(255, 255, 255, 0.3);
  208. }
  209. .ant-table-is-sorting .dragging {
  210. opacity: 1;
  211. box-shadow: 1px -2px 2px #008771;
  212. transition: all 0.2s;
  213. }
  214. .ant-table-is-sorting .dragging .ant-table-row-index {
  215. opacity: 0.3;
  216. }
  217. </style>
  218. {{end}}