当前位置:首页 > VUE

vue 实现table切换

2026-02-17 01:48:19VUE

Vue 实现 Table 切换的方法

使用 v-if 或 v-show 切换不同表格

通过条件渲染指令 v-ifv-show 可以快速切换不同的表格组件。v-if 是惰性的,适合切换频率低的场景;v-show 通过 CSS 控制显示,适合频繁切换。

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

    <table1 v-if="currentTable === 'table1'" />
    <table2 v-if="currentTable === 'table2'" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentTable: 'table1'
    }
  }
}
</script>

动态组件实现表格切换

使用 Vue 的 <component> 动态组件可以更灵活地切换表格,适合需要根据条件动态渲染不同组件的场景。

vue 实现table切换

<template>
  <div>
    <button @click="currentComponent = 'Table1'">表格1</button>
    <button @click="currentComponent = 'Table2'">表格2</button>

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

<script>
import Table1 from './Table1.vue'
import Table2 from './Table2.vue'

export default {
  components: { Table1, Table2 },
  data() {
    return {
      currentComponent: 'Table1'
    }
  }
}
</script>

通过路由切换表格页面

如果表格需要作为独立页面展示,可以使用 Vue Router 实现路由级别的切换。

vue 实现table切换

// router.js
const routes = [
  { path: '/table1', component: Table1 },
  { path: '/table2', component: Table2 }
]
<template>
  <div>
    <router-link to="/table1">表格1</router-link>
    <router-link to="/table2">表格2</router-link>

    <router-view></router-view>
  </div>
</template>

同一表格动态切换数据源

保持表格组件不变,仅通过改变数据源实现内容切换,适合表格结构相同但数据不同的场景。

<template>
  <div>
    <button @click="loadData('data1')">加载数据1</button>
    <button @click="loadData('data2')">加载数据2</button>

    <table>
      <tr v-for="item in tableData" :key="item.id">
        <td>{{ item.name }}</td>
      </tr>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      dataSources: {
        data1: [{ id: 1, name: '数据1' }],
        data2: [{ id: 2, name: '数据2' }]
      }
    }
  },
  methods: {
    loadData(source) {
      this.tableData = this.dataSources[source]
    }
  }
}
</script>

使用第三方表格组件切换

对于 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 v-if="currentTable === 'table1'" :data="data1"></el-table>
    <el-table v-if="currentTable === 'table2'" :data="data2"></el-table>
  </div>
</template>

以上方法可根据实际需求选择使用,简单场景推荐使用条件渲染,复杂场景可考虑动态组件或路由方案。数据驱动的方式在维护性和性能上通常更有优势。

标签: vuetable
分享给朋友:

相关文章

vue实现画图

vue实现画图

Vue 实现画图功能 在 Vue 中实现画图功能通常需要结合 HTML5 的 <canvas> 元素或第三方库。以下是几种常见的方法: 使用原生 Canvas API 通过 Vue 直接…

vue插件实现

vue插件实现

Vue 插件实现方法 Vue 插件是一种向 Vue 应用程序添加全局功能的机制。插件可以包含全局指令、过滤器、混入、实例方法等。 插件的基本结构 一个 Vue 插件通常是一个对象或函数,需要暴露一个…

vue 实现工具

vue 实现工具

Vue 实现工具的方法 使用 Vue 实现工具功能可以通过多种方式完成,具体取决于工具的类型和需求。以下是几种常见的方法: 组件化实现 创建一个独立的 Vue 组件来封装工具功能。例如,实现一个计…

vue实现首页

vue实现首页

Vue 实现首页的基本步骤 安装 Vue.js 和相关依赖,可以通过 Vue CLI 或 Vite 快速初始化项目。Vue CLI 适合传统项目,Vite 更适合现代前端开发。 npm instal…

vue router实现分页

vue router实现分页

Vue Router 实现分页的方法 在 Vue.js 中,可以通过 Vue Router 实现分页功能,通常需要结合路由参数和动态路由匹配。以下是几种常见的实现方式: 使用查询参数实现分页 在路由…

vue实现登录退出

vue实现登录退出

实现登录功能 在Vue中实现登录功能通常需要结合表单验证、API请求和状态管理。以下是一个基础实现示例: 安装必要依赖(如axios和vuex): npm install axios vuex 创…