el-table 循环+嵌套效果实现

el-table 循环+嵌套效果实现

效果

想要的效果:因为1~12月的数据是动态获取的,而且格式相同,通过循环月份数据简化代码,下面是要实现的效果:
在这里插入图片描述

实现

HTML代码

<div v-for="(item, index) in months" :key="index">
  <el-table-column :label="curYear + '年' + item + '月'">
    <el-table-column label="实际销售" label-class-name="c2">
      <el-table-column :label="`${curYear}`" label-class-name="c2">
        <template slot-scope="scope">
          {{ daily(scope.row.monthData[index].data,'sellReal',curYear) }}
        </template>
      </el-table-column>
      <el-table-column :label="`${curYear-1}`" label-class-name="c2">
        <template slot-scope="scope">
          {{ daily(scope.row.monthData[index].data,'sellReal',curYear-1) }}
        </template>
      </el-table-column>
      <el-table-column :label="`${curYear-2}`" label-class-name="c2">
        <template slot-scope="scope">
          {{ daily(scope.row.monthData[index].data,'sellReal',curYear-2) }}
        </template>
      </el-table-column>
    </el-table-column>
    <el-table-column label="同比" label-class-name="c2">
      <el-table-column :label="`${curShortYear}&${curShortYear-1}`" prop="ratio_last" label-class-name="c2">
        <template slot-scope="scope">
          <span :style="{color: colorColoum(scope.row.monthData[index].ratio_last, true)}">{{ scope.row.monthData[index].ratio_last }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="`${curShortYear}&${curShortYear-2}`" prop="ratio_prev" label-class-name="c2">
        <template slot-scope="scope">
          <span :style="{color: colorColoum(scope.row.monthData[index].ratio_prev, true)}">{{ scope.row.monthData[index].ratio_prev }}</span>
        </template>
      </el-table-column>
    </el-table-column>
    <el-table-column label="控比" label-class-name="c2">
      <el-table-column label="****" prop="ratio_cb" label-class-name="c2">
        <template slot-scope="scope">
          <span :style="{color: colorColoum(scope.row.monthData[index].ratio_cb, false)}">{{ scope.row.monthData[index].ratio_cb }}</span>
        </template>
      </el-table-column>
    </el-table-column>
  </el-table-column>
</div>

JS代码:

data() {
    return {
      tableData: [], // el-table 绑定的data数据
      // tableData的数据格式:
      //tableData: [
      //   {
      //       rowName: '第一列数据的名称,如店铺名称之类的',
      //       monthData: [{month: '01', ...}, {month: '02', ...}, ...], // 你的月份数据,对应months数组
      //   }
      //]
      months: ['01'], // 这里只默认了1月,具体的看你的数据有几个月,在处理数据时动态赋值
      curYear: null // 当前年份,自己在数据处理时动态赋值
    }
}
//再补充一个通过条件给单元格标颜色的方法,与嵌套循环配套使用(普通的表格可以用cell-style更简单)
methods: {
    // 其实js很简单,一个方法就搞定了,重点是上面的html代码,注意我们要绑定样式,必须在template标签里再嵌套一个span标签哦
    colorColoum(param, isMinus) {
      if (isMinus) {
        if (param.split('%')[0] < 0) {
          return 'red'
        }
      } else {
        if (param.split('%')[0] < 100) {
          return 'red'
        }
      }
      return 'black'
    },
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值