el-table展示报表,动态列匹配数据

文章描述了一个在Vue.js应用中处理后端返回的非标准数据结构以便于在表格中正确展示的情况。开发者通过循环数组和使用条件判断来动态生成表格列,并使用el-popover组件处理过长的内容。数据重组和渲染过程涉及对labelList和titleName数组的操作。
摘要由CSDN通过智能技术生成

先上一张效果图和接口返回的数据结构。
如果接口返回数据结构正常的话很好展示,无奈后台接口返回的比较奇怪,不能直接拿来用,所以记录一下。
这是最终的效果图
数据结构

下面是table部分的代码
除了第一列,其他列是从数组里循环出来的,为的是用到nano属性,可以匹配到后面经过处理后的数据项进行展示。
其实很好理解,主要我这里加了判断是不是要显示tip,忽略掉就可以了。

<!-- table -->
      <el-table v-loading="loading" :data="labelList" class="resultTable">
        <el-table-column :label="title" align="center">
          <el-table-column  align="center" prop="name" label="项目\类别">
            <template slot-scope="scope">
              {{ scope.row.name }}
            </template>
          </el-table-column>
          <el-table-column align="center" v-for="item in titleName" :key="item.index" :prop="item.nano" :label="item.name">
            <template slot-scope="scope">
              <span v-if="(scope.row.name == '病名' || scope.row.name == '发病区划') && scope.row[item.nano]">
                <el-popover v-if="scope.row[item.nano].length > 17" width="400" trigger="hover" placement="top">
                  <p>{{ scope.row[item.nano] }}</p>
                  <div slot="reference" class="name-wrapper">
                    <span>{{ scope.row[item.nano] }}</span>
                  </div>
                </el-popover>
                <span v-if="scope.row[item.nano].length <= 17">{{ scope.row[item.nano] }}</span>
              </span>
              <span v-else>{{ scope.row.name == '病名数量' ? scope.row[item.nano] + '种' : scope.row[item.nano] }}</span>
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>

这是data部分。
lableList是为了下面接收到数据以后把数据重组。

     titleName:[
        {name:'一类病', nano: 'one'},
        {name:'二类病', nano: 'two'},
        {name:'三类病', nano: 'three'},
        {name:'其他病', nano: 'other'}
      ],
      labelList: [
        {
          name: '病名数量',
          label: 'diseaseCount'
        },
        {
          name: '病名',
          label: 'diseaseName'
        },
        {
          name: '发病区划',
          label: 'province'
        },
        {
          name: '县数',
          label: 'countyCount'
        },
        {
          name: '县次数',
          label: 'countyCountNum'
        },
        {
          name: '新发疫点数',
          label: 'newFieldCount'
        },
        {
          name: '总疫点数',
          label: 'fieldCount'
        },
        {
          name: '养殖环节 / 屠宰环节 / 流通环节 / 其他',
          label: 'fieldTypeCount'
        },
        {
          name: '易感动物数',
          label: 'ygdws'
        },
        {
          name: '发病数',
          label: 'fbs'
        },
        {
          name: '发病率(%)',
          label: 'fbl'
        },
        {
          name: '病死数',
          label: 'bss'
        },
        {
          name: '病死率(%)',
          label: 'bsl'
        },
        {
          name: '急宰数',
          label: 'jzs'
        },
        {
          name: '扑杀数',
          label: 'pss'
        },
        {
          name: '无害化处理数',
          label: 'whhcls'
        },
        {
          name: '紧急免疫数',
          label: 'jjmys'
        },
        {
          name: '治疗数',
          label: 'zls'
        }
      ],

这是方法部分。
很简单的处理,看看就明白了。

    getList(data) {
      this.loading = true;
      reportThree(data).then(res => {
        if(res.code == 200) {
          this.title = res.data.title
          this.labelList.map(e=>{
            this.$set(e,'one',res.data.one[e.label])
            this.$set(e,'two',res.data.two[e.label])
            this.$set(e,'three',res.data.three[e.label])
            this.$set(e,'other',res.data.other[e.label])
          })
          this.total = this.labelList.length
          this.loading = false;
        }
      });
    },

最后上一个经过处理打印出来的数据结构
处理后el-table能直接使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值