当前位置:首页 > VUE

vue 实现table切换

2026-01-16 00:31:10VUE

实现 Vue 表格切换功能

在 Vue 中实现表格切换功能通常涉及动态渲染不同的表格数据或切换不同的表格样式。以下是几种常见的实现方式:

动态数据切换

通过改变表格绑定的数据源实现切换:

vue 实现table切换

<template>
  <div>
    <button @click="currentTable = 'table1'">表格1</button>
    <button @click="currentTable = 'table2'">表格2</button>

    <table>
      <thead>
        <tr>
          <th v-for="col in columns" :key="col">{{ col }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="row in activeData" :key="row.id">
          <td v-for="col in columns" :key="col">{{ row[col] }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentTable: 'table1',
      table1Data: [...],
      table2Data: [...],
      columns: ['id', 'name', 'age']
    }
  },
  computed: {
    activeData() {
      return this.currentTable === 'table1' ? this.table1Data : this.table2Data
    }
  }
}
</script>

组件切换方式

使用动态组件切换不同的表格组件:

<template>
  <div>
    <button @click="currentComponent = 'TableA'">表格A</button>
    <button @click="currentComponent = 'TableB'">表格B</button>

    <component :is="currentComponent" :data="tableData"/>
  </div>
</template>

<script>
import TableA from './TableA.vue'
import TableB from './TableB.vue'

export default {
  components: { TableA, TableB },
  data() {
    return {
      currentComponent: 'TableA',
      tableData: [...]
    }
  }
}
</script>

条件渲染

使用 v-if/v-show 控制不同表格的显示:

vue 实现table切换

<template>
  <div>
    <button @click="showTable1 = true; showTable2 = false">表格1</button>
    <button @click="showTable1 = false; showTable2 = true">表格2</button>

    <table v-if="showTable1">
      <!-- 表格1内容 -->
    </table>

    <table v-if="showTable2">
      <!-- 表格2内容 -->
    </table>
  </div>
</template>

使用第三方表格组件

结合 Element UI 或 Ant Design Vue 等 UI 库实现:

<template>
  <div>
    <el-radio-group v-model="currentTable">
      <el-radio-button label="table1">表格1</el-radio-button>
      <el-radio-button label="table2">表格2</el-radio-button>
    </el-radio-group>

    <el-table :data="activeData">
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
    </el-table>
  </div>
</template>

带过渡动画的切换

为表格切换添加过渡效果:

<template>
  <div>
    <transition name="fade" mode="out-in">
      <table v-if="showTable1" key="table1">
        <!-- 表格1内容 -->
      </table>
      <table v-else key="table2">
        <!-- 表格2内容 -->
      </table>
    </transition>
  </div>
</template>

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

选择哪种实现方式取决于具体需求。动态数据切换适合数据结构相同的表格,组件切换适合结构差异大的表格,条件渲染适合简单的切换场景,第三方组件能快速实现专业效果。

标签: vuetable
分享给朋友:

相关文章

vue实现单页面

vue实现单页面

Vue 实现单页面应用(SPA) Vue.js 是一个渐进式框架,非常适合构建单页面应用(SPA)。SPA 的核心特点是整个应用只有一个 HTML 页面,通过动态加载内容实现页面切换,无需重新加载整个…

vue如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed:…

vue实现签约合作弹窗

vue实现签约合作弹窗

实现签约合作弹窗的步骤 使用Vue实现签约合作弹窗可以通过组件化方式完成,结合模态框和表单交互逻辑。以下是具体实现方法: 1. 创建弹窗组件 新建一个Vue组件文件(如SignContractDi…

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交…

vue实现菜单栏锚点

vue实现菜单栏锚点

实现锚点菜单的基本思路 在Vue中实现菜单栏锚点功能,主要涉及两个方面:创建可跳转的锚点位置,以及菜单项的点击滚动控制。通过监听滚动事件可以高亮当前可见区域的对应菜单项。 创建页面锚点位置 在需要跳…

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-se…