columns.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import Sortable from "sortablejs";
  2. import { clone } from "@pureadmin/utils";
  3. import { tableDataDrag } from "../../data";
  4. import { ref, nextTick, onMounted } from "vue";
  5. // 列拖拽演示
  6. export function useColumns() {
  7. const dataList = ref(clone(tableDataDrag, true));
  8. const columnsDrag = ref<TableColumnList>([
  9. {
  10. label: "ID",
  11. prop: "id"
  12. },
  13. {
  14. label: "日期",
  15. prop: "date"
  16. },
  17. {
  18. label: "姓名",
  19. prop: "name"
  20. }
  21. ]);
  22. const columns = ref<TableColumnList>([
  23. {
  24. label: "ID",
  25. prop: index => columnsDrag.value[index].prop as string
  26. },
  27. {
  28. label: "日期",
  29. prop: index => columnsDrag.value[index].prop as string
  30. },
  31. {
  32. label: "姓名",
  33. prop: index => columnsDrag.value[index].prop as string
  34. }
  35. ]);
  36. const columnDrop = (event: { preventDefault: () => void }) => {
  37. event.preventDefault();
  38. nextTick(() => {
  39. const wrapper: HTMLElement = document.querySelector(
  40. ".el-table__header-wrapper tr"
  41. );
  42. Sortable.create(wrapper, {
  43. animation: 300,
  44. delay: 0,
  45. onEnd: ({ newIndex, oldIndex }) => {
  46. const oldItem = columnsDrag.value[oldIndex];
  47. columnsDrag.value.splice(oldIndex, 1);
  48. columnsDrag.value.splice(newIndex, 0, oldItem);
  49. }
  50. });
  51. });
  52. };
  53. onMounted(() => {
  54. nextTick(() => {
  55. columnDrop(event);
  56. });
  57. });
  58. return {
  59. columns,
  60. dataList,
  61. columnsDrag
  62. };
  63. }