elementui的table根据是否符合需求合并列

javascript 复制代码
 <el-table :data="tableData" border style="width: 100%;" :span-method="objectSpanMethodAuto">
          <!-- 空状态 -->
          <template slot="empty">
            <div><img src="@/assets/images/noData.png" /></div>
            <span>暂无数据</span>
          </template>
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column label="年度" align="center" prop="year">
          </el-table-column>
          <el-table-column prop="regionName" label="行政区划" align="center">
          </el-table-column>
          <el-table-column align="center">
            <template slot="header">
              <div class="header-con">(万人)</div>
              <div class="header-name">农业人口数量</div>
            </template>
            <template slot-scope="scope">
              <div>{{ scope.row.ruralPopNum }}</div>
            </template>
          </el-table-column>
          <el-table-column align="center">
            <template slot="header">
              <div class="header-con">(万人)</div>
              <div class="header-name">城镇人口数量</div>
            </template>
            <template slot-scope="scope">
              <div>{{ scope.row.urbanPopNum }}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="{ row }">
              <el-button type="text" size="small" class="operation" style="color: #2372ed;" @click="handleEditTable(row)">编辑</el-button>
              <el-button type="text" size="small" class="operation" style="color: #2372ed;" @click="handleDelete(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
javascript 复制代码
data: function () {
      return {
       spanArr:[],
       tableData:[]
	}
}
javascript 复制代码
getList() {
      getPopList(this.query).then((res) => {
        this.totalCount = res.total;
        this.tableData = res.rows;
        let contactDot = 0;
        let spanArr = [];
        this.tableData.forEach((item, index) => {
          if (index === 0) {
            console.log(spanArr);
            spanArr.push(1);
          } else {
            if (item.year === this.tableData[index - 1].year) {
              spanArr[contactDot] += 1;
              spanArr.push(0);
            } else {
              contactDot = index;
              spanArr.push(1);
            }
          }
        });
        this.spanArr = spanArr;
      });
    },
javascript 复制代码
  // 合并行
    objectSpanMethodAuto({ row, column, rowIndex, columnIndex }) {
      console.log("点击:", row, column, rowIndex, columnIndex);
      if (columnIndex == 1 || columnIndex == 5) {
        if (this.spanArr[rowIndex]) {
          return {
            rowspan: this.spanArr[rowIndex],
            colspan: 1,
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0,
          };
        }
      }
    },
相关推荐
橙子家3 小时前
浏览器缓存之【基础键值存储】:Local storage 和 Session storage
前端
星星在线5 小时前
MusicFree:一个「All in One」的个人音乐服务器,让听歌回归简单
前端·后端
IT_陈寒6 小时前
Redis的SETNX并发问题让我加了三天班
前端·人工智能·后端
demo007x7 小时前
Docling 文档转换以及技术架构分析
前端·后端·程序员
京东云开发者7 小时前
京东市民服务又“上新”!这次是黑龙江“龙易办”
前端
袋鱼不重8 小时前
我的神奇同事,AI 用多了居然写了个 Open In Codex
前端·后端·ai编程
竹林8188 小时前
Web3表单签名验证:我用 wagmi 和 ethers 给 DApp 加了一个“免密登录”,踩坑记录全在这了
javascript
用户6990304848758 小时前
try catch使用场景 处理同步代码错误兼容用的
javascript·uni-app
雪碧聊技术8 小时前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
Fireworks8 小时前
深入vue3源码解读 -- 1、响应式的基础概念
前端