<el-table>设置一列为固定字段,其他列为循环生成

本文介绍了如何使用Vue.js的ElementUI库中的el-table组件创建一个可动态展示数据的表格,重点讲解了如何通过formatter函数实现对特定ID的列值进行自定义处理,如将1映射为用户1,以此类推。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  <el-table :data="tableData" style="width: 100%">
      <el-table-column
        prop="name"
        label="固定字段名"
        :formatter="formatter"
      >
      </el-table-column>
      <el-table-column
        v-for="(item, index) in wordsColumns"
        :prop="item.name"
        :label="item.label"
        :key="index"
      >
      </el-table-column>
    </el-table>



const tableData = ref(
  [{
        date: '2016-05-02',
        id: '1',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-04',
        id: '2',
        name:'章三',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        date: '2016-05-01',
        id: '3',
        name: '李四',
        address: '上海市普陀区金沙江路 1519 弄'
      }, {
        date: '2016-05-03',
        id: '4',
        name: '王五',
        address: '上海市普陀区金沙江路 1516 弄'
      }]
);

const wordsColumns = [
  { name: "name", label: "姓名" },
  { name: "address", label: "地址" },
  { name: "date", label: "时间" },
];


const formatter = (row, column) => {
      console.log(row, column)
      switch (row.id) {
        case '1':
          return '用户1'
        case '2':
          return '用户2'
        case '3':
          return '用户3'
        case '4':
          return '用户4'
      }
      let newName = '用户' + row.name
      return newName
};

页面展示:

<template> <div class="factory-tables"> <div v-for="(factoryData, index) in pageList" :key="index" class="factory-table-container" > <h2>厂别: {{ factoryData.fact }}</h2> <el-table :data="generateTableData(factoryData)" border style="width: 100%; margin-bottom: 20px" :header-cell-style="{ background: '#f5f7fa' }" > <!-- 固定 --> <el-table-column prop="factory" label="Factory" width="120" fixed /> <el-table-column prop="category" label="Category" width="120" fixed /> <el-table-column prop="model" label="Model" width="180" fixed /> <!-- 动态季节 --> <el-table-column v-for="season in factoryData.seasonList" :key="season" :prop="season" :label="season" width="100" > <template #default="{ row }"> {{ row[season] || 0 }} </template> </el-table-column> </el-table> </div> </div> </template> <script setup> import { ref } from "vue"; import { categoryBySeasonList } from "@/api/planning/page"; const pageList = ref([]); const loading = ref(true); // 生成表格数据的方法(已更新) const generateTableData = (factoryData) => { const tableData = []; factoryData.categoryList.forEach((category) => { const models = factoryData.plngProdGrpDescList .filter((item) => { if (category == "0") { return ( item.order_major_category == "0" || item.order_major_category == "" || !item.order_major_category ); } return item.order_major_category == category; }) .map((item) => item.plng_prod_grp_desc); const uniqueModels = [...new Set(models)]; uniqueModels.forEach((model) => { const rowData = { factory: factoryData.fact, category: category, model: model, }; factoryData.seasonList.forEach((season) => { const matchedPop = factoryData.popList.find((pop) => { const popSeason = pop.season ? pop.season.trim() : ""; const currentSeason = season ? season.trim() : ""; return ( popSeason == currentSeason && pop.plngProdGrpDesc == model && (pop.orderMajorCategory == category || (category == "0" && (!pop.orderMajorCategory || pop.orderMajorCategory == ""))) ); }); rowData[season] = matchedPop ? matchedPop.totalCount : 0; }); tableData.push(rowData); }); }); return tableData; }; /** 查询【请填写功能名称】表 */ function getList() { loading.value = true; categoryBySeasonList().then((response) => { pageList.value = response.data; loading.value = false; }); } const props = defineProps({ autoLoad: { type: Boolean, default: true, }, }); const loadData = () => { getList(); }; defineExpose({ loadData, }); </script> <style scoped> .factory-tables { padding: 20px; } .factory-table-container { margin-bottom: 40px; } h2 { margin-bottom: 15px; color: #333; font-size: 18px; font-weight: bold; } .el-table { box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1); } .el-table--border { border: 1px solid #ebeef5; } .el-table__fixed-right { box-shadow: -2px 0 8px rgba(0, 0, 0, 0.08); } </style> 经过修缮以上代码是完全正确的,现在需要添加点需求,就是对表格进行合并,比如第factory,因为三个厂都有各自的表格,所以每个表格的factory值都样,所以就可以合并factory列为行就可以了,然后第二Category也可以进行合并,值相同的就合为行,其他保持不变
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值