vue 实现table上下拖拽行功能

在项目中要实现表格可以上下拖拽行,这样一个功能

下面是实现的代码

html 复制代码
<div class="tablebox">
          <div class="head">
            <div class="borderright" style="width: 30%">显示</div>
            <div class="borderright" style="width: 60%">列名</div>
            <div class="borderright" style="width: 30%">拖动调整顺序</div>
          </div>
          <div
            v-for="(item, index) in items"
            :key="item.id"
            draggable="true"
            @dragstart="dragStart(index)"
            @dragover.prevent
            @drop="drop(index)"
            class="line"
          >
            <div class="borderright" style="width: 30%">
              <el-checkbox />
            </div>
            <div class="borderright" style="width: 60%">{{ item.name }}</div>
            <div class="borderright" style="width: 30%">
              <el-button type="" icon="Operation" link></el-button>
            </div>
          </div>
        </div>

//---------------js代码------------------------------

html 复制代码
const items = ref([
  { id: 1, name: 'Item 1', value: 'Value 1' },
  { id: 2, name: 'Item 2', value: 'Value 2' },
  { id: 3, name: 'Item 3', value: 'Value 3' },
])

let dragIndex = null

const dragStart = (index) => {
  dragIndex = index
}

const drop = (newIndex) => {
  if (dragIndex !== newIndex) {
    const toBeMoved = items.value[dragIndex]
    items.value.splice(dragIndex, 1)
    items.value.splice(newIndex, 0, toBeMoved)
    dragIndex = null
  }
}

//------------------css样式-----------------------------

html 复制代码
.tablebox {
  border-top: 1px solid #e4e4e4;
  border-left: 1px solid #e4e4e4;
  .head {
    display: flex;
    background: #f5f5f5;
    .borderright {
      border-right: 1px solid #e4e4e4;
      border-bottom: 1px solid #e4e4e4;
      text-align: center;
      font-size: 13px;
      color: #333333;
      font-family: '微软雅黑 Bold', '微软雅黑 Regular', '微软雅黑';
      font-weight: 700;
      line-height: 40px;
    }
  }
  .line {
    display: flex;
    line-height: 40px;
    .borderright {
      border-right: 1px solid #e4e4e4;
      border-bottom: 1px solid #e4e4e4;
      text-align: center;
    }
  }
}

希望可以帮到你~

看到这如果对你有用的话不如留下你的足迹

加个关注

不迷路~~~~

相关推荐
kungggyoyoyo16 分钟前
TRAE中国版SOLO模式上线!我用它从0到1开发了一款AI小说编辑器
前端·vue.js·trae
ohyeah18 分钟前
栈:那个“先进后出”的小可爱,其实超好用!
前端·数据结构
心随雨下28 分钟前
typescript中Triple-Slash Directives如何使用
前端·javascript·typescript
自在极意功。33 分钟前
AJAX 深度详解:从基础原理到项目实战
前端·ajax·okhttp
s***45334 分钟前
SpringBoot返回文件让前端下载的几种方式
前端·spring boot·后端
海上彼尚38 分钟前
[逆向] 1.本地登录爆破
前端·安全
什么时候吃饭43 分钟前
vue2、vue3父子组件嵌套生命周期执行顺序
前端·vue.js
2501_9409439144 分钟前
体系课\ Python Web全栈工程师
开发语言·前端·python
q***06471 小时前
SpringSecurity相关jar包的介绍
android·前端·后端
低保和光头哪个先来1 小时前
场景2:Vue Router 中 query 与 params 的区别
前端·javascript·vue.js·前端框架