当前位置:首页 > VUE

vue table实现折叠

2026-03-29 06:36:30VUE

vue table 实现折叠

使用 el-tableexpand 功能结合 scoped slot 实现折叠展开效果。以下是一个完整示例:

<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column type="expand">
      <template #default="props">
        <p>姓名: {{ props.row.name }}</p>
        <p>地址: {{ props.row.address }}</p>
      </template>
    </el-table-column>
    <el-table-column label="日期" prop="date"></el-table-column>
    <el-table-column label="姓名" prop="name"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        },
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }
      ]
    }
  }
}
</script>

自定义折叠图标

通过 expand-change 事件和 row-key 属性控制折叠状态:

<template>
  <el-table 
    :data="tableData" 
    row-key="id"
    @expand-change="handleExpand"
  >
    <el-table-column type="expand">
      <template #default="props">
        <div v-if="props.row.expand">
          <p>详细信息: {{ props.row.detail }}</p>
        </div>
      </template>
    </el-table-column>
    <!-- 其他列 -->
  </el-table>
</template>

<script>
export default {
  methods: {
    handleExpand(row, expandedRows) {
      console.log(row, expandedRows)
    }
  }
}
</script>

手风琴效果折叠

只允许同时展开一行:

<template>
  <el-table 
    :data="tableData" 
    :row-key="row => row.id"
    @expand-change="handleExpand"
  >
    <!-- 列定义 -->
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      currentExpandRow: null
    }
  },
  methods: {
    handleExpand(row, expandedRows) {
      if (expandedRows.length > 1) {
        this.$refs.table.toggleRowExpansion(expandedRows[0])
      }
      this.currentExpandRow = row
    }
  }
}
</script>

动态加载折叠内容

展开时异步加载数据:

vue table实现折叠

<template>
  <el-table :data="tableData">
    <el-table-column type="expand">
      <template #default="props">
        <div v-if="props.row.loaded">
          <p>{{ props.row.detail }}</p>
        </div>
        <div v-else>加载中...</div>
      </template>
    </el-table-column>
    <!-- 其他列 -->
  </el-table>
</template>

<script>
export default {
  methods: {
    loadDetail(row) {
      // 模拟异步请求
      setTimeout(() => {
        row.detail = '这是动态加载的详细内容'
        row.loaded = true
        this.$forceUpdate()
      }, 1000)
    },
    handleExpand(row) {
      if (!row.loaded) {
        this.loadDetail(row)
      }
    }
  }
}
</script>

注意事项

  1. 使用 row-key 确保每行有唯一标识
  2. 动态数据更新后可能需要调用 $forceUpdate()
  3. 折叠内容样式可通过 CSS 自定义
  4. 大量数据时考虑性能优化

以上实现方式适用于 Element UI 的表格组件,其他 UI 库实现原理类似,具体 API 可能有所不同。

标签: vuetable
分享给朋友:

相关文章

vue实现导航切换内容

vue实现导航切换内容

Vue实现导航切换内容的方法 在Vue中实现导航切换内容,可以通过以下方式实现: 使用动态组件 动态组件通过<component :is="currentComponent">实现内容切…

vue实现图库

vue实现图库

Vue 实现图库的基本方法 使用 Vue 实现图库功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用 Vue 和第三方库(如 Vue Gallery) 安装 vue-gallery 或…

vue 实现滚动

vue 实现滚动

实现滚动的基本方法 在Vue中实现滚动效果可以通过多种方式完成,包括使用原生JavaScript、CSS或第三方库。以下是一些常见的方法: 使用window.scrollTo方法实现页面滚动 可以通…

实现vue table

实现vue table

Vue 表格实现方法 基础表格实现 使用 Vue 的 v-for 指令可以快速渲染表格数据。在模板中定义表格结构,通过数据绑定动态生成行和列。 <template> <tabl…

vue实现github

vue实现github

Vue 实现 GitHub 相关功能 创建 GitHub API 请求 在 Vue 项目中,可以使用 axios 或 fetch 与 GitHub API 进行交互。需要在 GitHub 上创建个人访…

vue  实现分页

vue 实现分页

实现分页的基本思路 在Vue中实现分页通常需要结合后端API返回的数据和前端的分页组件。分页的核心逻辑包括计算总页数、处理当前页码变化以及触发数据请求。 分页组件示例 使用Element UI的分页…