diff --git a/locales/en_US.json b/locales/en_US.json index ccd0df8..6111b30 100644 --- a/locales/en_US.json +++ b/locales/en_US.json @@ -89,6 +89,7 @@ "route": { "appRoot": "Home", "cardList": "Card list", + "draggableList": "Draggable list", "commonList": "Common list", "dashboard": "Dashboard", "demo": "Function example", diff --git a/locales/zh_CN.json b/locales/zh_CN.json index b693bd6..c049528 100644 --- a/locales/zh_CN.json +++ b/locales/zh_CN.json @@ -126,6 +126,7 @@ "list": "列表页", "commonList": "常用列表", "cardList": "卡片列表", + "draggableList": "拖拽列表", "demo": "功能示例", "fetch": "请求示例", "echarts": "Echarts示例", diff --git a/package.json b/package.json index 4e0fb32..4a80f4a 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "quill": "^2.0.2", "radash": "^12.1.0", "vue": "^3.5.1", + "vue-draggable-plus": "^0.5.3", "vue-i18n": "^9.14.0", "vue-router": "^4.4.3" }, diff --git a/src/hooks/useTableDrag.ts b/src/hooks/useTableDrag.ts new file mode 100644 index 0000000..d31ea15 --- /dev/null +++ b/src/hooks/useTableDrag.ts @@ -0,0 +1,35 @@ +import type { NDataTable } from 'naive-ui' +import { useDraggable } from 'vue-draggable-plus' + +export function useTableDrag(params: { + tableRef: Ref | undefined> + data: Ref + onRowDrag: (rows: T[]) => void +}) { + const tableEl = computed(() => params.tableRef?.value?.$el as HTMLElement) + const tableBodyRef = ref(undefined) + + const { start } = useDraggable(tableBodyRef, params.data, { + immediate: false, + animation: 150, + handle: '.drag-handle', + onEnd: (event) => { + const { oldIndex, newIndex } = event + const start = Math.min(oldIndex!, newIndex!) + const end = Math.max(oldIndex!, newIndex!) - start + 1 + const changedRows = [...params.data.value].splice(start, end) + params.onRowDrag(unref([...changedRows])) + }, + }) + + onMounted(async () => { + while (!tableBodyRef.value) { + tableBodyRef.value = tableEl.value?.querySelector('tbody') || undefined + await new Promise(resolve => setTimeout(resolve, 100)) + } + }) + + watchOnce(() => tableBodyRef.value, (el) => { + el && start() + }) +} diff --git a/src/router/routes.static.ts b/src/router/routes.static.ts index 73a382c..f8473ca 100644 --- a/src/router/routes.static.ts +++ b/src/router/routes.static.ts @@ -435,4 +435,14 @@ export const staticRoutes: AppRoute.RowRoute[] = [ id: 43, pid: 13, }, + { + name: 'draggableList', + path: '/list/draggableList', + title: '拖拽列表', + requiresAuth: true, + icon: 'icon-park-outline:menu-fold', + componentPath: '/demo/list/draggableList/index.vue', + id: 44, + pid: 10, + }, ] diff --git a/src/styles/index.css b/src/styles/index.css index 8c119bf..5c75a34 100644 --- a/src/styles/index.css +++ b/src/styles/index.css @@ -14,3 +14,7 @@ body, .gray-mode { filter: grayscale(100%); } + +.drag-handle { + cursor: move; +} diff --git a/src/views/demo/list/draggableList/index.vue b/src/views/demo/list/draggableList/index.vue new file mode 100644 index 0000000..80351ab --- /dev/null +++ b/src/views/demo/list/draggableList/index.vue @@ -0,0 +1,170 @@ + + +