npm install sortablejs --save
import Sortable from 'sortablejs'
<template>
<div class="home">
<el-table :data="tableData" style="width: 100%">
<el-table-column v-for="(item, index) in col" :key="`col_${index}`" :prop="item.prop" :label="item.label">
</el-table-column>
</el-table>
</div>
</template>
<script setup>
import Sortable from 'sortablejs'; //引入插件
import { onMounted, ref, nextTick } from 'vue';
const col = ref([
{
label: '日期',
prop: 'date'
},
{
label: '姓名',
prop: 'name'
},
{
label: '地址',
prop: 'address'
}
]);
const tableData = ref([
{
date: '2016-05-03',
name: '建筑电工',
address: '天河区'
},
{
date: '2016-05-02',
name: '管道工',
address: '番禺区'
},
{
date: '2016-05-04',
name: '木工',
address: '越秀区'
},
{
date: '2016-05-01',
name: '架子工',
address: '海珠区'
}
]);
onMounted(() => {
// 阻止默认行为
document.body.ondrop = function (event) {
event.preventDefault();
event.stopPropagation();
};
rowDrop();
columnDrop();
});
//行拖拽
const rowDrop = () => {
const tbody = document.querySelector('.el-table__body-wrapper tbody');
Sortable.create(tbody, {
onEnd({ newIndex, oldIndex }) {
if (newIndex == oldIndex) return;
tableData.value.splice(newIndex, 0, tableData.value.splice(oldIndex, 1)[0]);
const newArray = tableData.value.slice(0);
tableData.value = [];
nextTick(function () {
tableData.value = newArray;
});
}
});
};
//列拖拽
const columnDrop = () => {
const wrapperTr = document.querySelector('.el-table__header-wrapper tr');
Sortable.create(wrapperTr, {
animation: 180,
delay: 0,
onEnd: (evt) => {
const oldItem = col.value[evt.oldIndex];
col.value.splice(evt.oldIndex, 1);
col.value.splice(evt.newIndex, 0, oldItem);
const newArray = col.value.slice(0);
col.value = [];
nextTick(function () {
col.value = newArray;
});
}
});
};
</script>
<style scoped>
.home {
font-size: 36px;
}
</style>
4、列表的拖拽排序
列表拖拽排序一般只用在行的拖拽排序,此处我们依旧可以使用sortableJs来实现拖拽排序的功能,具体代码如下
<template>
<div class="home">
<div style="width: 800px">
<ul id="items">
<li v-for="item in listData" :key="item.id" class="item">
{{ item.name }}
</li>
</ul>
</div>
</div>
</template>
<script setup>
import Sortable from 'sortablejs'; //引入插件
import { onMounted, ref, nextTick } from 'vue';
const listData = ref([
{
id: 1,
name: '数据一'
},
{
id: 2,
name: '数据二'
},
{
id: 3,
name: '数据三'
},
{
id: 4,
name: '数据四'
}
]);
onMounted(() => {
// 阻止默认行为
document.body.ondrop = function (event) {
event.preventDefault();
event.stopPropagation();
};
rowDrop();
});
//行拖拽
const rowDrop = () => {
const tbody = document.getElementById('items');
Sortable.create(tbody, {
onEnd({ newIndex, oldIndex }) {
if (newIndex == oldIndex) return;
listData.value.splice(newIndex, 0, listData.value.splice(oldIndex, 1)[0]);
const newArray = listData.value.slice(0);
listData.value = [];
nextTick(function () {
listData.value = newArray;
console.log(listData.value);
});
}
});
};
</script>
<style scoped>
.item {
border: 1px solid #a7a2a2;
padding: 10px;
}
</style>
原文地址:https://blog.csdn.net/weixin_43422861/article/details/134664136
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_22880.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。