antd vue tabs标签拖拽功能实现

本文介绍了如何在Ant Design Vue 3.2.13版本中为Tabs组件添加拖拽功能,由于官方不支持,因此借助vuedraggable库来实现。首先安装vuedraggable,然后在模板中自定义TabBar,使用draggable包裹Tab标签,通过v-model和item-key属性绑定数据,并监听拖拽事件更新Tab页。注意保持Tab激活状态的class和点击切换的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

antv的tabs标签拖拽实现

目前antd vue3.2.13tabs标签页不支持左右拖拽功能。现项目中需要使用,为此引入vuedraggable

准备

  • 安装vuedraggable
	npm i vuedraggable

开始

代码

<template>
  <a-tabs v-model:activeKey="activeKey">
    <a-tab-pane v-for="item in state.list" :key="item.id" tab="Tab 1">
      我是{{ item.name }},今年{{ item.age }}
    </a-tab-pane>

    <template #renderTabBar>
      <div class="ant-tabs-nav-wrap">
        <div class="ant-tabs-nav-list" style="transform: translate(0px, 0px)">
          <draggable v-model="state.list" item-key="id">
            <template #item="{ element }">
              <div
                class="ant-tabs-tab"
                :class="{ 'ant-tabs-tab-active': activeKey === element.id }"
                @click="activeKey = element.id"
              >
                <div
                  role="tab"
                  aria-selected="true"
                  class="ant-tabs-tab-btn"
                  tabindex="0"
                  id="rc-tabs-12-tab-1"
                  aria-controls="rc-tabs-12-panel-1"
                >
                  {{ element.name }}
                </div>
              </div>
            </template>
          </draggable>
        </div>
      </div>
    </template>
  </a-tabs>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import draggable from 'vuedraggable'

const activeKey = ref<number>(0)

const state = reactive({
  list: [
    {
      id: 0,
      name: '张三',
      age: 18
    },
    {
      id: 1,
      name: '李四',
      age: 20
    },
    {
      id: 2,
      name: '王五',
      age: 24
    }
  ]
})
</script>

解释

  • 首先需要自定义页签头
    <!-- 基础部分 -->
    <a-tab-pane key="1" tab="Tab 1">Content of Tab Pane 1</a-tab-pane>
    <a-tab-pane key="2" tab="Tab 2">Content of Tab Pane 2</a-tab-pane>
    
    <!-- 自定义表头部分 -->
     <template #renderTabBar="{ DefaultTabBar, ...props }">
       <!-- 自定义内容 -->
     </template>
    
  • 通过F12审查元素,将页头的样式复制一份
    • 将复制的代码放在上述的自定义内容中
  • 确定你需要拖拽的部分,并进行适当的调整
    • 将需要拖拽的部分,放在<draggable>标签中
    • 标签的使用,可参考上述准备>参考指南
  • 需要注意
    • class的激活样式::class="{ 'ant-tabs-tab-active': activeKey === element.id }"
    • 仅需写一个<a-tab-pane>标签,其余均通过循环展示

———————————————————————
PS:个人总结,仅供参考,不足之处,欢迎评论指教。

Ant Design Vue 中的 Tabs 组件可以与 Vue Router 配合使用,实现标签栏的功能。具体步骤如下: 1. 在路由配置中,添加 `meta` 字段用于标识当前路由是否需要在标签栏中显示,例如: ```javascript const routes = [ { path: '/', name: 'Home', component: Home, meta: { title: '首页', keepAlive: true, // 是否缓存组件 showTab: true, // 是否在标签栏中显示 }, }, // ... ]; ``` 2. 在 App.vue 中,使用 Tabs 组件来渲染标签栏,并使用 `router-view` 组件来渲染当前路由对应的组件: ```html <template> <div> <a-tabs v-model:selectedTabKey="selectedTabKey" type="editable-card" hide-add @edit="handleTabEdit" style="margin: 0 24px;"> <a-tab-pane v-for="(tab, index) in tabs" :key="tab.key" :tab="tab.title" :closable="index !== 0" @close="handleTabClose(index)"> </a-tab-pane> </a-tabs> <router-view /> </div> </template> <script> export default { data() { return { selectedTabKey: '/', tabs: [ { key: '/', title: '首页', }, ], }; }, created() { const { path, meta } = this.$route; if (meta.showTab) { this.selectedTabKey = path; this.addTab(path, meta.title); } }, methods: { addTab(key, title) { const index = this.tabs.findIndex((tab) => tab.key === key); if (index === -1) { this.tabs.push({ key, title, }); } }, removeTab(key) { const index = this.tabs.findIndex((tab) => tab.key === key); if (index !== -1) { this.tabs.splice(index, 1); } }, handleTabClose(index) { const { key } = this.tabs[index]; this.removeTab(key); this.$router.replace(this.tabs[this.tabs.length - 1].key); }, handleTabEdit(targetKey, action) { if (action === 'add') { this.$router.push(targetKey); } else if (action === 'remove') { this.handleTabClose(this.tabs.findIndex((tab) => tab.key === targetKey)); } }, }, }; </script> ``` 在这个示例中,我们使用了 `selectedTabKey` 属性来绑定当前选中的标签页,使用 `tabs` 数组来存储所有已打开的标签页。在 `created` 钩子函数中,我们通过判断当前路由的 `meta.showTab` 字段来决定是否需要添加标签页。在 `addTab` 方法中,我们使用 `tabs` 数组来存储已打开的标签页,防止重复添加。在 `removeTab` 方法中,我们使用 `tabs` 数组来删除已关闭的标签页。在 `handleTabEdit` 方法中,我们通过判断用户的操作来决定是添加标签页还是关闭标签页。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值