您好,欢迎来到刀刀网。
搜索
您的当前位置:首页el-table 动态生成列

el-table 动态生成列

来源:刀刀网
<template>
    <el-table
            :data="tableData"
            :cell-style="{ 'text-align': 'center' }"
            :header-cell-style="{
                  background: '#c0c1c4',
                  color: '#1e1f22',
                  height: '35px',
                  'text-align': 'center'
              }"
            border stripe
            max-height="580"
            style="margin-top: 2vh"
          >
            <el-table-column align="center" label="电表名称" prop="name"/>
            <!-- 动态生成日期列 -->
            <el-table-column
              v-for="date in sortedDates"
              :key="date"
              :label="date"
              :prop="date"
            >
              <template #default="{ row }">
                <span>{{ row[date] || '' }}</span>
              </template>
            </el-table-column>
    </el-table>
</template>

 

<script setup lang="ts" name="dayTotal">

const tableData = ref([]) // 用于展示的数组
const allDates = ref(new Set()); // / 提取所有日期字段并去重

// 按照接口返回的数据中
// 按日期排序
const sortedDates = computed(() => {
  return Array.from(allDates.value).sort((a, b) => {
    const dayA = parseInt(a);
    const dayB = parseInt(b);
    return dayA - dayB;
  });
});


const getList = () => {
    const data = await dailyApi.getPage(queryParams)
    tableData.value = data.list[0]
    total.value = data.total


    // 收集数据中的所有日期
    tableData.value.forEach(item => {
      Object.keys(item).forEach(key => {
        if (key.endsWith("日")) {
          allDates.value.add(key);
        }
      });
    });

}
getList()

</script>

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- gamedaodao.com 版权所有 湘ICP备2022005869号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务