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

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

<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的方式拓展,把这一列拓展成多列

<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、二维表格

<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实现二维表格 - 简书

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
动态生成el-table二维表格,并且使用图标作为表头和表,可以使用el-table-column组件并结合自定义的作用域插槽来实现。 首先,需要定义一个二维数组的数据结构和对应的图标数组: ```javascript data() { return { tableData: [ ['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'] ], iconData: [ 'el-icon-user', 'el-icon-goods', 'el-icon-message' ] } } ``` 然后,在模板中使用el-table组件,并使用v-for指令动态生成表头和表格数据,并在作用域插槽中使用图标作为表头和表的内容: ```html <el-table :data="tableData"> <el-table-column v-for="(row, rowIndex) in tableData" :key="rowIndex"> <template slot="header" slot-scope="scope"> <i :class="iconData[scope.$index]"></i> </template> <template slot-scope="{ row }"> <el-table-column v-for="(cell, cellIndex) in row" :key="cellIndex"> {{ cell }} </el-table-column> </template> </el-table-column> </el-table> ``` 在这个例子中,我们使用v-for指令遍历二维数组的每一行,并使用el-table-column动态生成每一行的表头。然后,在作用域插槽中使用iconData数组中对应位置的图标作为表头的内容。 接着,在每一行的作用域插槽中,再使用v-for指令遍历该行的每一个单元格,并使用el-table-column动态生成每一的表头和数据。 最终效果就是一个动态生成的二维表格,表头和表使用了图标作为内容。 完整的代码示例: ```html <template> <el-table :data="tableData"> <el-table-column v-for="(row, rowIndex) in tableData" :key="rowIndex"> <template slot="header" slot-scope="scope"> <i :class="iconData[scope.$index]"></i> </template> <template slot-scope="{ row }"> <el-table-column v-for="(cell, cellIndex) in row" :key="cellIndex"> {{ cell }} </el-table-column> </template> </el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ ['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'] ], iconData: [ 'el-icon-user', 'el-icon-goods', 'el-icon-message' ] } } } </script> ``` 这样就可以实现一个动态生成的二维表格,表头和表使用了图标作为内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值