当前位置:首页 > 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实现发送值接受值

vue实现发送值接受值

Vue 实现父子组件传值 父组件向子组件传值(props) 父组件通过 v-bind 或简写 : 向子组件传递数据。子组件通过 props 接收数据。 父组件代码: <template&g…

vue实现高亮

vue实现高亮

Vue 实现文本高亮的方法 在 Vue 中实现文本高亮可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-html 指令 通过 v-html 指令可以动态插入 HTML 内容,将需要高…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…

vue实现导入

vue实现导入

Vue 实现文件导入功能 使用 <input type="file"> 元素 在 Vue 模板中添加一个文件输入元素,绑定 change 事件处理函数。 <template>…

vue最佳实现

vue最佳实现

Vue 最佳实践 组件设计 组件应遵循单一职责原则,每个组件只负责一个功能模块。命名采用 PascalCase(如 UserProfile.vue),模板中使用 kebab-case(如 <us…

vue实现 弹窗

vue实现 弹窗

Vue 实现弹窗的基本方法 使用 Vue 实现弹窗可以通过多种方式,包括组件化、指令、插件等。以下是几种常见的实现方法。 组件化实现弹窗 创建一个独立的弹窗组件,通过 props 控制显示与隐藏。…