el-table实现固定列相同合并切重排序号

效果图:
如何实现:
TypeScript 复制代码
<template>
  <div>
    <el-table
      :data="tableData"
      border
      :span-method="objectSpanMethod"
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column type="index" :index="getIndex" label="序号" width="80" />
      <el-table-column prop="resourceTypeName" label="需求类型" width="180" />
      <el-table-column prop="resourceName" label="属地需求" />
      <el-table-column prop="resourceNum" label="需求数量" />
      <el-table-column prop="dispatchNum" label="AI辅助建议" />
    </el-table>
  </div>
</template>
<script setup>
import {
  onBeforeUnmount,
  onMounted,
  getCurrentInstance,
  ref,
  watch,
  nextTick,
} from "vue";

const currentIndex = ref(1);
const tableData = ref([
  {
    resourceType: "1",
    resourceTypeName: "专家",
    resourceCode: "resource_gw_hfssjdccz",
    resourceName: "XXXX内容2",
    resourceNum: "1",
    dispatchNum: "0",
  },
  {
    resourceType: "1",
    resourceTypeName: "专家",
    resourceCode: "resource_gw_swzdsjdccz",
    resourceName: "XXXX内容1",
    resourceNum: "1",
    dispatchNum: "0",
  },
  {
    resourceType: "1",
    resourceTypeName: "专家",
    resourceCode: "resource_gw_tfjxcrbdccz",
    resourceName: "XXXX内容3",
    resourceNum: "2",
    dispatchNum: "8",
  },
  {
    resourceType: "3",
    resourceTypeName: "物资",
    resourceCode: "supp_material_type_1_1_1_1",
    resourceName: "XXXX内容4",
    resourceNum: "1",
    dispatchNum: "297191",
  },
  {
    resourceType: "3",
    resourceTypeName: "物资",
    resourceCode: "supp_material_type_1_1_1_1",
    resourceName: "XXXX内容9",
    resourceNum: "1",
    dispatchNum: "297191",
  },
]);

const objectSpanMethod = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex === 1) {
    // 如果当前值和上一行的值相同,则将当前单元格隐藏
    if (row.resourceType === tableData.value[rowIndex - 1]?.resourceType) {
      return { rowspan: 0, colspan: 0 };
    } else {
      // 否则计算当前单元格应该跨越多少行
      let rowspan = 1;
      for (let i = rowIndex + 1; i < tableData.value.length; i++) {
        if (tableData.value[i].resourceType === row.resourceType) {
          rowspan++;
        } else {
          break;
        }
      }
      return { rowspan, colspan: 1 };
    }
  }
};


const getIndex = (index) => {
  const resourceType = tableData.value[index].resourceType;
  let counter = 0;
  for (let i = 0; i < index; i++) {
    if (tableData.value[i].resourceType === resourceType) {
      counter++;
    }
  }
  return currentIndex.value + counter;
};
</script>
相关推荐
我命由我12345几秒前
CesiumJS 案例 P13:删除标记、移动标记、标记点击事件
前端·javascript·前端框架·html·css3·html5·js
码不停T35 分钟前
乾坤项目学习总结
vue.js·qiankun·乾坤
retun_true1 小时前
记录生活之JSON.parse坑 -- 【JavaScript】
前端·javascript
customer081 小时前
【开源免费】基于SpringBoot+Vue.J影城管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·开源
jason_yang1 小时前
vue3复习-源码-迷你版vite
vue.js·vite
正小安1 小时前
JavaScript对象的深拷贝和浅拷贝 - 2024最新版前端秋招面试短期突击面试题【100道】
前端·javascript·面试
jason_yang2 小时前
vue3复习-源码-编译原理-自定义vite插件
vue.js·vite
清灵xmf2 小时前
JavaScript 中如何识别异步函数?
开发语言·javascript·async·异步函数
热爱前端的小君同学2 小时前
实现插入公式的富文本框(tinymce+kityformula-editor)
vue.js
计算机学姐3 小时前
基于协同过滤算法的旅游网站推荐系统
vue.js·mysql·算法·mybatis·springboot·旅游·1024程序员节