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 分钟前
React中的stopPropagation和preventDefault
前端·javascript·react.js
天天向上10245 分钟前
vue3 抽取el-dialog子组件
前端·javascript·vue.js
lecepin10 分钟前
AI Coding 资讯 2025-11-05
前端·javascript
excel13 分钟前
Vue 模板解析器 parserOptions 深度解析
前端
前端小咸鱼一条18 分钟前
17.React获取DOM的方式
前端·javascript·react.js
excel20 分钟前
Vue 编译核心中的运行时辅助函数注册机制详解
前端
excel20 分钟前
🌿 深度解析 Vue DOM 编译器模块源码:compile 与 parse 的构建逻辑
前端
excel22 分钟前
深度解析 Vue 编译器中的 transformShow:v-show 指令的编译原理
前端
excel22 分钟前
深度解析:decodeHtmlBrowser —— 浏览器端 HTML 解码函数设计
前端
excel23 分钟前
深度解析:Vue 模板编译器中的 transformVText 实现原理
前端