当前位置: 首页 > article >正文

el-tabel实现拖拽排序

1、使用npm安装sortableJs插件

npm install sortablejs --save

2、在需要使用的页面进行引入

import Sortable from 'sortablejs'

3、表格拖拽排序完整代码

<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>

参考:拖拽排序(el-table)


http://www.kler.cn/a/149135.html

相关文章:

  • 绩效考核的基础及基本内容
  • NOI / 1.8编程基础之多维数组 提问24:蛇形填充数组 c语言
  • 进程和线程的关系
  • 力扣106. 从中序与后序遍历序列构造二叉树
  • linux(2)之buildroot使用手册
  • asp.net mvc游戏门户网站
  • 基于U2-Net如何训练一个一键抠图模型
  • 什么是量子优势?
  • 系列十六、Spring IOC容器的扩展点
  • vue3-10
  • 【C++】构造函数和析构函数第四部分(深拷贝和浅拷贝)--- 2023.11.25
  • Spring Boot 3 + Spring Security 6 最新版本修改 Json 登录后 RememberMe 功能问题失效的解决方案
  • NextJS开发:封装shadcn/ui中的AlertDialog确认对话框
  • windows系统mobaxterm远程执行linux上ssh命令
  • 中伟视界:AI智能分析盒子的人数统计AI算法通过什么算法模型可以解决重复统计的问题?
  • 【AI考证笔记】NO.1人工智能的基础概念
  • Mysql更新Blob存储的Josn数据
  • c++调用openssl对文件加解密
  • ubuntu配置免密登录vscode
  • 如何优化 Elasticsearch 查询性能