vue3 elementPlus 表格实现行列拖拽及列检索功能

1、安装vuedraggable

npm i -S vuedraggable@next

2、完整代码

TypeScript 复制代码
<template>
<div class='container'>
  <div class="dragbox">
    <el-table row-key="id" :data="tableData" :border="true">
      <el-table-column
        v-for="item in columnList"
        :key="item.prop"
        :prop="item.prop"
        :label="item.label"
        sortable
      >
        <template #header>
          {{item.label}}
          <el-popover
            :visible="item.visible"
            placement="bottom"
            :width="200"
            trigger="click"
          >
            <template #reference>
              <el-button :type="item.input===''?'info':'primary'" link :icon="Search" @click.stop="item.visible = !item.visible"></el-button>
            </template>
            <div>
              <el-input v-model="item.input" placeholder="请输入" size="small" />
              <div style="margin-top: 5px; display: flex; justify-content: space-between;">
                <el-button size="small" type="primary" @click="searchItem(item)">查询</el-button>
                <el-button size="small" @click="resetItem(item)">重置</el-button>
              </div>
            </div>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
  </div>
</div>
</template>
<script setup lang='ts'>
import { Search } from '@element-plus/icons-vue'
import Sortable from "sortablejs"
import { ref } from 'vue'

const tableData = ref([
  {id: 1, name: '纸巾', type: '百货', price: 30},
  {id: 2, name: '抽纸', type: '百货', price: 18},
  {id: 3, name: '水杯', type: '百货', price: 50},
])

const columnList = ref([
  {label: '名称', prop: 'name', input: '', visible: false},
  {label: '类型', prop: 'type', input: '', visible: false},
  {label: '价格', prop: 'price', input: '', visible: false},
])
onMounted(() => {
  // 页面加载完成执行拖拽函数
  rowDrop()
  columnDrop()
})

// 列查询
const searchItem = (item: any) => {
  item.visible = false
  console.log(item);
}
// 列查询重置
const resetItem = (item: any) => {
  item.visible = false
  item.input = ''
  console.log(item);
}
// 行拖拽
const rowDrop = () => {
  const tbody = document.querySelector('.dragbox .el-table__body-wrapper tbody');
  Sortable.create(tbody, {
    draggable: ".dragbox .el-table__row",
    onEnd(evt: any) {
      const currRow = tableData.value.splice(evt.oldIndex, 1)[0];
      tableData.value.splice(evt.newIndex, 0, currRow);
      console.log(tableData.value);
    }
  });
}
// 列拖拽
const columnDrop = () => {
  const tr = document.querySelector('.dragbox .el-table__header-wrapper tr');
  Sortable.create(tr, {
    animation: 150,
    delay: 0,
    onEnd: (evt: any) => {
      const oldItem = columnList.value[evt.oldIndex];
      columnList.value.splice(evt.oldIndex, 1);
      columnList.value.splice(evt.newIndex, 0, oldItem);
      console.log(columnList.value);
    }
  });
}

</script>
<style lang='scss' scoped>
.container {
  height: 100vh;
  .dragbox {
    height: 100%;
  }
}
</style>
相关推荐
头顶一只喵喵14 小时前
Vue3基础知识:组合式API中的provide和inject,他们作用是什么?如何使用?以及案例演示
前端·javascript·vue.js·vue3
左边的天堂1 天前
【篇三】在vue3上实现阿里云oss文件直传
vue3·文件上传·oss·分片上传
至天1 天前
UniApp 中 Web/H5 正确使用反向代理解决跨域问题
前端·uni-app·vue3·vue2·vite·反向代理
宁波阿成2 天前
基于jeecgboot-vue3的Flowable流程-集成仿钉钉流程(一)图标svgicon的使用
前端·vue3·flowable·jeecgboot
Modify_QmQ2 天前
Leaflet【六】绘制交互图形、测量、经纬度展示
gis·vue3·leaflet·地图交互·距离测量、面积测量
alicca3 天前
Vue3 Hooks 用法 scrollTop, mousemoveHandler,useCountDown
前端·javascript·vue.js·vue3
宁波阿成5 天前
tomcat8.5在windows下运行出现日志中文乱码
windows·tomcat·vue3
.陌路5 天前
uniapp开发手机APP、H5网页、微信小程序、长列表插件
小程序·uni-app·vue·app·vue3·list长列表
简学云资源平台5 天前
如何在Vue3项目中使用Pinia进行状态管理
vue3
kingbal7 天前
Vuetify3:快捷回到顶部
前端·vue3·vuetify3·一键回到顶部