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,
          };
        }
      }
    },
相关推荐
你的人类朋友35 分钟前
【Node&Vue】JS是编译型语言还是解释型语言?
javascript·node.js·编程语言
烛阴43 分钟前
TypeScript高手密技:解密类型断言、非空断言与 `const` 断言
前端·javascript·typescript
样子20182 小时前
Uniapp 之renderjs解决swiper+多个video卡顿问题
前端·javascript·css·uni-app·html
Nicholas682 小时前
flutterAppBar之SystemUiOverlayStyle源码解析(一)
前端
黑客飓风2 小时前
JavaScript 性能优化实战大纲
前端·javascript·性能优化
emojiwoo3 小时前
【前端基础知识系列六】React 项目基本框架及常见文件夹作用总结(图文版)
前端·react.js·前端框架
张人玉4 小时前
XML 序列化与操作详解笔记
xml·前端·笔记
杨荧4 小时前
基于Python的宠物服务管理系统 Python+Django+Vue.js
大数据·前端·vue.js·爬虫·python·信息可视化
YeeWang5 小时前
🎉 Eficy 让你的 Cherry Studio 直接生成可预览的 React 页面
前端·javascript
gnip5 小时前
Jenkins部署前端项目实战方案
前端·javascript·架构