前言

徐小宝:本文仅以经验指南实现拖拉拽效果,涉及TypeScript、Elment Plus、SortableJs 等,以表格为例讲演。

一、基础了解

主要了解本文涉及的框架语言工具等,了解其基本内容

1、Vue 3

2、TypeScript

3、Element Plus

4、SortableJs

二、SortableJs安装

1、SortableJs安装

npm i sortablejs
pnpm add sortablejs
yarn add sortablejs

2、SortableJs类型安装

为了在TypeScript下有代码提示,且不会找不到模块

npm i @types/sortablejs -D
pnpm add @types/sortablejs -D
yarn add @types/sortablejs -D

三、代码封装

1、自定义指令 draggable

// FilePath: src/directives/draggable/index.ts
import type { DraggableOption } from '@/directives/draggable/types';
import Sortable from 'sortablejs';
import type { Directive, DirectiveBinding } from 'vue';

const draggable: Directive = {
	mounted(el: HTMLElement, binding: DirectiveBinding) {
		const options: DraggableOption[] = binding.value;
		options.forEach(item => {
			new Sortable(el.querySelector(item.selector) as HTMLElement, item.options);
		});
	}
};

export default draggable;

2、自定义指令 draggable 值类型定义

// FilePath: src/directives/draggable/types.ts
import type Sortable from 'sortablejs';

export interface DraggableOption {
	selector: string;
	options: Sortable.Options;
}

3、自定义指令入口定义

// FilePath: src/directives/index.ts
import type { App } from 'vue';
import draggable from './draggable';

const directives = {
	draggable
};

const install = (app: App) => {
	Object.keys(directives).forEach(key => {
		app.directive(key, directives[key as keyof typeof directives]);
	});
};

export default {
	install
};

4、使用

// FilePath: main.ts
import Directives from '@/directive';
import { createApp } from 'vue';
import App from './App.vue';

app.use(Directives);

import vDraggable from '@/directives/draggable';

< script setup>注册

import draggable from '@/directives/draggable';

export default {
   setup(){
   	/*...*/
   },
   directives: {
   	draggable
   }
} 

四、示例

<template>
	<el-table :data="data" v-draggable="draggableOptions">
		<el-table-column type="index" label="序号" align="center" width="50" />
		<el-table-column prop="name" label="姓名" align="center" />
	</el-table>
</template>

<script setup lang="ts">
import type { SortableEvent } from 'sortablejs';
import { nextTick, onBeforeMount, ref } from 'vue';

interface DemoData {
	name: string;
	age: number;
}

ononBeforeMount(() => {
	getData(10);
});

const demoData = ref<DemoData[]>([]);
const getData = (length: number) => {
	demoData.value = Array.from({ length }).map((_, index) => ({
		name: `张三-${index}`
		age: index * 2
	}))
};

const draggableOptions = [{
	selector: 'tbody',
	options: {
		animation: 150,
		onEnd: (evt: SortableEvent) => {
			const { newIndex, oldIndex } = evt;
			const arr: DemoData[] = [...demoData.value];

			// 拖动元素与新位置元素互换位置
			// ES6 解构写法
			// [arr[newIndex as number],arr[oldIndex as number]] = [arr[oldIndex as number],arr[newIndex as number]]
			// ES5 普通写法
			// arr.splice(newIndex as number,1, ...arr.splice(oldIndex as number,1,arr[newIndex as number]))
			// 拖动元素至新位置后其余依次下移
			const [moveRowData] = [...arr.splice(oldIndex as number, 1)];
			arr.splice(newIndex as number, 0, moveRowData);
			
			demoData.value = [];
			nextTick(() => {
				demoData.value = [...arr];
			});
		}
	}
}];
</script>

五、结语

本文仅以Vue 3+TypeScript+Elment Plus+SortableJs 环境下,使用Vue自定义指令实现表格拖拽效果,其他框架及元素(HTMLElment) 可自行理解后实现,验证后欢迎私聊扩充示例代码

如有问题或不到之处,欢迎评论留言

原文地址:https://blog.csdn.net/jk4568/article/details/129160622

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_30746.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注