Vue.js学习笔记(七)使用sortablejs或el-table-draggable拖拽ElementUI的el-table表格组件

文章目录


前言

记录 el-table-draggable 插件使用方法。


一、el-table-draggable是什么?

el-table-draggable的存在就是为了让vue-draggable支持element-ui中的el-table组件,让表格可以进行行列拖动等功能。

项目地址:https://gitcode.com/gh_mirrors/el/el-table-draggable

二、使用步骤

1.安装使用

javascript 复制代码
npm i -S el-table-draggable

(示例):拖动修改数据排序,输入修改逻辑为,【1,2,3,4】,修改2为3,则3-1。

javascript 复制代码
<template>
	<el-table-draggable @input="dragInputHandler">
		<el-table :data="tableData" row-key="id" v-loading="loading" size="mini" 
		max-height="500px">
			<el-table-column label="展示排序" width="160" prop="sort" align="center" sortable>
              <template slot-scope="{row}">
                <el-input-number v-model="row.newSort" controls-position="right" :min="1"
                  @change="(newSort) => { editSort(row, newSort) }" @blur="() => {
                if (!row.newSort) { $message.warning('序号不能为空'); row.newSort = row.sort;};
                if (Number(row.newSort) <= 0) { $message.warning('序号不能小于1'); row.newSort= row.sort; };
                  }"></el-input-number>
              </template>
            </el-table-column>
          </el-table>
        </el-table-draggable>
</template>


import ElTableDraggable from "el-table-draggable";
export default {
 components: {
    ElTableDraggable,
  },
 methods:{
    /** 输入修改排序 */
    editSort(row, rowSort) {
      if (!rowSort) return;
      const oldSort = Number(row.sort);
      const newSort = Math.min(Number(rowSort), this.tableData.length);
      this.$nextTick(() => {
        this.tableData = this.tableData.map((item) => {
          const itemData = { ...item };
          if (itemData.id === row.id) {
            itemData.sort = newSort;
            itemData.newSort = newSort;
          } else if (oldSort < newSort) {
            if (itemData.sort > oldSort && itemData.sort <= newSort) {
              itemData.sort -= 1;
              itemData.newSort -= 1;
            }
          } else if (oldSort > newSort) {
            if (itemData.sort >= newSort && itemData.sort < oldSort) {
              itemData.sort += 1;
              itemData.newSort += 1;
            }
          }
          return itemData;
        });
      })
    },
     /** 拖拽排序 */
    dragInputHandler(data) {
      this.$nextTick(() => {
        this.tableData = data.map((item, index) => { return { ...item, sort: index + 1,newSort: index + 1 } });
      });
    },
  },
}

2.sortablejs

el-table-draggable插件是基于sortablejs进行封装的,如果已经安装了sortablejs,想实现element的el-table拖拽,则可以进行以下操作。

javascript 复制代码
<template>
    <el-table ref="tableList" :data="tableList" row-key="id" tooltip-effect="dark" 
    max-height="500"  border v-loading="loading" class="draggable">
      <el-table-column label="拖拽排序" width="80" align="center">
        <template slot-scope="{ row }">
          <i class="el-icon-rank allowDrag" style="cursor:pointer"></i>
        </template>
      </el-table-column>
     </el-table>
</template>


import Sortable from "sortablejs";
export default {
 mounted() {
	this.$nextTick(() => {
        this.lineDrop();
	});
 },
 methods:{
     /** 拖拽 */
    lineDrop() {
      const tbody = document.querySelector(".draggable .el-table__body-wrapper tbody");
      const _this = this;
      new Sortable(tbody, {
        animation: 150,
        ghostClass: "ghostClass",
        handle: ".allowDrag",//设置操作区域
        onEnd(evt) {
          const newIndex = evt.newIndex;
          const row = _this.tableList[newIndex];
          const oneRow = _this.tableList[newIndex - 1];
          hotLangSort({
            id: oneRow.id,
            langId: _this.searchParams.langId,
            sort: row.sort,
            type: _this.activeTab === 'series' ? 0 : 1
          }).then(res => {
            _this.$message.success('操作成功')
            _this.$parent.getInfo();
            _this.getInfo()
          }).catch(err => { _this.$message.error('操作失败') }).finally(() => { })
        }
      })
    },
 }
}

总结

以上为拖拽插件学习记录。

相关推荐
羊羊小栈1 天前
基于「多模态大模型 + BGE向量检索增强RAG」的航空维修智能问答系统(vue+flask+AI算法)
vue.js·人工智能·python·语言模型·flask·毕业设计
peter67681 天前
pandas学习小结
学习·pandas
机器视觉知识推荐、就业指导1 天前
STM32 外设驱动模块【含代码】:SG90 舵机模块
stm32·单片机·嵌入式硬件·学习
喝拿铁写前端1 天前
Vue 组件通信的两种世界观:`.sync` 与普通 `props` 到底有什么不同?
前端·vue.js·前端框架
用户22152044278001 天前
vue3组件间的通讯方式
前端·vue.js
暴富奥利奥1 天前
完成docker方式的ros环境配置
linux·学习·docker·容器
tyro曹仓舒1 天前
Vue单文件组件到底需不需要写name
前端·vue.js
总有刁民想爱朕ha1 天前
AI大模型学习(17)python-flask AI大模型和图片处理工具的从一张图到多平台适配的简单方法
人工智能·python·学习·电商图片处理
Cathy Bryant1 天前
球极平面投影
经验分享·笔记·数学建模
閞杺哋笨小孩1 天前
Vue3 点击指令(防抖 / 节流)
前端·vue.js