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

希望可以帮到你~

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

加个关注

不迷路~~~~

相关推荐
程序员老赵39 分钟前
Docker 部署青龙面板:轻松搭建定时任务管理平台
前端·docker·开源
Swot41 分钟前
Asciidoctor 公式数字下标错位问题:原因分析与解决方案
前端
Swot41 分钟前
leo-editor 中将 Markdown 内容转成 asciidoc 格式
前端
狒狒AI1 小时前
Remotion / GSAP+Three / HyperFrames / Skia 四栈实测:代码渲染知识视频的选栈方法(附开源技能)
前端·人工智能·aigc
修炼的dance1 小时前
浏览器里导出H.265,8.5秒和55.5秒差在硬件
前端
补码缩写1 小时前
WebCodecs选编码器,探测之后还要验解码和播放
前端
大文说跨境1 小时前
亚马逊多账号管理浏览器怎么选?云登/紫鸟/AdsPower 对比
java·前端·自动化
hunter_0761 小时前
iOS 首页复杂嵌套滚动技术方案:头部固定 + 网格滚动 + Tab 吸顶 + 每 Tab 独立下拉刷新
前端
CoderYanger1 小时前
Java EE 进阶:4.Spring Web MVC 入门(第一弹)
java·开发语言·前端·spring·面试·java-ee·mvc
用户2788607285671 小时前
fallback 会盖掉平台提示词:system、空数组和 systemAppend 的差别
前端·后端