当前位置: 首页 > news >正文

vxe-table4.6 + vue3.2 + ant-design-vue 3.x 实现对列的显示、隐藏、排序

概要

vxe-table中的vxe-toolbar没有拖拽功能,故自己实现

源码

<template>
  <a-popover v-model:visible="open" placement="bottomRight" trigger="click">
    <template #content>
      <div class="content">
        <div class="list" ref="listRef">
          <div v-for="item in columns" :key="item.key" class="item">
            <a-checkbox v-model:checked="item.visible" @change="onCheckChange">
              {{ item.title }}
            </a-checkbox>
          </div>
        </div>
      </div>
    </template>
    <a-button shape="circle"> <MenuFoldOutlined /> </a-button>
  </a-popover>
</template>
<script setup>
import { ref, watch, nextTick, defineProps } from 'vue'
import { MenuFoldOutlined } from '@ant-design/icons-vue'
import Sortable from 'sortablejs'

const props = defineProps({
  data: {
    type: Object,
    default: () => ({})
  }
})

const open = ref(false)
const spinning = ref(false)
const columns = ref([])
const listRef = ref(null)

const onCheckChange = () => {
  const $table = props.data
  if ($table) {
    $table.refreshColumn()
  }
}

const initColumns = () => {
  const $table = props.data
  spinning.value = true
  columns.value = $table.getColumns()
  setTimeout(() => {
    spinning.value = false
  }, 300)
}

const initSortable = () => {
  setTimeout(() => {
    const el = listRef.value
    if (el) {
      Sortable.create(listRef.value, {
        animation: 150,
        ghostClass: 'ghost',
        draggable: '.item',
        onEnd: (evt) => {
          const $table = props.data
          const { oldIndex, newIndex } = evt
          const column = columns.value[oldIndex]
          columns.value.splice(oldIndex, 1)
          columns.value.splice(newIndex, 0, column)
          $table.loadColumn(columns.value)
        }
      })
    }
  }, 500)
}

watch(
  () => open.value,
  async (val) => {
    if (val && props.data) {
      initColumns()
      initSortable()
    }
  }
)
</script>

<style scoped lang="scss">
.content {
  height: 200px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .list {
    flex: 1;
    overflow-y: auto;
    margin-bottom: 10px;
  }
}
</style>

效果

在这里插入图片描述

相关文章:

  • MYSQL-创建和使用表
  • Higress: 阿里巴巴高性能云原生API网关详解
  • 分享:批量识别图片文字并重命名,根据图片文字内容对图片批量重命名,Python和Tesseract OCR的完成方案
  • 阿里云负载均衡可以抗ddos吗
  • Qt零散知识点
  • Node.js 中处理 Excel 文件的最佳实践
  • SpringBoot中3种优雅停机的实现方式
  • 基于 Qt 的图片处理工具开发(一):拖拽加载与基础图像处理功能实现
  • HTML 嵌入标签对比:小众(<embed>、<object>) 与 <iframe> 的优缺点及使用场景和方式
  • Hierarchical Reinforcement Learning for Course Recommendation in MOOCs论文阅读
  • leetcode 74. 搜索二维矩阵
  • Java从入门到“放弃”(精通)之旅——启航①
  • Ubuntu远程连接Mysql数据库(图文详解)
  • HashMap为什么扩容为原来2倍呢?
  • 栈和队列详细讲解
  • HDCP(五)
  • 【进程通信】 Linux下使用共享内存实现跨进程通信:基于C++的完整示例
  • rancher 采用ingerss ssl 部署nginx+php项目
  • c# 使用NPOI将datatable的数据导出到excel
  • OSPF不规则区域和LSA
  • 品牌网站建设完善大蝌蚪/网站优化推广公司
  • 旅游电子商务网站开发方案/我们公司想做网络推广
  • 一个网址多少钱/seo推广有哪些
  • 美化网站公司/抖音seo推广
  • 网站的运营方案/百度云网盘搜索引擎
  • 钢笔工具网站/网站优化排名的方法