当前位置:首页 > VUE

vue实现表格树

2026-03-29 04:08:56VUE

Vue 实现表格树的方法

在 Vue 中实现表格树可以通过递归组件或第三方库(如 Element UI 或 Ant Design Vue)来完成。以下是几种常见的实现方式:

使用递归组件

递归组件是 Vue 中实现树形结构的常见方式。通过组件调用自身,可以轻松实现多层嵌套的表格树。

vue实现表格树

<template>
  <table>
    <thead>
      <tr>
        <th>名称</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <tree-node v-for="node in treeData" :key="node.id" :node="node"></tree-node>
    </tbody>
  </table>
</template>

<script>
import TreeNode from './TreeNode.vue';

export default {
  components: { TreeNode },
  data() {
    return {
      treeData: [
        {
          id: 1,
          name: '节点1',
          children: [
            { id: 2, name: '子节点1' },
            { id: 3, name: '子节点2' }
          ]
        }
      ]
    };
  }
};
</script>

TreeNode.vue 中递归调用自身:

vue实现表格树

<template>
  <tr>
    <td>{{ node.name }}</td>
    <td>
      <button @click="toggleChildren">展开/折叠</button>
    </td>
  </tr>
  <tr v-if="showChildren && node.children">
    <td colspan="2">
      <table>
        <tbody>
          <tree-node 
            v-for="child in node.children" 
            :key="child.id" 
            :node="child"
          ></tree-node>
        </tbody>
      </table>
    </td>
  </tr>
</template>

<script>
export default {
  name: 'TreeNode',
  props: ['node'],
  data() {
    return {
      showChildren: false
    };
  },
  methods: {
    toggleChildren() {
      this.showChildren = !this.showChildren;
    }
  }
};
</script>

使用 Element UI 的树形表格

Element UI 提供了 el-tableel-tree 组件,可以结合使用实现树形表格。

<template>
  <el-table
    :data="tableData"
    row-key="id"
    :tree-props="{ children: 'children' }"
  >
    <el-table-column prop="name" label="名称"></el-table-column>
    <el-table-column prop="operation" label="操作"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        {
          id: 1,
          name: '节点1',
          children: [
            { id: 2, name: '子节点1' },
            { id: 3, name: '子节点2' }
          ]
        }
      ]
    };
  }
};
</script>

使用 Ant Design Vue 的树形表格

Ant Design Vue 的 a-table 组件也支持树形表格功能。

<template>
  <a-table
    :columns="columns"
    :data-source="data"
    :row-key="record => record.id"
    :pagination="false"
    :expand-icon-column-index="1"
  >
    <template #operation>
      <a-button>操作</a-button>
    </template>
  </a-table>
</template>

<script>
export default {
  data() {
    return {
      columns: [
        { title: '名称', dataIndex: 'name' },
        { title: '操作', slots: { customRender: 'operation' } }
      ],
      data: [
        {
          id: 1,
          name: '节点1',
          children: [
            { id: 2, name: '子节点1' },
            { id: 3, name: '子节点2' }
          ]
        }
      ]
    };
  }
};
</script>

注意事项

  • 递归组件需要注意性能问题,避免过深的嵌套层级。
  • 使用第三方库时,确保引入正确的版本和样式文件。
  • 树形表格的数据结构通常包含 children 字段,用于表示子节点。

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

相关文章

vue alert实现

vue alert实现

使用 Vue 实现 Alert 组件 在 Vue 中实现 Alert 组件可以通过自定义组件或结合第三方库完成。以下是几种常见方法: 自定义 Alert 组件 创建一个可复用的 Alert 组件,通…

通过vue实现

通过vue实现

Vue 实现方法 安装 Vue 确保已安装 Node.js 和 npm,通过以下命令安装 Vue CLI: npm install -g @vue/cli 创建 Vue 项目 使用 Vue CLI…

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…

vue实现翻页

vue实现翻页

Vue实现翻页功能 在Vue中实现翻页功能通常需要结合分页组件和数据请求逻辑。以下是常见的实现方法: 使用Element UI分页组件 安装Element UI后,可以直接使用其分页组件: <…

mvvm实现vue

mvvm实现vue

MVVM 模式在 Vue 中的实现 MVVM(Model-View-ViewModel)是一种前端架构模式,Vue 的设计灵感来源于此。以下是 MVVM 在 Vue 中的核心实现方式: 数据绑定(M…

vue  实现分页

vue 实现分页

实现分页的基本思路 在Vue中实现分页通常需要结合后端API返回的数据和前端的分页组件。分页的核心逻辑包括计算总页数、处理当前页码变化以及触发数据请求。 分页组件示例 使用Element UI的分页…