当前位置:首页 > VUE

vue主从表实现

2026-03-29 18:38:55VUE

Vue 主从表实现方法

基础数据结构设计

主表数据通常是一个数组,每个主表项包含唯一标识符(如id)和关联的子表数据字段:

data() {
  return {
    masterData: [
      { id: 1, name: '主项1', details: [{ subId: 1, content: '子项1' }] },
      { id: 2, name: '主项2', details: [] }
    ],
    activeMasterId: null
  }
}

主表渲染

使用v-for循环渲染主表,通过点击事件设置当前激活的主表ID:

<ul>
  <li v-for="item in masterData" 
      :key="item.id"
      @click="activeMasterId = item.id"
      :class="{ active: activeMasterId === item.id }">
    {{ item.name }}
  </li>
</ul>

从表动态显示

根据当前激活的主表ID过滤显示对应的子表数据:

<div v-if="activeMasterId">
  <table>
    <tr v-for="(detail, index) in getCurrentDetails()" :key="index">
      <td>{{ detail.content }}</td>
    </tr>
  </table>
</div>

数据联动方法

添加计算属性或方法获取当前主表对应的子表数据:

vue主从表实现

methods: {
  getCurrentDetails() {
    const master = this.masterData.find(item => item.id === this.activeMasterId)
    return master ? master.details : []
  }
}

完整组件示例

<template>
  <div class="master-detail">
    <div class="master-list">
      <h3>主表</h3>
      <ul>
        <li v-for="item in masterData" 
            @click="selectMaster(item.id)"
            :class="{ active: activeMasterId === item.id }">
          {{ item.name }}
        </li>
      </ul>
    </div>

    <div class="detail-list" v-if="activeMasterId">
      <h3>子表</h3>
      <button @click="addDetail">添加子项</button>
      <table>
        <tr v-for="(detail, index) in currentDetails" :key="detail.subId">
          <td>{{ detail.content }}</td>
          <td><button @click="removeDetail(index)">删除</button></td>
        </tr>
      </table>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      masterData: [
        { id: 1, name: '项目A', details: [
          { subId: 101, content: '子项A1' },
          { subId: 102, content: '子项A2' }
        ]},
        { id: 2, name: '项目B', details: [] }
      ],
      activeMasterId: null
    }
  },
  computed: {
    currentDetails() {
      const master = this.masterData.find(item => item.id === this.activeMasterId)
      return master ? master.details : []
    }
  },
  methods: {
    selectMaster(id) {
      this.activeMasterId = id
    },
    addDetail() {
      const master = this.masterData.find(item => item.id === this.activeMasterId)
      if (master) {
        const newId = Math.max(...master.details.map(d => d.subId), 0) + 1
        master.details.push({
          subId: newId,
          content: `新子项${newId}`
        })
      }
    },
    removeDetail(index) {
      const master = this.masterData.find(item => item.id === this.activeMasterId)
      if (master) {
        master.details.splice(index, 1)
      }
    }
  }
}
</script>

<style>
.master-detail { display: flex; }
.master-list { width: 200px; }
.detail-list { flex: 1; }
.active { background-color: #eee; }
</style>

关键实现要点

  1. 数据双向绑定:主表和子表数据都通过Vue的响应式系统管理
  2. 状态管理:使用activeMasterId跟踪当前选中的主表项
  3. 组件通信:子表操作通过方法直接修改主表数据中的对应数组
  4. 唯一标识:主表和子表都需要稳定的key标识(如idsubId

进阶优化方向

  1. 使用Vuex管理复杂状态
  2. 添加表单验证功能
  3. 实现本地缓存或API数据持久化
  4. 添加分页加载功能
  5. 使用动态组件实现更灵活的布局

标签: 主从vue
分享给朋友:

相关文章

vue怎么实现数据检测

vue怎么实现数据检测

Vue 数据检测的实现方式 Vue 通过响应式系统实现数据检测,核心机制基于 Object.defineProperty(Vue 2)或 Proxy(Vue 3)。以下是具体实现方法: 基于 Vue…

vue实现xss

vue实现xss

XSS 攻击简介 XSS(跨站脚本攻击)是一种常见的安全漏洞,攻击者通过注入恶意脚本到网页中,当其他用户访问该页面时,脚本会在其浏览器中执行,可能导致数据泄露或会话劫持。 Vue 中的 XSS…

vue能实现

vue能实现

Vue.js 是一个流行的前端框架,能够实现多种功能。以下是 Vue 能够实现的主要功能和特性: 响应式数据绑定 Vue 的核心特性之一是响应式数据绑定。通过数据驱动视图,当数据发生变化时,视图会自…

vue实现博客

vue实现博客

Vue 实现博客的基本步骤 使用 Vue.js 实现一个博客系统可以分为前端和后端两部分。以下是基于 Vue 的前端实现方案,后端可以选择 Node.js、Django 或其他框架。 项目初始化 使…

vue实现portal

vue实现portal

Vue 实现 Portal 功能 Portal 是一种将子节点渲染到父组件 DOM 层级之外的 DOM 节点的技术,常用于实现模态框、弹出层等需要脱离当前组件层级的场景。Vue 可以通过多种方式实现…

vue拖动实现

vue拖动实现

实现 Vue 拖动功能的方法 使用 HTML5 拖放 API 在 Vue 中可以利用 HTML5 的原生拖放 API 实现拖动功能。通过 draggable 属性标记可拖动元素,结合 @dragsta…