当前位置:首页 > VUE

vue table实现折叠

2026-03-08 19:30:19VUE

Vue 表格折叠实现方法

使用 Element UI 的 el-table 组件

Element UI 的 el-table 组件内置了展开行功能,通过 type="expand"scoped-slot 实现。

<template>
  <el-table :data="tableData">
    <el-table-column type="expand">
      <template #default="props">
        <p>详细信息: {{ props.row.desc }}</p>
      </template>
    </el-table-column>
    <el-table-column prop="name" label="姓名"></el-table-column>
    <el-table-column prop="age" label="年龄"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 20, desc: '这是张三的详细信息' },
        { name: '李四', age: 22, desc: '这是李四的详细信息' }
      ]
    }
  }
}
</script>

自定义折叠实现

对于非 Element UI 项目,可以通过 v-show/v-if 和点击事件实现折叠效果。

<template>
  <table>
    <thead>
      <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="item in tableData" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
        <td>
          <button @click="toggleExpand(item.id)">
            {{ expandedRows.includes(item.id) ? '收起' : '展开' }}
          </button>
        </td>
      </tr>
      <tr v-for="item in tableData" 
          v-show="expandedRows.includes(item.id)"
          :key="'detail-' + item.id">
        <td colspan="3">
          {{ item.desc }}
        </td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: '张三', age: 20, desc: '详细信息...' },
        { id: 2, name: '李四', age: 22, desc: '详细信息...' }
      ],
      expandedRows: []
    }
  },
  methods: {
    toggleExpand(id) {
      const index = this.expandedRows.indexOf(id)
      if (index > -1) {
        this.expandedRows.splice(index, 1)
      } else {
        this.expandedRows.push(id)
      }
    }
  }
}
</script>

使用第三方组件库

一些 Vue 表格组件库如 VxeTable、Ant Design Vue 等也提供了折叠功能:

VxeTable 示例:

<template>
  <vxe-table :data="tableData">
    <vxe-column type="expand" width="60">
      <template #content="{ row }">
        <div>{{ row.desc }}</div>
      </template>
    </vxe-column>
    <vxe-column field="name" title="姓名"></vxe-column>
    <vxe-column field="age" title="年龄"></vxe-column>
  </vxe-table>
</template>

动画过渡效果

为折叠添加动画效果,可以使用 Vue 的 transition 组件:

vue table实现折叠

<transition name="fade">
  <tr v-if="expandedRows.includes(item.id)">
    <td colspan="3">
      {{ item.desc }}
    </td>
  </tr>
</transition>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity .5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

标签: vuetable
分享给朋友:

相关文章

vue实现atm

vue实现atm

Vue实现ATM机模拟功能 使用Vue.js实现ATM机模拟功能需要结合组件化开发、状态管理以及用户交互逻辑。以下是关键实现步骤: 核心组件结构 创建以下Vue组件构建ATM界面: Car…

vue实现switch

vue实现switch

Vue 实现 Switch 开关组件 在 Vue 中实现 Switch 开关组件可以通过自定义组件或使用现有 UI 库完成。以下是几种常见实现方式: 自定义 Switch 组件 创建一个基础 Sw…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue实现cs

vue实现cs

Vue 实现 CS(Client-Side)渲染的常见方法 Vue.js 是一个用于构建用户界面的渐进式框架,默认采用客户端渲染(CSR)。以下是几种常见的实现方式: 使用 Vue CLI 创建项目…

vue实现视频

vue实现视频

Vue 实现视频播放的方法 在 Vue 项目中实现视频播放可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 的 <video> 标签 在 Vue 组件中直接使用 HTML…

vue实现京东

vue实现京东

Vue 实现京东电商网站的关键步骤 项目搭建与基础配置 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 管理路由,Vuex/Pinia 管理状态。配置基础 UI 框架如 E…