el-table表头根据响应数据排列

水一篇文章

今天写了什么

用文心一言写了数据结构转换,想想后面还用得到,记录一下。

接口响应数据:

{
    "categorySaleStatisticsTend": [
        {
            "dataDateUnit": "2024-09-15",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-09-16",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-09-17",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-09-18",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-09-19",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-09-20",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-09-21",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-09-22",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-09-23",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-09-24",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-09-25",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-09-26",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-09-27",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-09-28",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-09-29",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-09-30",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-10-01",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-10-02",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-10-03",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-10-04",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-10-05",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-10-06",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-10-07",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-10-08",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-10-09",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-10-10",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-10-11",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-10-12",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-10-13",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        },
        {
            "dataDateUnit": "2024-10-14",
            "keywordProperty": {
                "上衣": 0,
                "下装": 0,
                "配件": 0,
                "汇总": 0,
                "文胸": 0
            }
        }
    ],
    "categorySaleTendData": [
        {
            "dataDateUnit": "上衣",
            "keywordProperty": {
                "2024-09-20": 0,
                "2024-09-21": 0,
                "2024-09-22": 0,
                "2024-09-27": 0,
                "2024-09-28": 0,
                "2024-09-29": 0,
                "2024-09-23": 0,
                "2024-09-24": 0,
                "2024-09-25": 0,
                "2024-09-26": 0,
                "2024-10-09": 0,
                "2024-10-07": 0,
                "2024-10-08": 0,
                "2024-10-05": 0,
                "2024-10-06": 0,
                "2024-10-03": 0,
                "2024-10-04": 0,
                "2024-10-01": 0,
                "2024-10-02": 0,
                "2024-09-30": 0,
                "2024-09-16": 0,
                "2024-09-17": 0,
                "2024-09-18": 0,
                "2024-09-19": 0,
                "2024-09-15": 0,
                "2024-10-14": 0,
                "2024-10-12": 0,
                "2024-10-13": 0,
                "2024-10-10": 0,
                "2024-10-11": 0
            }
        },
        {
            "dataDateUnit": "下装",
            "keywordProperty": {
                "2024-09-20": 0,
                "2024-09-21": 0,
                "2024-09-22": 0,
                "2024-09-27": 0,
                "2024-09-28": 0,
                "2024-09-29": 0,
                "2024-09-23": 0,
                "2024-09-24": 0,
                "2024-09-25": 0,
                "2024-09-26": 0,
                "2024-10-09": 0,
                "2024-10-07": 0,
                "2024-10-08": 0,
                "2024-10-05": 0,
                "2024-10-06": 0,
                "2024-10-03": 0,
                "2024-10-04": 0,
                "2024-10-01": 0,
                "2024-10-02": 0,
                "2024-09-30": 0,
                "2024-09-16": 0,
                "2024-09-17": 0,
                "2024-09-18": 0,
                "2024-09-19": 0,
                "2024-09-15": 0,
                "2024-10-14": 0,
                "2024-10-12": 0,
                "2024-10-13": 0,
                "2024-10-10": 0,
                "2024-10-11": 0
            }
        },
        {
            "dataDateUnit": "配件",
            "keywordProperty": {
                "2024-09-20": 0,
                "2024-09-21": 0,
                "2024-09-22": 0,
                "2024-09-27": 0,
                "2024-09-28": 0,
                "2024-09-29": 0,
                "2024-09-23": 0,
                "2024-09-24": 0,
                "2024-09-25": 0,
                "2024-09-26": 0,
                "2024-10-09": 0,
                "2024-10-07": 0,
                "2024-10-08": 0,
                "2024-10-05": 0,
                "2024-10-06": 0,
                "2024-10-03": 0,
                "2024-10-04": 0,
                "2024-10-01": 0,
                "2024-10-02": 0,
                "2024-09-30": 0,
                "2024-09-16": 0,
                "2024-09-17": 0,
                "2024-09-18": 0,
                "2024-09-19": 0,
                "2024-09-15": 0,
                "2024-10-14": 0,
                "2024-10-12": 0,
                "2024-10-13": 0,
                "2024-10-10": 0,
                "2024-10-11": 0
            }
        },
        {
            "dataDateUnit": "汇总",
            "keywordProperty": {
                "2024-09-20": 0,
                "2024-09-21": 0,
                "2024-09-22": 0,
                "2024-09-27": 0,
                "2024-09-28": 0,
                "2024-09-29": 0,
                "2024-09-23": 0,
                "2024-09-24": 0,
                "2024-09-25": 0,
                "2024-09-26": 0,
                "2024-10-09": 0,
                "2024-10-07": 0,
                "2024-10-08": 0,
                "2024-10-05": 0,
                "2024-10-06": 0,
                "2024-10-03": 0,
                "2024-10-04": 0,
                "2024-10-01": 0,
                "2024-10-02": 0,
                "2024-09-30": 0,
                "2024-09-16": 0,
                "2024-09-17": 0,
                "2024-09-18": 0,
                "2024-09-19": 0,
                "2024-09-15": 0,
                "2024-10-14": 0,
                "2024-10-12": 0,
                "2024-10-13": 0,
                "2024-10-10": 0,
                "2024-10-11": 0
            }
        },
        {
            "dataDateUnit": "文胸",
            "keywordProperty": {
                "2024-09-20": 0,
                "2024-09-21": 0,
                "2024-09-22": 0,
                "2024-09-27": 0,
                "2024-09-28": 0,
                "2024-09-29": 0,
                "2024-09-23": 0,
                "2024-09-24": 0,
                "2024-09-25": 0,
                "2024-09-26": 0,
                "2024-10-09": 0,
                "2024-10-07": 0,
                "2024-10-08": 0,
                "2024-10-05": 0,
                "2024-10-06": 0,
                "2024-10-03": 0,
                "2024-10-04": 0,
                "2024-10-01": 0,
                "2024-10-02": 0,
                "2024-09-30": 0,
                "2024-09-16": 0,
                "2024-09-17": 0,
                "2024-09-18": 0,
                "2024-09-19": 0,
                "2024-09-15": 0,
                "2024-10-14": 0,
                "2024-10-12": 0,
                "2024-10-13": 0,
                "2024-10-10": 0,
                "2024-10-11": 0
            }
        }
    ]
}

