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>
相关推荐
约定Da于配置4 小时前
uniapp封装websocket
前端·javascript·vue.js·websocket·网络协议·学习·uni-app
大叔_爱编程4 小时前
wx030基于springboot+vue+uniapp的养老院系统小程序
vue.js·spring boot·小程序·uni-app·毕业设计·源码·课程设计
计算机学姐6 小时前
基于微信小程序的驾校预约小程序
java·vue.js·spring boot·后端·spring·微信小程序·小程序
村口蹲点的阿三6 小时前
Spark SQL 中对 Map 类型的操作函数
javascript·数据库·hive·sql·spark
noravinsc7 小时前
python md5加密
前端·javascript·python
cafehaus8 小时前
抛弃node和vscode,如何用记事本开发出一个完整的vue前端项目
前端·vue.js·vscode
微光无限9 小时前
Vue3 中使用组合式API和依赖注入实现自定义公共方法
前端·javascript·vue.js
GISer_Jing9 小时前
React+AntDesign实现类似Chatgpt交互界面
前端·javascript·react.js·前端框架
家里有只小肥猫10 小时前
虚拟mock
vue.js
智界工具库10 小时前
【探索前端技术之 React Three.js—— 简单的人脸动捕与 3D 模型表情同步应用】
前端·javascript·react.js