elementPlus el-table动态列扩展及二维表格

1、循环列数据源,动态生成列

javascript 复制代码
<template>
  <div>
    <el-table ref="table" :data="pageData.tableData" stripe style="width: 100%">
      <el-table-column v-for="column in pageData.columns" :key="column.prop" :prop="column.prop" :label="column.label"
                       :min-width="column.minWidth">
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup lang="ts">
import {reactive} from 'vue';

const pageData = reactive({
  columns: [
    {
      prop: 'source',
      label: '来源',
      minWidth: '100px'
    },
    {
      prop: 'target',
      label: '目标',
      minWidth: '100px'
    },
    {
      prop: 'value1',
      label: 'value1(万)',
      minWidth: '150px'
    },
    {
      prop: 'value2',
      label: 'value2(亿)',
      minWidth: '150px'
    },
  ],
  tableData: [{
    source: '北京',
    target: '上海',
    value1: '189',
    value2: '1.89'
  }, {
    source: '天津',
    target: '河北',
    value1: '233',
    value2: '2.33'
  }, {
    source: '上海',
    target: '陕西',
    value1: '97',
    value2: '0.97'
  }, {
    source: '内蒙古',
    target: '山东',
    value1: '180',
    value2: '1.80'
  }]
});

</script>

2、对某一列用slot的方式拓展,把这一列拓展成多列

javascript 复制代码
<template>
  <div>
    <el-table :data="tableData">
      <el-table-column label="来源" prop="source"></el-table-column>
      <el-table-column label="目标" prop="target"></el-table-column>
      <el-table-column v-for="(item, index) in tableData[0].zbList" :key="index">
        <template #header>
          {{ item.zb }}
        </template>
        <template v-slot="{ row }">
          {{ row.zbList[index].value }}
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import {ref} from 'vue';

const tableData = ref([{
  source: '北京',
  target: '上海',
  zbList: [{
    zb: '指标一',
    value: '2000'
  },
    {
      zb: '指标二',
      value: '4000'
    },
    {
      zb: '指标三',
      value: '6000'
    },
  ]
},
  {
    source: '北京',
    target: '天津',
    zbList: [{
      zb: '指标一',
      value: '20'
    },
      {
        zb: '指标二',
        value: '40'
      },
      {
        zb: '指标三',
        value: '60'
      },
    ]
  },
]);
</script>

3、二维表格

javascript 复制代码
<template>
  <el-table :data="data.tableData" style="width: 100%">
    <el-table-column prop="color" label="颜色\尺码" width="180"></el-table-column>
    <el-table-column v-for="(i, index) in data.sizes" :label="i" align="center" header-align="center" :key="index">
      <template v-slot="scope">{{ scope.row[i] }}</template>
    </el-table-column>
  </el-table>
</template>
<script setup lang="ts">
import {reactive} from 'vue';

const data = reactive({
  sizes: ["x", "xl"],
  tableData: [
    {
      color: "red",
      xl: 10,
      x: 0
    },
    {
      color: "blue",
      xl: 10,
      x: 0
    },
    {
      color: "black",
      xl: 10,
      x: 5
    }
  ],
});

</script>

参考: Element-plus的el-table动态列表格_elementplus 带状态表格-CSDN博客

前端(PC)---elementUI实现二维表格 - 简书

相关推荐
wangbing11258 分钟前
界面规范11-对话框
javascript·vue.js·elementui
不一样的少年_41 分钟前
Vue3 后台分页写腻了?我用 1 个 Hook 删掉 90% 重复代码(附源码)
前端·vue.js·设计模式
一枚前端小能手1 小时前
🔥 滚动监听写到手抽筋?IntersectionObserver让你躺平实现懒加载
前端·javascript
我是日安1 小时前
从零到一打造 Vue3 响应式系统 Day 5 - 核心概念:单向链表、双向链表
前端·vue.js
遂心_1 小时前
React中的onChange事件:从原理到实践的全方位解析
前端·javascript·react.js
GHOME1 小时前
原型链的原貌
前端·javascript·面试
槿泽1 小时前
Vue集成Electron目前最新版本
前端·vue.js·electron
luckyCover1 小时前
带你一起攻克js之原型到原型链~
前端·javascript
麦当_1 小时前
SwipeMultiContainer 滑动切换容器算法指南
前端·javascript·算法
用户31506327304871 小时前
使用 vue-virtual-scroller 实现高性能传输列表功能总结
javascript·vue.js