Vue页面

<template>
  <div>
    <el-card>
      <div slot="header">
        品类销售趋势
        <!-- <span
                      ><i class="el-icon-collection"></i>
                      {{ query.salesGrade + " " }}年度完成率</span
                    > -->
      </div>
      <!-- <LineChart /> -->
      <div
        style="margin-left: 2px; margin-right: 2px; height: 410px"
        ref="salesQuantity"
      ></div>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="dataDateUnit" label="类别" width="100">
        </el-table-column>
        <el-table-column
          v-for="(month, index) in months"
          :key="index"
          :prop="'salesData.' + index"
          :label="month"
          width="120"
        >
        </el-table-column>
      </el-table>
    </el-card>
    <el-card>
      <div slot="header">
        同期品类销售额对比分析
        <!-- <span
                      ><i class="el-icon-collection"></i>
                      {{ query.salesGrade + " " }}年度完成率</span
                    > -->
      </div>
      <barLine :echartDataSku="barLineChart" />
      <el-table :data="barLineData" style="width: 100%">
        <el-table-column
          prop="category1"
          label="类别"
          width="100"
        ></el-table-column>
        <el-table-column prop="salesAmount" label="销售额" width="100">
        </el-table-column>
        <el-table-column
          prop="yoySalesAmount"
          label="上一年销售额"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="salesAmountYoy"
          label="同比"
          width="100"
        ></el-table-column>
      </el-table>
    </el-card>
  </div>
</template>
<script>
import * as echarts from "echarts/lib/echarts.js";
import {
  selectCategorySaleTend,
  selectCategorySaleYoy,
} from "@/api/productStructure/BiCategoryBasicsDwd";
import LineChart from "./components/LineChart";
import barLine from "./components/barLine";

