el-table 表格结合Sortable.js快速实现拖拽

// 通过 npm安装
npm install sortablejs --save
 //导入
import Sortable from 'sortablejs'

实例:

    // 行拖拽
    const rowDrop = () => {
      const tbody = document.querySelector('.el-table__body-wrapper tbody')
      const _this = state
      Sortable.create(tbody, {
        onEnd({ newIndex, oldIndex }) {
          if (newIndex == oldIndex) return
          _this.tableData.splice(newIndex, 0, _this.tableData.splice(oldIndex, 1)[0])
          var newArray = _this.tableData.slice(0)
          _this.tableData = []
          ctx.root.$nextTick(() => {
            _this.tableData = newArray
          })
        },
      })
    }

再mounted的时候调用一下该方法,快速实现行拖拽

 

 onMounted(() => {
      init()
      // 阻止默认行为
      document.body.ondrop = function (event) {
        event.preventDefault()
        event.stopPropagation()
      }
      rowDrop() //行拖拽
    })

组件文档:Sortable.js中文网|配置


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue2中使用Sortable.js实现两个el-table之间的树形结构拖拽功能,可以按照以下步骤进操作: 1. 安装Sortable.js和Vue-Sortable插件: ```bash npm install sortablejs vue-sortable ``` 2. 在Vue组件中引入并使用Vue-Sortable插件: ```vue <template> <div> <el-table :data="treeData1" :row-key="row => row.id" ref="table1" > <!-- 表格列定义 --> </el-table> <el-table :data="treeData2" :row-key="row => row.id" ref="table2" > <!-- 表格列定义 --> </el-table> </div> </template> <script> import Sortable from 'sortablejs'; import { VueSortable } from 'vue-sortable'; export default { components: { VueSortable }, data() { return { treeData1: [ // 表格数据 ], treeData2: [ // 表格数据 ] }; }, mounted() { this.$nextTick(() => { this.makeTablesSortable(); }); }, methods: { makeTablesSortable() { Sortable.create(this.$refs.table1.$el, { group: 'table', animation: 150, onEnd: this.onTableDragend }); Sortable.create(this.$refs.table2.$el, { group: 'table', animation: 150, onEnd: this.onTableDragend }); }, onTableDragend(event) { const sourceTable = event.from === this.$refs.table1.$el ? 'table1' : 'table2'; const targetTable = event.to === this.$refs.table1.$el ? 'table1' : 'table2'; const sourceData = this[sourceTable]; const targetData = this[targetTable]; // 根据event对象中的信息,更新sourceData和targetData的顺序 } } }; </script> ``` 在上面的示例代码中,我们使用Vue-Sortable插件将Sortable.js应用于el-table。通过在mounted钩子函数中调用makeTablesSortable方法,我们创建了两个可拖拽Sortable实例,并将其应用于el-table组件。在onEnd回调函数中,我们可以根据拖拽事件的信息来更新sourceData和targetData的顺序,从而实现树形结构的拖拽功能。 请注意,上述代码仅提供了一个基本的示例,具体的更新逻辑需要根据你的业务需求来实现。 希望以上信息对你有所帮助!如果有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值