当前位置:首页 > VUE

vue实现多级表头

2026-03-26 21:49:31VUE

Vue 实现多级表头的方法

使用 Element UI 的 el-table 组件

Element UI 的表格组件原生支持多级表头,通过嵌套 el-table-column 实现。

vue实现多级表头

<el-table :data="tableData">
  <el-table-column prop="date" label="日期" width="180"></el-table-column>
  <el-table-column label="配送信息">
    <el-table-column prop="name" label="姓名" width="180"></el-table-column>
    <el-table-column label="地址">
      <el-table-column prop="province" label="省份" width="120"></el-table-column>
      <el-table-column prop="city" label="市区" width="120"></el-table-column>
      <el-table-column prop="address" label="详细地址" width="300"></el-table-column>
    </el-table-column>
  </el-table-column>
</el-table>

使用 Ant Design Vue 的 a-table 组件

Ant Design Vue 也支持多级表头,通过 columns 配置实现。

vue实现多级表头

const columns = [
  {
    title: '姓名',
    dataIndex: 'name',
    key: 'name',
    width: 100,
  },
  {
    title: '其他信息',
    children: [
      {
        title: '年龄',
        dataIndex: 'age',
        key: 'age',
        width: 200,
      },
      {
        title: '地址',
        children: [
          {
            title: '街道',
            dataIndex: 'street',
            key: 'street',
          },
          {
            title: '城市',
            dataIndex: 'city',
            key: 'city',
          },
        ],
      },
    ],
  },
];

自定义实现多级表头

如果没有使用 UI 框架,可以通过嵌套表头的方式实现。

<table>
  <thead>
    <tr>
      <th rowspan="2">日期</th>
      <th colspan="3">配送信息</th>
    </tr>
    <tr>
      <th>姓名</th>
      <th colspan="2">地址</th>
    </tr>
    <tr>
      <th></th>
      <th></th>
      <th>省份</th>
      <th>城市</th>
    </tr>
  </thead>
  <tbody>
    <tr v-for="item in tableData" :key="item.id">
      <td>{{ item.date }}</td>
      <td>{{ item.name }}</td>
      <td>{{ item.province }}</td>
      <td>{{ item.city }}</td>
    </tr>
  </tbody>
</table>

动态生成多级表头

根据后端返回的数据动态生成多级表头。

function generateColumns(data) {
  return data.map(item => {
    const column = {
      title: item.label,
      key: item.key,
    };
    if (item.children) {
      column.children = generateColumns(item.children);
    }
    return column;
  });
}

注意事项

  • 多级表头会增加表格的复杂度,可能影响性能
  • 表头层级不宜过深,建议控制在3层以内
  • 注意表格的响应式设计,确保在小屏幕上也能正常显示
  • 表头合并单元格时要注意跨行(rowspan)和跨列(colspan)的正确使用

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

相关文章

vue实现drag

vue实现drag

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

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

vue实现导出

vue实现导出

Vue 实现导出功能的方法 在 Vue 项目中实现导出功能通常涉及将数据导出为 Excel、CSV 或其他格式。以下是几种常见的实现方式: 使用 xlsx 库导出 Excel 安装 xlsx 库:…

vue实现抽屉

vue实现抽屉

Vue 实现抽屉组件 抽屉组件是一种常见的 UI 模式,通常用于从屏幕边缘滑出内容。以下是几种实现抽屉的方法: 使用 Vue 原生实现 创建一个基本的抽屉组件,利用 Vue 的过渡和条件渲染功能。…

vue删除实现

vue删除实现

Vue 删除功能的实现方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 方法一:使用数组的 splice 方法 通过索引删除数组中的元素,适用于列表渲染的场景。 methods: {…

vue实现门户

vue实现门户

Vue 实现门户网站的关键步骤 搭建基础框架 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 管理多页面路由。门户通常需要响应式布局,可引入 UI 库如 Element P…