export default {
  props: {
    query: {
      type: Object,
      default: () => {},
    },
  },
  components: {
    barLine,
    LineChart,
  },
  data() {
    return {
      tableData: [], // 用于存储转换后的数据
      months: [], // 用于存储所有月份
      //对比
      barLineChart: {},
      barLineData: [],
    };
  },
  watch: {
    query: {
      handler: function (newnew, oldold) {
        this.getselectCategorySaleTend("salesQuantity");
        this.getselectCategorySaleYoy();
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    getselectCategorySaleYoy() {
      selectCategorySaleYoy(this.query).then((response) => {
        this.barLineData = response;
        this.barLineChart = {
          columns: ["品类", "销售额", "上一年销售额", , "同比"],
          rows: [],
        };

        response.forEach((item) => {
          this.barLineChart.rows.push({
            品类: item.category1,
            销售额: item.salesAmount,
            上一年销售额: item.yoySalesAmount,
            同比: item.salesAmountYoy ? item.salesAmountYoy : 0,
          });
        });
      });
    },
    getselectCategorySaleTend(property) {
      selectCategorySaleTend(this.query).then((response) => {
        this.tableData = []; // 用于存储转换后的数据
        this.months = []; // 用于存储所有月份
        // 提取所有月份
        response.categorySaleTendData.forEach((item) => {
          for (const month in item.keywordProperty) {
            // const element = array[index];
            if (!this.months.includes(month)) {
              this.months.push(month);
            }
          }
        });
        console.log(JSON.stringify(this.months));

        response.categorySaleTendData.forEach((item) => {
          const row = {
            dataDateUnit: item.dataDateUnit,
            salesData: [],
          };
          this.months.forEach((month) => {
            row.salesData.push(item.keywordProperty[month] || 0);
          });
          this.tableData.push(row);
        });

        // return;

        let order = { date: [], data: [] };
        order.date = response.categorySaleStatisticsTend.map(
          (item) => item.dataDateUnit
        );
        response.categorySaleStatisticsTend.forEach((item) => {
          order.data.push(item.keywordProperty);
        });
        const xData = order.date;

        console.log(JSON.stringify(order.data));
        const keyArray = Object.keys(order.data[1]);
        const series = [];
        keyArray.forEach((key) => {
          series.push({
            name: key,
            data: order.data.map((item) => item[key]),
            type: "line",
          });
        });

        const option = {
          grid: {
            left: "8%",
            top: "10%",
            right: "2%",
            bottom: "10%",
          },
          // 提示框
          tooltip: { trigger: "axis" },
          xAxis: {
            data: xData,
          },
          yAxis: {},
          legend: {
            data: keyArray,
          },
          series,
        };

        const E = echarts.init(this.$refs[property]);

        E.setOption(option);
      });
    },
  },
};
</script>

重点代码:


<el-table :data="tableData" style="width: 100%">
        <el-table-column prop="dataDateUnit" label="类别" width="110">
        </el-table-column>
        <el-table-column
          v-for="(month, index) in months"
          :key="index"
          :prop="'salesData.' + index"
          :label="month"
          width="120"
        >
        </el-table-column>
      </el-table>


//methods
selectCategoryVisitorTend(this.query).then((response) => {
        this.tableData = []; // 用于存储转换后的数据
        this.months = []; // 用于存储所有月份
        // 提取所有月份
        response.categorySaleTendData.forEach((item) => {
          for (const month in item.keywordProperty) {
            if (!this.months.includes(month)) {
              this.months.push(month);
            }
          }
        });

        // 转换数据格式
        response.categorySaleTendData.forEach((item) => {
          const row = { dataDateUnit: item.dataDateUnit, salesData: [] };
          this.months.forEach((month) => {
            row.salesData.push(item.keywordProperty[month] || 0); // 使用逻辑或操作符确保没有数据的月份显示为0
          });
          this.tableData.push(row);
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值