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>
相关推荐
小二·11 分钟前
Python Web 开发进阶实战:AI 伦理审计平台 —— 在 Flask + Vue 中构建算法偏见检测与公平性评估系统
前端·人工智能·python
走粥19 分钟前
选项式API与组合式API的区别
开发语言·前端·javascript·vue.js·前端框架
We་ct22 分钟前
LeetCode 12. 整数转罗马数字:从逐位实现到规则复用优化
前端·算法·leetcode·typescript
方安乐30 分钟前
react笔记之useMemo
前端·笔记·react.js
晚霞的不甘34 分钟前
解决 Flutter for OpenHarmony 构建失败:HVigor ERROR 00303168 (SDK component missing)
android·javascript·flutter
清风细雨_林木木38 分钟前
react 中 form表单提示
前端·react.js·前端框架
小二·1 小时前
Python Web 开发进阶实战:边缘智能网关 —— 在 Flask + MicroPython 中构建轻量级 IoT 边缘推理平台
前端·python·flask
TOPGUS1 小时前
解析200万次对话数据:ChatGPT引用内容的核心特征与优化策略
前端·人工智能·搜索引擎·chatgpt·seo·数字营销
羊仔AI探索1 小时前
前端已死,未来已来,谷歌Gemini 3 Pro杀回来了!
前端·人工智能·ai·aigc
2501_944521591 小时前
Flutter for OpenHarmony 微动漫App实战:分享功能实现
android·开发语言·javascript·flutter·ecmascript