当前位置:首页 > VUE

vue 实现tab表格

2026-03-30 04:10:40VUE

Vue 实现 Tab 表格

基础结构设计

使用 Vue 的 v-for 动态生成 Tab 标题,配合 v-showv-if 控制表格内容的显示切换。以下为示例代码框架:

<template>
  <div>
    <div class="tabs">
      <button 
        v-for="(tab, index) in tabs" 
        :key="index"
        @click="activeTab = index"
        :class="{ 'active': activeTab === index }"
      >
        {{ tab.title }}
      </button>
    </div>

    <div class="tab-content">
      <table v-for="(tab, index) in tabs" :key="index" v-show="activeTab === index">
        <thead>
          <tr>
            <th v-for="(header, i) in tab.headers" :key="i">{{ header }}</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(row, rowIndex) in tab.data" :key="rowIndex">
            <td v-for="(cell, cellIndex) in row" :key="cellIndex">{{ cell }}</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

数据绑定

在 Vue 实例的 data 中定义动态数据:

<script>
export default {
  data() {
    return {
      activeTab: 0,
      tabs: [
        {
          title: '表格1',
          headers: ['姓名', '年龄'],
          data: [
            ['张三', 25],
            ['李四', 30]
          ]
        },
        {
          title: '表格2',
          headers: ['产品', '价格'],
          data: [
            ['手机', 2999],
            ['电脑', 5999]
          ]
        }
      ]
    }
  }
}
</script>

样式优化

通过 CSS 增强交互视觉效果:

<style scoped>
.tabs button {
  padding: 8px 16px;
  margin-right: 5px;
  border: none;
  cursor: pointer;
}
.tabs button.active {
  background-color: #42b983;
  color: white;
}
table {
  width: 100%;
  border-collapse: collapse;
}
th, td {
  border: 1px solid #ddd;
  padding: 8px;
}
</style>

动态数据加载

如需从接口异步加载数据,可在 mounted 或方法中处理:

methods: {
  async fetchData() {
    const response = await axios.get('/api/tab-data');
    this.tabs = response.data;
  }
},
mounted() {
  this.fetchData();
}

组件化拆分

对于复杂场景,可将 Tab 和 Table 拆分为独立组件:

vue 实现tab表格

<template>
  <tab-container :tabs="tabs">
    <tab-content v-for="(tab, index) in tabs" :key="index">
      <data-table :headers="tab.headers" :rows="tab.data" />
    </tab-content>
  </tab-container>
</template>

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

相关文章

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tabl…

vue实现弹窗

vue实现弹窗

Vue 实现弹窗的常见方法 使用组件化方式封装弹窗 创建一个独立的弹窗组件(如 Modal.vue),通过 v-if 或 v-show 控制显示隐藏: <template> <…

vue实现selectable

vue实现selectable

Vue 实现可选择的 Select 组件 在 Vue 中实现一个可选择的 Select 组件,可以通过以下几种方式: 使用原生 HTML select 元素 <template> &…

vue实现弹幕

vue实现弹幕

Vue 实现弹幕功能 弹幕功能通常包括动态生成、滚动显示、颜色和速度随机化等特性。以下是基于 Vue 3 的实现方法。 核心思路 使用 CSS 动画控制弹幕从右向左滚动。 动态生成弹幕数据,随机设置…

vue mvvm 实现

vue mvvm 实现

Vue MVVM 实现原理 Vue.js 的 MVVM(Model-View-ViewModel)架构通过数据绑定和响应式系统实现视图与数据的自动同步。以下是其核心实现机制: 数据劫持与响应式系统…

vue实现popper

vue实现popper

Vue 实现 Popper 的方法 使用 Tippy.js 库 Tippy.js 是一个轻量级的 Popper.js 封装库,提供丰富的工具提示功能。安装 Tippy.js 及其 Vue 封装: n…