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>
相关推荐
燃先生._.14 分钟前
Day-03 Vue(生命周期、生命周期钩子八个函数、工程化开发和脚手架、组件化开发、根组件、局部注册和全局注册的步骤)
前端·javascript·vue.js
高山我梦口香糖1 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
black^sugar2 小时前
纯前端实现更新检测
开发语言·前端·javascript
2401_857600953 小时前
SSM 与 Vue 共筑电脑测评系统:精准洞察电脑世界
前端·javascript·vue.js
2401_857600953 小时前
数字时代的医疗挂号变革:SSM+Vue 系统设计与实现之道
前端·javascript·vue.js
GDAL3 小时前
vue入门教程:组件透传 Attributes
前端·javascript·vue.js
小白学大数据3 小时前
如何使用Selenium处理JavaScript动态加载的内容?
大数据·javascript·爬虫·selenium·测试工具
轻口味4 小时前
Vue.js 核心概念:模板、指令、数据绑定
vue.js
2402_857583494 小时前
基于 SSM 框架的 Vue 电脑测评系统:照亮电脑品质之路
前端·javascript·vue.js