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

推荐订阅源

P
Palo Alto Networks Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
S
Schneier on Security
S
Securelist
酷 壳 – CoolShell
酷 壳 – CoolShell
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cyberwarzone
Cyberwarzone
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
GbyAI
GbyAI
Security Latest
Security Latest
Last Week in AI
Last Week in AI
Microsoft Security Blog
Microsoft Security Blog
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
Webroot Blog
Webroot Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
C
Cisco Blogs
博客园 - 【当耐特】
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
B
Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Attack and Defense Labs
Attack and Defense Labs
The Last Watchdog
The Last Watchdog
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
Project Zero
Project Zero
WordPress大学
WordPress大学
L
LINUX DO - 最新话题
F
Fortinet All Blogs
L
LINUX DO - 热门话题
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
MongoDB | Blog
MongoDB | Blog
Latest news
Latest news
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
爱范儿
爱范儿
O
OpenAI News
J
Java Code Geeks
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - ^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