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>
相关推荐
十步杀一人_千里不留行38 分钟前
Google 登录集成教程(Web + Expo 移动端)
前端
gAlAxy...4 小时前
IntelliJ IDEA 四种项目构建:从普通 Java 到 Maven Web 项目
前端·firefox
my一阁4 小时前
2025-web集群-问题总结
前端·arm开发·数据库·nginx·负载均衡·web
会飞的小妖4 小时前
个人博客系统(十一、前端-简短的配置)
前端
念念不忘 必有回响5 小时前
nginx前端部署与Vite环境变量配置指南
前端·nginx·vite
JIngJaneIL6 小时前
篮球论坛|基于SprinBoot+vue的篮球论坛系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·篮球论坛系统
程序猿阿伟7 小时前
《首屏加载优化手册:Vue3+Element Plus项目提速的技术细节》
前端·javascript·vue.js
麦麦大数据7 小时前
D030知识图谱科研文献论文推荐系统vue+django+Neo4j的知识图谱|论文本文相似度推荐|协同过滤
vue.js·爬虫·django·知识图谱·科研·论文文献·相似度推荐
尘觉7 小时前
面试-浅复制和深复制?怎样实现深复制详细解答
javascript·面试·职场和发展
fruge9 小时前
Vue Pinia 状态管理实战指南
前端·vue.js·ubuntu