当前位置:首页 > 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实现换肤

vue实现换肤

使用 CSS 变量实现换肤 在 Vue 项目中,可以通过 CSS 变量(Custom Properties)动态切换主题色。CSS 变量可以在运行时修改,适合实现换肤功能。 定义全局 CSS 变量:…

vue实现横滚

vue实现横滚

Vue 实现横滚效果 横滚效果通常指水平滚动内容,常见于轮播图、横向导航栏等场景。以下是几种实现方式: 使用 CSS 和 Vue 结合 通过 CSS 的 overflow-x 和 white-spa…

vue实现截屏怎么实现

vue实现截屏怎么实现

Vue实现截屏的方法 使用html2canvas库 html2canvas是一个流行的JavaScript库,可以将网页元素转换为Canvas图像。在Vue项目中安装html2canvas: npm…

vue 实现单点登录

vue 实现单点登录

单点登录(SSO)实现原理 单点登录允许用户通过一次身份验证访问多个系统。核心原理是用户首次登录后,认证中心颁发令牌(如Token),其他系统通过验证令牌实现免登录。 Vue中实现SSO的方案 基于…

vue实现拦截登录

vue实现拦截登录

Vue 实现登录拦截的方法 使用路由守卫进行拦截 Vue Router 提供了全局前置守卫 beforeEach,可以在路由跳转前进行验证。在路由配置文件中添加以下代码: import router…

vue如何实现tap

vue如何实现tap

实现Tap事件的方法 在Vue中实现类似移动端的tap事件(轻触)可以通过以下几种方式完成。原生移动端浏览器通常通过touchstart和touchend事件组合模拟tap,而Vue中可以封装自定义指…