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>
相关推荐
前端拾光者10 分钟前
利用D3.js实现数据可视化的简单示例
开发语言·javascript·信息可视化
木子02041 小时前
前端VUE项目启动方式
前端·javascript·vue.js
endingCode1 小时前
45.坑王驾到第九期:Mac安装typescript后tsc命令无效的问题
javascript·macos·typescript
运维-大白同学2 小时前
将django+vue项目发布部署到服务器
服务器·vue.js·django
Myli_ing2 小时前
HTML的自动定义倒计时,这个配色存一下
前端·javascript·html
I_Am_Me_3 小时前
【JavaEE进阶】 JavaScript
开发语言·javascript·ecmascript
℘团子এ3 小时前
vue3中如何上传文件到腾讯云的桶(cosbrowser)
前端·javascript·腾讯云
学习前端的小z3 小时前
【前端】深入理解 JavaScript 逻辑运算符的优先级与短路求值机制
开发语言·前端·javascript
星星会笑滴3 小时前
vue+node+Express+xlsx+emements-plus实现导入excel,并且将数据保存到数据库
vue.js·excel·express
前端百草阁3 小时前
【TS简单上手,快速入门教程】————适合零基础
javascript·typescript