当前位置:首页 > 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实现签约合作弹窗可以通过组件化方式完成,结合模态框和表单交互逻辑。以下是具体实现方法: 1. 创建弹窗组件 新建一个Vue组件文件(如SignContractDia…

vue实现drag

vue实现drag

Vue 实现拖拽功能的方法 在 Vue 中实现拖拽功能可以通过原生 HTML5 的拖拽 API 或第三方库如 vuedraggable 来完成。以下是两种常见方法的实现方式。 使用 HTML5 拖拽…

vue实现oauth

vue实现oauth

Vue 实现 OAuth 的步骤 安装必要的依赖 确保项目中安装了 axios 和 vue-router。OAuth 通常需要发送 HTTP 请求和处理路由跳转。 npm install axios…

vue实现slidetoggle

vue实现slidetoggle

Vue 实现 SlideToggle 效果 SlideToggle 是一种常见的交互效果,元素以滑动方式展开或收起。以下是几种实现方法: 使用 CSS Transition 和 v-show 通过…

vue工序实现

vue工序实现

Vue 工序实现方法 在Vue中实现工序(或流程)功能,通常涉及状态管理、组件通信和动态渲染。以下是几种常见方法: 使用动态组件 通过Vue的<component :is="currentCo…

vue sku 实现

vue sku 实现

Vue SKU 实现方案 在电商系统中,SKU(Stock Keeping Unit)是商品的最小库存单位,通常由多个属性组合而成(如颜色、尺寸等)。以下是基于 Vue 的实现方案。 数据结构设计…