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;
    }
  }
}

希望可以帮到你~

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

加个关注

不迷路~~~~

相关推荐
kooboo china.20 分钟前
Tailwind CSS 实战,基于 Kooboo 构建 AI 对话框页面(四):语音识别输入功能
前端·css·人工智能·ui·html·交互·语音识别
Python涛哥30 分钟前
前端流行框架Vue3教程:26. 异步组件
前端·javascript·vue.js
赵大仁33 分钟前
Vue Hook Store 设计模式最佳实践指南
前端·vue.js·设计模式
代码搬运媛35 分钟前
Next.js路由导航完全指南
前端·javascript·vue.js·next
努力敲代码呀~36 分钟前
前端高频面试题1:HTML/CSS/浏览器/计算机网络
前端·css·html
come112341 小时前
关于 JavaScript 版本、TypeScript、Vue 的区别说明, PHP 开发者入门 Vue 的具体方案
javascript·vue.js·typescript
qq_10613834571 小时前
SpringBoot+Vue+Echarts实现可视化图表的渲染
vue.js·spring boot·echarts
怀旧,1 小时前
【Python】3.函数与列表
java·前端·python
唐人街都是苦瓜脸2 小时前
uni-app 提供的页面跳转方法详细解释及其区别
前端·uni-app