vuedraggable 实现两个列表拖拽、删除、编辑

1. 不多说直接看图片明了:(线上项目)

2.开始上代码:

左边代码结构,为模板库 templateList  是组件

<el-col :span="6" style="margin-right: 15px;">
    <div class="title">
        <h1>模板库</h1>
        <span>可选中模板拖拽至页面中</span>
    </div>
    <div class="draggableList">
        <draggable v-model="templateList" class="list-group" v-bind="dragOptions1">
            <transition-group>
             <div v-for="item in templateList" :key="item.localFloorName" class="wrap">
                 <el-card shadow="hover">
                    <img :src="item.img" alt="" class="cardImg" />
                  </el-card>
                </div>
            </transition-group>
        </draggable>
    </div>
</el-col>

3.右边结构

 

<el-col :span="6" style="margin-right: 15px;">
          <div class="title">
            <h1>页面编辑</h1>
            <el-link type="primary" @click="setup">设置预览用户</el-link>
          </div>
          <div class="draggableList">
            <draggable v-model="listRight" class="list-group" v-bind="dragOptions2" @add="updateHandler" @update="rightEnd">
              <div v-for="(item, key) in listRight" :key="key" class="wrap" :class="{ 'current-select': item === componentData }">
                <el-card shadow="hover">
                  <div class="imIwrap" v-if="item">
                    <div @click="editCompnent(item)" v-for="res in visualization" :key="res.id">
                      <component v-if="item.floorType === res.id" :dataInfo.sync="item" :is="res.path" />
                    </div>
                    <!-- <img :src="item.img" alt="" @click="editCompnent(item)" class="cardImg" /> -->
                    <i class="el-icon-delete delet" @click="deletComponents(key, item)"></i>
                    <i class="el-icon-top up" v-if="item.floorType !== 1 && item.floorType !== 13 && hasUpArrow(key)" @click="up(item, key)"></i>
                    <i class="el-icon-bottom down" v-if="item.floorType !== 1 && item.floorType !== 13 && hasDownArrow(key)" @click="down(item, key)"></i>
                  </div>
                </el-card>
              </div>
            </draggable>
          </div>
        </el-col>
computed: {
    dragOptions1() {
      return {
        animation: 0,
        group: {
          name: 'description',
          pull: 'clone',
          put: false
        },
        ghostClass: 'ghost'
      }
    },
    dragOptions2() {
      return {
        animation: 0,
        group: 'description'
      }
    }
  },
/**
     * 删除某个楼层
     *
     */
    async deletComponents(v, item) {
      try {
        await this.deleteFloorByIdFun(item.floorId)
        await this.initData()
      } catch (err) {
        console.log(err)
      }
    },
    /**
     * 上移楼层
     *
     */
    async up(data, index) {
      // TODO: 边界情况处理 楼层索引和orderVal值的关系
      console.log(data, index)
      await this.modifyAppPageFloorOrderValFun(data.floorCode, index)
      this.initData()
    },
    /**
     * 楼层下移
     *
     */
    async down(data, index) {
      // TODO: 边界情况处理 楼层索引和orderVal值的关系
      console.log(data, index)
      await this.modifyAppPageFloorOrderValFun(data.floorCode, index + 2)
      this.initData()
    },
    // 组件拖拽完成
    async updateHandler(evt) {
      let { newIndex: newDraggableIndex } = evt
      const moveData = this.listRight[newDraggableIndex]
      this.sortListRight() // 数据可能重新排序
      newDraggableIndex = this.listRight.indexOf(moveData) // 重新根据数据找回索引位置
      newDraggableIndex++ // 楼层排序默认从1开始
      try {
        const { data: newFloorCode } = await this.createAppPageFloorFun(moveData.floorType, newDraggableIndex, moveData.navigationType)
        await this.initData(newFloorCode)
        this.componentsName = moveData.localFloorName
        this.tableComponent = moveData.components
      } catch (err) {
        this.listRight.splice(newDraggableIndex, 1)
      }
    },
    // 右边移动
    async rightEnd(evt) {
      let { newIndex: newDraggableIndex } = evt
      const moveData = this.listRight[newDraggableIndex]
      this.sortListRight() // 数据可能重新排序
      newDraggableIndex = this.listRight.indexOf(moveData) // 重新根据数据找回索引位置
      newDraggableIndex++ // 楼层排序默认从1开始
      console.log(moveData, 'moveData')
      await this.modifyAppPageFloorOrderValFun(moveData.floorCode, newDraggableIndex++)
      this.initData()
    },
    // 具体编辑某组件
    editCompnent(v) {
      if (v.floorId) {
        this.componentData = v
        const { components, localFloorName } = v
        this.tableComponent = components
        this.componentsName = localFloorName
      }
    },

 

Vue2框架中,`vuedraggable`是一个非常有用的插件,可以轻松地实现元素的拖拽功能,包括表格行的拖动。要实现两个`el-table`之间拖拽行,你需要按照以下步骤操作: 1. 安装依赖:首先需要安装`vuedraggable`插件,你可以通过npm或yarn来添加它: ```bash npm install vuedraggable --save # 或者 yarn add vuedraggable ``` 2. 引入并注册:在你的Vue组件中引入并注册`vuedraggable`,通常会放在`<script>`标签中: ```javascript import draggable from 'vuedraggable'; export default { components: { DraggableTableRow: draggable({ group: '__tableRows__', // 设置同组内的元素可以互相拖拽 dragStart: (e) => { // 开始拖动时执行的函数,例如更新数据状态 }, dragEnd: (e) => { // 拖动结束时执行的函数,处理目标位置等 } }) }, }; ``` 3. 使用`DraggableTableRow`组件:将这两个`el-table`的数据源包裹在`draggable`组件里,比如每个表的`tbody`部分: ```html <el-table ref="table1"> <template v-for="(item, index) in tableData1" :key="index"> <DraggableTableRow :dragItem="item" @drop="handleDrop(item, $event.newIndex)"> <!-- 表格行内容 --> </DraggableTableRow> </template> </el-table> <!-- 类似于上一个table,但是用ref="table2" --> <el-table ref="table2"> <!-- ... --> </el-table> ``` 4. 编写拖放事件处理器:`handleDrop`方法会在拖动的行到达目的地时触发,可以在这里移动数据、更新UI或者其他必要的操作: ```javascript methods: { handleDrop(item, newIndex) { const sourceTable = this.$refs.table1; const targetTable = this.$refs.table2; // 获取源表当前索引 let sourceIndex = sourceTable.rows.indexOf(item); // 更新源表数据 sourceTable.rows.splice(sourceIndex, 1); // 将行插入到目标表的新位置 targetTable.rows.splice(newIndex, 0, item); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值