前言
徐小宝:本文仅以经验指南,实现拖拉拽效果,涉及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类型安装
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';
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进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。