vue3+ts+elementplus通过sortablejs实现拖拽排序

1 安装依赖

yarn add sortablejs
yarn add -save-dev @types/sortablejs

2 开始编码

<template>
	<div class="app-container home">
		<el-table
			class="t1"
			ref="dragTable"
			:data="tableData"
			row-key="id"
			border
			:row-class-name="tableRowClassName">
			<el-table-column prop="date" label="日期"></el-table-column>
			<el-table-column prop="name" label="姓名"></el-table-column>
			<el-table-column prop="address" label="地址"></el-table-column>
			<el-table-column label="操作">
				<el-button class="move" type="primary" link size="small">拖 拽</el-button>
			</el-table-column>
		</el-table>
	</div>
</template>
<script setup lang="ts">
import Sortable from 'sortablejs'
import { onMounted, ref } from 'vue'
const tableData = ref<Array<any>>([
	{
		id: '1',
		name: 'text_1',
		date: '1111-11-11',
		address: '测试_1'
	},
	{
		id: '2',
		name: 'text_2_不可拖拽',
		date: '2222-22-22',
		address: '测试_2_不可拖拽',
		disabled: true
	},
	{
		id: '3',
		name: 'text_3',
		date: '3333-33-33',
		address: '测试_3'
	},
	{
		id: '4',
		name: 'text_4',
		date: '4444-44-44',
		address: '测试_4'
	},
	{
		id: '5',
		name: 'text_5',
		date: '5555-55-55',
		address: '测试_5'
	}
])

// 创建sortable实例
function initSortable(className: string) {
	// 获取表格row的父节点
	const table = document.querySelector('.' + className + ' .el-table__body-wrapper tbody') as any
	// 创建拖拽实例
	Sortable.create(table, {
		animation: 150, //动画
		disabled: false, // 拖拽不可用? false 启用(刚刚渲染表格的时候起作用,后面不起作用)
		handle: '.move', //指定拖拽目标,点击此目标才可拖拽元素(此例中设置操作按钮拖拽)
		filter: '.disabled', //指定不可拖动的类名(el-table中可通过row-class-name设置行的class)
		dragClass: 'dragClass', //设置拖拽样式类名
		ghostClass: 'ghostClass', //设置拖拽停靠样式类名
		chosenClass: 'chosenClass', //设置选中样式类名
		// 开始拖动事件
		onStart: () => {
			console.log('开始拖动')
		},
		// 结束拖动事件
		onEnd: ({ newIndex, oldIndex }) => {
			console.log('结束拖动', `拖动前索引${oldIndex}---拖动后索引${newIndex}`)
			const currRow = tableData.value.splice(oldIndex as number, 1)[0]
			tableData.value.splice(newIndex as number, 0, currRow)
			console.log('结束拖动', tableData.value)
		}
	})
}
// 设置表格row的class
function tableRowClassName({ row }) {
	if (row.disabled) {
		return 'disabled'
	}
	return ''
}
onMounted(() => {
	initSortable('t1')
})
</script>

<style lang="scss" scope>
// 拖拽
.dragClass {
	background: rgba($color: #41c21a, $alpha: 0.5) !important;
}
// 停靠
.ghostClass {
	background: rgba($color: #6cacf5, $alpha: 0.5) !important;
}
// 选择
.chosenClass:hover > td {
	background: rgba($color: #f56c6c, $alpha: 0.5) !important;
}
</style>

界面:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要安装Vue3、TypeScript和Element Plus。 安装命令: ``` npm install vue@next vue-router@next typescript@latest --save-dev npm install element-plus --save ``` 接下来,创建一个Vue3 TypeScript项目,并在`main.ts`中引入Element Plus。 ```typescript import { createApp } from 'vue'; import App from './App.vue'; import router from './router'; import ElementPlus from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; const app = createApp(App); app.use(router); app.use(ElementPlus); app.mount('#app'); ``` 在`App.vue`中,创建一个表单,包括车牌号码和设备号码两个输入框,并添加一个绑定按钮。 ```html <template> <div class="app"> <form class="form"> <el-form-item label="车牌号码"> <el-input v-model="carNumber"></el-input> </el-form-item> <el-form-item label="设备号码"> <el-input v-model="deviceNumber"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="bindDevice">绑定</el-button> </el-form-item> </form> </div> </template> <script lang="ts"> import { defineComponent, ref } from 'vue'; export default defineComponent({ name: 'App', setup() { const carNumber = ref(''); const deviceNumber = ref(''); const bindDevice = () => { // TODO: 实现绑定设备的逻辑 }; return { carNumber, deviceNumber, bindDevice, }; }, }); </script> ``` 在`bindDevice`方法中,可以调用API将车辆和设备进行绑定。这里使用mock数据模拟API请求。 ```typescript const bindDevice = () => { // TODO: 实现绑定设备的逻辑 console.log(`车牌号码:${carNumber.value},设备号码:${deviceNumber.value}`); // mock API请求 setTimeout(() => { console.log('绑定成功!'); carNumber.value = ''; deviceNumber.value = ''; }, 2000); }; ``` 最后,运行项目即可看到界面和绑定设备的功能。 完整代码如下: ```html <template> <div class="app"> <form class="form"> <el-form-item label="车牌号码"> <el-input v-model="carNumber"></el-input> </el-form-item> <el-form-item label="设备号码"> <el-input v-model="deviceNumber"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="bindDevice">绑定</el-button> </el-form-item> </form> </div> </template> <script lang="ts"> import { defineComponent, ref } from 'vue'; export default defineComponent({ name: 'App', setup() { const carNumber = ref(''); const deviceNumber = ref(''); const bindDevice = () => { console.log(`车牌号码:${carNumber.value},设备号码:${deviceNumber.value}`); // mock API请求 setTimeout(() => { console.log('绑定成功!'); carNumber.value = ''; deviceNumber.value = ''; }, 2000); }; return { carNumber, deviceNumber, bindDevice, }; }, }); </script> <style scoped> .app { display: flex; justify-content: center; align-items: center; height: 100vh; } .form { width: 400px; } </style> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值