惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 司徒正美
Google Online Security Blog
Google Online Security Blog
博客园_首页
量子位
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
Vercel News
Vercel News
GbyAI
GbyAI
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cisco Blogs
IT之家
IT之家
A
Arctic Wolf
P
Privacy International News Feed
G
GRAHAM CLULEY
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
T
The Blog of Author Tim Ferriss
AWS News Blog
AWS News Blog
A
About on SuperTechFans
P
Proofpoint News Feed
I
Intezer
月光博客
月光博客
Cloudbric
Cloudbric
Google DeepMind News
Google DeepMind News
T
Tor Project blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Securelist
Engineering at Meta
Engineering at Meta
爱范儿
爱范儿
F
Full Disclosure
V2EX - 技术
V2EX - 技术
Last Week in AI
Last Week in AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
人人都是产品经理
人人都是产品经理
C
Check Point Blog
I
InfoQ
S
Security Affairs
Simon Willison's Weblog
Simon Willison's Weblog
P
Palo Alto Networks Blog
F
Fortinet All Blogs
S
Security @ Cisco Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
K
Kaspersky official blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security

博客园 - ^Mao^

不同缩放下适配 vxe-table 3D饼图 openlayer实现给线的附近添加点,点支持移动 element-plus el-select html导出pdf openlayers增加移动功能 适配 部分页面统计用户访问时长 openlayers基本使用(街景+标注+绘制) 使用openlayer绘制街景地图 正则表达式--取对应表达式的值 echarts-雷达图 echarts--地图 散点图效果 echarts散点图区域设置 vant 的vant-uploader组件问题 Threejs学习 Echarts-普通地图和3D地图实现 test
vue2实现el-table-column多级效果
^Mao^ · 2025-05-24 · via 博客园 - ^Mao^

背景

有的时候我们表格的列可能是多级的情况,但是如果采用官方的示例,需要循环嵌套。

  1. 支持多层嵌套
  2. 某列支持插槽

效果图

代码

App.vue

<template>
  <div class="app">
    <sys-table :tableColumns="table_columns" :data="table_data">
      <template #address="scope">
        <h1>嘻嘻:{{ scope.row.address }}</h1>
      </template>
    </sys-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      headerCellStyle: {
        backgroundColor: 'orange',
        color: '#fff',
      },
      table_columns: [
        {
          label: '姓名',
          prop: 'name',
        },
        {
          label: '年龄',
          prop: 'age',
        },
        {
          label: '住址',
          prop: 'address',
          slotFlag: true,
        },
        {
          label: '朋友-1',
          children: [
            {
              label: '朋友A',
              prop: 'friend_a',
            },
            {
              label: '朋友B',
              prop: 'friend_b',
            },
          ],
        },
        {
          label: '朋友-2',
          children: [
            {
              label: '朋友-2A',
              prop: 'friend_2_a',
              children: [
                {
                  label: '朋友-3A',
                  prop: 'friend_3a',
                },
              ],
            },
            {
              label: '朋友-2B',
              prop: 'friend_2_b',
            },
          ],
        },
      ],
      table_data: [],
    }
  },
  created() {
    setTimeout(() => {
      this.table_data = [
        {
          name: '小明',
          age: 23,
          address: '光明路',
          friend_a: 'a1',
          friend_b: 'b1',
        },
      ]
    }, 300)
  },
}
</script>

<style lang="less" scoped></style>

sys-table.vue

<template>
  <div class="sys-table">
    <el-table border v-bind="$attrs">
      <sys-table-column
        v-for="item in tableColumns"
        :key="item.prop"
        :col="item"
      >
        <!-- 动态插槽 -->
        <template v-for="slot in Object.keys($scopedSlots)" #[slot]="scope">
          <!-- 以之前的名字命名插槽,同时把数据原样绑定 -->
          <slot :name="slot" v-bind="scope" />
        </template>
      </sys-table-column>
    </el-table>
  </div>
</template>

<script>
import SysTableColumn from './sys-table-column.vue'
export default {
  components: {
    SysTableColumn,
  },
  props: {
    tableColumns: {
      type: Array,
    },
  },
}
</script>

<style lang="less" scoped></style>

sys-table-column.vue

<template>
  <el-table-column
    v-if="!col.children"
    :label="col.label"
    :prop="col.prop || ''"
    :show-overflow-tooltip="!col.els"
  >
    <template #header>
      <slot v-if="col.headerSlot" :name="col.headerSlot" />
      <span v-else>{{ col.label }}</span>
    </template>
    <template #default="scope">
      <slot v-if="col.slotFlag" :name="col.prop" :row="scope.row" />
      <span v-else>{{
        typeof scope.row[col.prop] !== 'number' && !scope.row[col.prop]
          ? '--'
          : scope.row[col.prop]
      }}</span>
    </template>
  </el-table-column>

  <el-table-column v-else :label="col.label">
    <sys-table-column v-for="t in col.children" :key="t.prop" :col="t">
      <!-- 注意:如果是vue2中的话customSlots可以替换为$scopedSlots,而且下面setup中的取值也不需要了 -->
      <template v-for="slot in Object.keys($scopedSlots)" #[slot]="scope">
        <!-- 以之前的名字命名插槽,同时把数据原样绑定 -->
        <slot :name="slot" v-bind="scope" />
      </template>
    </sys-table-column>
  </el-table-column>
</template>

<script>
export default {
  name: 'SysTableColumn',
  props: {
    // columns: Array,
    col: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {}
  },
}
</script>

<style lang="less" scoped></style>

参考文档

https://blog.csdn.net/qq_16844491/article/details/116064593