当前位置:首页 > VUE

vue实现表格收缩展开

2026-01-21 19:02:24VUE

Vue 表格展开/收缩实现方法

使用 v-ifv-show 控制显隐

通过数据驱动的方式控制行的展开与收缩状态,结合 Vue 的指令实现动态渲染。

<template>
  <table>
    <tr v-for="item in tableData" :key="item.id">
      <td>{{ item.name }}</td>
      <td>
        <button @click="toggleExpand(item)">
          {{ item.expanded ? '收起' : '展开' }}
        </button>
      </td>
    </tr>
    <!-- 展开内容 -->
    <tr v-if="item.expanded" v-for="item in tableData" :key="'detail-' + item.id">
      <td colspan="2">{{ item.details }}</td>
    </tr>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: '项目1', details: '详细内容...', expanded: false },
        // 其他数据...
      ]
    }
  },
  methods: {
    toggleExpand(item) {
      item.expanded = !item.expanded
    }
  }
}
</script>

使用 Element UI 的展开行功能

如果使用 Element UI 组件库,可直接利用其内置的展开行功能。

<template>
  <el-table
    :data="tableData"
    style="width: 100%">
    <el-table-column type="expand">
      <template #default="props">
        <p>详情: {{ props.row.details }}</p>
      </template>
    </el-table-column>
    <el-table-column prop="name" label="名称"/>
  </el-table>
</template>

动态嵌套表格实现多级展开

通过递归组件实现多级嵌套展开效果。

<template>
  <table>
    <TableRow 
      v-for="item in tableData" 
      :key="item.id" 
      :row-data="item"
      @toggle="onToggle"/>
  </table>
</template>

<!-- TableRow.vue 组件 -->
<template>
  <tr>
    <td>{{ rowData.name }}</td>
    <td>
      <button @click="$emit('toggle', rowData.id)">
        {{ rowData.expanded ? '收起' : '展开' }}
      </button>
    </td>
  </tr>
  <tr v-if="rowData.expanded">
    <td colspan="2">
      <!-- 嵌套内容或子表格 -->
      <table v-if="rowData.children">
        <TableRow 
          v-for="child in rowData.children"
          :key="child.id"
          :row-data="child"
          @toggle="$emit('toggle', child.id)"/>
      </table>
    </td>
  </tr>
</template>

动画过渡效果

添加 Vue 的过渡效果使展开/收缩更平滑。

vue实现表格收缩展开

<transition name="slide">
  <tr v-if="item.expanded">
    <td colspan="2">{{ item.details }}</td>
  </tr>
</transition>

<style>
.slide-enter-active, .slide-leave-active {
  transition: all 0.3s ease;
  max-height: 100px;
}
.slide-enter, .slide-leave-to {
  opacity: 0;
  max-height: 0;
}
</style>

实现要点

  • 数据驱动:通过修改数据项的 expanded 属性控制展开状态
  • 性能优化:大数据量时建议使用虚拟滚动
  • 组件化:复杂结构建议拆分为子组件
  • 无障碍:确保按钮有明确的 ARIA 标签

以上方法可根据具体需求组合使用,Element UI 等组件库提供了开箱即用的解决方案,自定义实现则更灵活可控。

标签: 表格vue
分享给朋友:

相关文章

vue实现级联

vue实现级联

Vue 实现级联选择器的方法 级联选择器(Cascader)是常见的表单组件,用于处理多层级联动数据选择(如省市区选择)。以下是几种实现方式: 基于 Element UI 的 el-cascader…

vue实现通知

vue实现通知

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

javascript实现vue

javascript实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,主要包括数据绑定、响应式系统和虚拟 DOM。以下是一个简化的实现示例。 数据响应式系统 通过 Object.de…

vue实现 单选

vue实现 单选

实现 Vue 单选功能 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 可以轻松实现单选功能,将单选按钮的值绑定到同…

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…

mvvm实现vue

mvvm实现vue

MVVM 模式在 Vue 中的实现 MVVM(Model-View-ViewModel)是一种前端架构模式,Vue 的设计灵感来源于此。以下是 MVVM 在 Vue 中的核心实现方式: 数据绑定(M…