当前位置: 首页 > news >正文

vue table 自定义处理 key 作为 表头

   数据格式:有不同的key 对应不同数组

解决方式:重新渲染一遍数据

<template><div class="bg"><el-table :data="tableListShow" border ref="table" :cell-style="{ 'text-align': 'center' }" :header-cell-style="{background: '#5f697d',color: '#fff',height: '10px','text-align': 'center'}" stripe v-loading="loading"><el-table-column v-for="(column, index) in tableColumns" :key="index" :label="column.label":prop="column.prop"><template #default="scope">{{ scope.row[column.prop] }}</template></el-table-column></el-table></div>
</template>
<script setup>
const loading = ref(false)
const tableList = ref([]) // 原始数据
const tableColumns = computed(() => {  // 重新渲染表头const columns = [];for (const key in tableList.value) {const firstItem = tableList.value[key][0];if (firstItem) {const gradeNoLabel = showCustomValue(grandeNoList.value, firstItem.GRADE_NO);columns.push({ label: `${t('delivery_fee.weight_table')}`, prop: `${key}_WT_WTNM` });columns.push({ label: gradeNoLabel, prop: `${key}_MNY` });}}return columns;
});const tableListShow = computed(() => {  // 重新渲染数据const result = [];let maxLength = 0;for (const key in tableList.value) {maxLength = Math.max(maxLength, tableList.value[key].length);}for (let i = 0; i < maxLength; i++) {const row = {};for (const key in tableList.value) {const item = tableList.value[key][i];if (item) {row[`${key}_WT_WTNM`] = `${formatAmount(item.WT)} ${item.WT_NM}`;row[`${key}_MNY`] = `₩${formatAmount(item.MNY)}`;} else {row[`${key}_WT_WTNM`] = "-";row[`${key}_MNY`] = "-";}}result.push(row);}return result;
});const getList = async () => {loading.value = truetry {await api.getFeeList().then(res => {if (res.code === 200) {tableList.value = JSON.parse(JSON.stringify(res?.data));}})} finally {loading.value = false}
}onMounted(async () => {await getList();});</script>

planB:

<template><div class="bg"><el-table:data="tableListShow"borderref="table":cell-style="{ 'text-align': 'center' }":header-cell-style="{background: '#5f697d',color: '#fff',height: '10px','text-align': 'center'}"stripev-loading="loading"><el-table-columnv-for="(column, index) in tableColumns":key="index":label="column.label":prop="column.prop"><template #default="scope">{{ scope.row[column.prop] }}</template></el-table-column></el-table></div>
</template><script setup>
import { ref, onMounted, computed } from 'vue'
import { formatAmount } from '@/utils/format'const loading = ref(false)
const tableList = ref({}) // 原始数据
const tableColumns = ref([]) // 表头
const tableListShow = ref([]) // 展示数据// 提取并扁平化数据
const processTableData = (rawData) => {const columns = []const result = []let maxLength = 0// 第一次遍历:构建表头 + 找最大长度for (const key in rawData) {const list = rawData[key]if (list.length > 0) {maxLength = Math.max(maxLength, list.length)const gradeNoLabel = showCustomValue(grandeNoList.value, list[0].GRADE_NO)columns.push({ label: `${t('delivery_fee.weight_table')}`, prop: `${key}_WT_WTNM` })columns.push({ label: gradeNoLabel, prop: `${key}_MNY` })}}// 第二次遍历:构建展示数据for (let i = 0; i < maxLength; i++) {const row = {}for (const key in rawData) {const item = rawData[key][i]if (item) {row[`${key}_WT_WTNM`] = `${formatAmount(item.WT)} ${item.WT_NM}`row[`${key}_MNY`] = `₩${formatAmount(item.MNY)}`} else {row[`${key}_WT_WTNM`] = '-'row[`${key}_MNY`] = '-'}}result.push(row)}return { columns, result }
}const getList = async () => {loading.value = truetry {const res = await api.getFeeList()if (res.code === 200) {const { columns, result } = processTableData(res.data)tableColumns.value = columnstableListShow.value = resulttableList.value = res.data // 保留原始数据用于后续操作}} finally {loading.value = false}
}onMounted(async () => {await getList()
})
</script>

http://www.lryc.cn/news/585721.html

相关文章:

  • AUTOSAR进阶图解==>AUTOSAR_SWS_IOHardwareAbstraction
  • [精选]如何解决pip安装报错ModuleNotFoundError: No module named ‘subprocess’问题
  • Matlab裁剪降水数据:1km掩膜制作实战
  • C++STL-list
  • 这个方法的目的是检查一个给定的项目ID(projectId)是否在当前数据库中被使用(搜索全库)
  • 四、神经网络——正则化方法
  • VLM-R1 + GRPO 算法完整复现全过程日志
  • Linux修炼:权限
  • SpringCloud【OpenFeign】
  • 学习日记-spring-day46-7.11
  • 伺服驱动控制CANopen协议
  • 网络编程(基本概念)
  • 【C++篇】二叉树进阶(上篇):二叉搜索树
  • TCP详解——流量控制、滑动窗口
  • mysql的性能优化:组提交、数据页复用、全表扫描优化、刷脏页
  • 【JMeter】调试方法
  • 论容器化 | 分析Go和Rust做医疗的后端服务
  • Express实现定时任务
  • 飞算科技正在撬动各行业数字化转型的深层变革
  • ch06 部分题目思路
  • OpenCV实现感知哈希(Perceptual Hash)算法的类cv::img_hash::PHash
  • 深入探究编程拷贝
  • 基于Java Spring Boot开发的旅游景区智能管理系统 计算机毕业设计源码32487
  • 4万亿英伟达,凭什么?
  • 【Linux应用】Ubuntu20.04 aarch64开发板一键安装ROS2(清华源)
  • PandaCoder重大产品更新-引入Jenkinsfile文件支持
  • mysql的LIMIT 用法
  • 【AI大模型】超越RAG的搜索革命!分层框架让AI像专家团队一样深度思考
  • Java教程:JavaWeb ---MySQL高级
  • 隆重介绍 Xget for Chrome:您的终极下载加速器