el-table合并单元格 vue3

垂直方向上合并单元格

// 具体使用data就是接口获取的值表格数据  
state.rowMergeArrs = rowMergeHandle(
   state.needMergeArr, data)
);
// :span-method="arraySpanMethod"
    
const state = reactive({
  needMergeArr: [
    {
      colName: "riskAreaId",
      mergeCheckNames: ["riskAreaId"],
    },
    {
      colName: "blameDeptName",
      mergeCheckNames: ["riskAreaId", "blameDeptName"],
    },
    {
      colName: "riskUnitId",
      mergeCheckNames: ["blameDeptName", "riskUnitId"],
    },
  ],
  rowMergeArrs: {}, // 包含需要一个或多个合并项信息的对象
});
const rowMergeHandle = (arr: any, data: any) => {
if (!Array.isArray(arr) && !arr.length) return false;
  if (!Array.isArray(data) && !data.length) return false;
  let needMerge = {};

  arr.forEach((mergeItem: any) => {
    // 创建合并管理对象
    needMerge[mergeItem.colName] = {
      rowArr: [],
      rowMergeNum: 0,
    };
    let currentMergeItemData = needMerge[mergeItem.colName];

    // 进行合并管理对象数据的遍历整理
    data.forEach((item: any, index: any) => {
      if (index === 0) {
        currentMergeItemData.rowArr.push(1);
        currentMergeItemData.rowMergeNum = 0;
      } else {
        let currentRowData = data[index];
        let preRowData = data[index - 1];

        if (
          colMergeCheck(currentRowData, preRowData, mergeItem.mergeCheckNames)
        ) {
          currentMergeItemData.rowArr[currentMergeItemData.rowMergeNum] += 1;
          currentMergeItemData.rowArr.push(0);
        } else {
          currentMergeItemData.rowArr.push(1);
          currentMergeItemData.rowMergeNum = index;
        }
      }
    });
  });
  return needMerge;
};
const colMergeCheck = (
  currentRowData: any,
  preRowData: any,
  mergeCheckNames: any
) => {
  if (!Array.isArray(mergeCheckNames) && !mergeCheckNames.length) return false;
  let result = true;
  for (let index = 0; index < mergeCheckNames.length; index++) {
    const mergeCheckName = mergeCheckNames[index];
    if (currentRowData[mergeCheckName] !== preRowData[mergeCheckName]) {
      result = false;
      break;
    }
  }
  return result;
};

const mergeAction = (val: any, rowIndex: any, colData: any) => {
  let _row = state.rowMergeArrs[val].rowArr[rowIndex];
  let _col = _row > 0 ? 1 : 0;
  return [_row, _col];
};
const arraySpanMethod = ({ row, column, rowIndex, columnIndex }: any) => {
  let needMerge = state.needMergeArr.some((item: any) => {
    return item.colName === column.property;
  });
  if (needMerge === true) {
    return mergeAction(column.property, rowIndex, column);
  }
};

参考vue2项目
https://blog.csdn.net/to_the_Future/article/details/122947219

你可能感兴趣的:(vue.js,前端,javascript)