当前位置:   article > 正文

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

arrayspanmethod

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

 

代码如下

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

  1. arraySpanMethod({ row, column, rowIndex, columnIndex }){
  2. if (columnIndex === 0) { //如果是第一列
  3. const _row = this.spanArr[rowIndex]; //将每一行传入上述方法,获取到每一行的合并行数
  4. const _col = _row > 0 ? 1 : 0;
  5. return {
  6. rowspan: _row, //合并行
  7. colspan: _col //合并列
  8. }
  9. }
  10. },
  11. //获取数据
  12. getData() {
  13. let routerMessage = this.routerMessage;
  14. this.$post({
  15. url: "normalProcess",
  16. optype: routerMessage.optype,
  17. params: routerMessage.params
  18. }).then(result => {
  19. let data = result.data;
  20. // 获取左侧资金项目
  21. // let projectList = Object.keys(data.rec);
  22. let projectList = [];
  23. let tableList = [];
  24. let headerList = [];
  25. data.rec.map(item =>{
  26. projectList.push(Object.keys(item)[0]);
  27. })
  28. data.list.forEach(dataItem => {
  29. headerList.push({
  30. key: dataItem.acc_id + "_header",
  31. text: dataItem.acc_short_name
  32. });
  33. });
  34. this.headerList = headerList;
  35. projectList.forEach((item,index) => {
  36. let showItem = {
  37. project_name: data.rec[index][item],
  38. totalAmount : 0,
  39. }
  40. data.list.forEach(dataItem => {
  41. showItem[dataItem.acc_id + "_header"] = dataItem[item];
  42. showItem.totalAmount += dataItem[item];
  43. });
  44. showItem.totalAmount = showItem.totalAmount.toFixed(2);
  45. tableList.push(showItem);
  46. tableList.forEach((subItem,index) => {
  47. if(index == 0){
  48. subItem.name = '期初资金'
  49. }else if(index > 0 && index < 12){
  50. subItem.name = '收入'
  51. }else if(index > 11 && index < 15){
  52. subItem.name = '支出'
  53. }else if(index == 16){
  54. subItem.name = '期末资金'
  55. }else{
  56. subItem.name = '收支差'
  57. }
  58. })
  59. });
  60. this.tableList = tableList;
  61. this.getSpanArr(this.tableList)
  62. });
  63. },
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/279849
推荐阅读