处理table数据,将后台返回的数据把列变成行,并且某列的数据一样的进行合并,动态表头

如果后台返回的数据是按列返回,而el-table渲染的数据是按行进行渲染的,所以需要将后台的数据进行一下转换,并且把某列的数据一样进行合并。如下图所示下边是后台返回的数据

 

代码如下

<el-table :data="tableList"
    border
    :span-method="arraySpanMethod"
    height="100%" 
    size="mini">
    <!-- <el-table-column label="名称" width="180">
          <template slot-scope="scope">
               {{ scope.$index == 0 ? '期初资金' : (scope.row.project_name == '2固定费用' || scope.row.project_name == '2变动费用'|| scope.row.project_name == '支出合计') ? '支出' :scope.$index == 15 ? '期末资金' : scope.$index == 16 ?'收支差' : '收入' }}
           </template>
     </el-table-column> -->
     <el-table-column prop="name" label="名称" width="180"></el-table-column>
     <el-table-column prop="project_name" label="资金项目" width="180"></el-table-column>
     <el-table-column v-for="item in headerList" :key="item.key"
                                    :prop="item.key" :label="item.text" width="180"></el-table-column>
     <el-table-column prop="totalAmount" label="合计" width="180"></el-table-column>
</el-table>
data: function () {
     return {
        tableList: [], //列表数据
        headerList: [],
        spanArr:[],
        pos:0
     }
},

arraySpanMethod({ row, column, rowIndex, columnIndex }){
    if (columnIndex === 0) {    //如果是第一列
        const _row = this.spanArr[rowIndex];   //将每一行传入上述方法,获取到每一行的合并行数
        const _col = _row > 0 ? 1 : 0;
        return {
            rowspan: _row,   //合并行
            colspan: _col    //合并列
        }
     }
},
//获取数据
getData() {
    let routerMessage = this.routerMessage;
    this.$post({
         url: "normalProcess",
         optype: routerMessage.optype,
         params: routerMessage.params
    }).then(result => {
         let data = result.data;
         // 获取左侧资金项目
                   
         // let projectList = Object.keys(data.rec);
         let projectList = [];
         let tableList = [];
         let headerList = [];
         data.rec.map(item =>{
              projectList.push(Object.keys(item)[0]);
         })
         data.list.forEach(dataItem => {
              headerList.push({
                  key: dataItem.acc_id + "_header",
                  text: dataItem.acc_short_name
              });
         });
         this.headerList = headerList;
         projectList.forEach((item,index) => {
              let showItem = {
                   project_name: data.rec[index][item],
                   totalAmount : 0,
              }
              data.list.forEach(dataItem => {
                   showItem[dataItem.acc_id + "_header"] = dataItem[item];
                   showItem.totalAmount += dataItem[item];
              });
              showItem.totalAmount = showItem.totalAmount.toFixed(2);
              tableList.push(showItem);
              tableList.forEach((subItem,index) => {
                  if(index == 0){
                      subItem.name = '期初资金'
                  }else if(index > 0 && index < 12){
                      subItem.name = '收入'
                  }else if(index > 11 && index < 15){
                      subItem.name = '支出'
                  }else if(index == 16){
                      subItem.name = '期末资金'
                  }else{
                      subItem.name = '收支差'
               } 
            })
         });
         this.tableList = tableList;
         this.getSpanArr(this.tableList)
     });
},
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值