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>
相关推荐
万少8 小时前
产品原型不用从零画 -GPT 出图,Gemini 生成 HTML
前端·javascript·后端
逐光老顽童9 小时前
用 Go 实现一个 LLM 路由网关:Thompson Sampling 与自适应故障转移实践
vue.js·go
wuhen_n9 小时前
RAG 第一步:多格式文档加载与文本预处理实战
前端·langchain·ai编程
程序员黑豆9 小时前
全新系列开启:AI 全栈开发
前端·后端·全栈
小小小小宇9 小时前
Partial Clone
前端
小小小小宇9 小时前
git sparse-checkout(稀疏检出)
前端
ZC跨境爬虫10 小时前
跟着 MDN 学JavaScript day_9:字符串方法实战挑战与解题思路
开发语言·前端·javascript
夜焱辰10 小时前
WebMCP 的正确打开方式:只注册 2 个工具,代理 N 个——CreatorWeave 的 On-Demand 实践
前端
hewins10 小时前
NestJS 从入门到精通
javascript
柒和远方10 小时前
LeetCode 452. 用最少数量的箭引爆气球 —— 区间贪心经典:排序 + 扫描一箭穿心
javascript·python·算法