vue3实现el-table的拖拽

我这里使用的是 sortablejs 插件;

安装命令: npm install sortablejs --save

注意点:

你的表格数据中要有id作为key去使用;

javascript 复制代码
<div class="draggable">
          <el-table row-key="id" :data="form.tableData" style="width: 100%" max-height="250">
            <el-table-column type="index" width="80" label="数源顺序" />
            <el-table-column
              v-for="(column, index) in oldList"
              :key="index"
              :prop="column.prop"
              :label="column.label"
              :width="column.width"
              show-overflow-tooltip
            />
            <el-table-column fixed="right" label="操作" min-width="260">
              <template #default="scope">
                <el-button
                  type="primary"
                  @click="handleEdit(scope.row)"
                  link>
                  编辑
                </el-button>
                <el-button
                  link
                  type="danger"
                  @click.prevent="deleteRow(scope.$index)"
                >
                  删除
                </el-button>
                <el-button
                  v-if="scope.$index !== 0"
                  type="primary"
                  @click.prevent="moveUp(scope.row, scope.$index)"
                  link>
                  上移
                </el-button>
                <el-button
                  v-if="scope.$index !== form.tableData.length - 1"
                  type="primary"
                  @click.prevent="shiftDown(scope.row, scope.$index)"
                  link>
                  下移
                </el-button>
                <el-button
                  v-if="title === '数源配置' && scope.row.status === '0'"
                  type="primary"
                  @click.prevent="enable(scope.row)"
                  link>
                  启用
                </el-button>
                <el-button
                  v-if="title === '数源配置' && scope.row.status === '1'"
                  type="primary"
                  @click.prevent="forbidden(scope.row)"
                  link>
                  禁用
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>

<script setup>
import Sortable from 'sortablejs';


const form = ref({
  tableData: [
    { id: 1, status: '0', name: '哈哈哈111' },
    { id: 2, status: '1', name: '哈哈哈222' },
    { id: 3, status: '0', name: '哈哈哈333' },
    { id: 4, status: '0', name: '哈哈哈444' }],
});
const oldList = ref([]);
const columns = ref([
  { prop: 'state', label: '数源英文名称', width: '200' },
  { prop: 'name', label: '数源中文名称', width: '200' },
  { prop: 'name', label: '字段名称', width: '200' },
  { prop: 'name', label: '所属部门', width: '200' },
  { prop: 'state', label: '业务系统名称', width: '200' },
  { prop: 'state', label: '数源说明', width: '200' }
]);

// 行拖拽
const rowDrop = () => {
  // 要拖拽元素的父容器 tbody
  const tbody = document.querySelector('.draggable .el-table__body-wrapper tbody')
  Sortable.create(tbody, {
    //  可被拖拽的子元素
    draggable: ".draggable .el-table__row",
    onEnd({ newIndex, oldIndex }) {
      // newIndex 拖动到的新的索引
      // oldIndex 没拖动前的索引
      const currRow = form.value.tableData.splice(oldIndex, 1)[0]
      form.value.tableData.splice(newIndex, 0, currRow)
    }
  });
};

onMounted(() => {
  oldList.value = JSON.parse(JSON.stringify(columns.value))
  rowDrop()
})


</script>
相关推荐
编程牛马姐2 分钟前
独立站SEO流量增长:提高Google排名的优化方法
前端·javascript·网络
NotFound48618 分钟前
实战指南如何实现Java Web 拦截机制:Filter 与 Interceptor 深度分享
java·开发语言·前端
Dontla41 分钟前
高基数(High Cardinality)问题介绍(Prometheus、高基数字段、低基数字段)
前端·数据库·prometheus
一 乐2 小时前
医院挂号|基于springboot + vue医院挂号管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·医院挂号管理系统
whuhewei3 小时前
为什么客户端不存在跨域问题
前端·安全
妮妮喔妮3 小时前
supabase的webhook报错
开发语言·前端·javascript
qq_12084093714 小时前
Three.js 大场景分块加载实战:从全量渲染到可视集调度
开发语言·javascript·数码相机
yivifu4 小时前
手搓HTML双行夹批效果
前端·html·html双行夹注
奔跑的卡卡4 小时前
Web开发与AI融合-第一篇:Web开发与AI融合的时代序幕
前端·人工智能
IT_陈寒5 小时前
Redis批量删除的大坑,差点让我加班到天亮
前端·人工智能·后端