copyRow

import { omit, cloneDeep } from 'lodash';
interface Row {
  property: string;
  type: string;
  23699: string | null;
  24116: string | null;
  24373: string | null;
  25642: string | null;
  24759: string | null;
}

const cells = [23699, 24116, 24373, 24373, 25642, 24759];

class Comp {
  highlightRow1: Row = {
    23699: null,
    24116: null,
    24373: null,
    24759: null,
    25642: null,
    property: 'a',
    type: 'string',
  };
  highlightRow2: Row = {
    23699: null,
    24116: null,
    24373: null,
    24759: null,
    25642: null,
    property: 'b',
    type: 'string',
  };

  // 出事渲染
  init(rowData: Row[]) {
    rowData.forEach((row) => {
      if (row.property === this.highlightRow1.property) {
        this.highlightRow1 = cloneDeep(row);
      }
      if (row.property === this.highlightRow2.property) {
        this.highlightRow2 = cloneDeep(row);
      }
    });
  }

  emit(val: any) {
    console.log(val);
  }

  change(data: { property: string; data: Row }) {
    const { property, data: row } = data;
    if (property === this.highlightRow1.property) {
      this.highlightRow1 = Object.assign(
        this.highlightRow1,
        omit(cloneDeep(row), ['property', 'type'])
      );
    } else {
      this.highlightRow2 = Object.assign(
        this.highlightRow2,
        omit(cloneDeep(row), ['property', 'type'])
      );
    }
  }

  rowParam: [prev: string, cur: string] = ['a', 'b'];

  copyAM(rowData: Row[]) {
    const [__, curProperty] = this.rowParam;

    // 判断是否是黄色第一行
    const isRowOne = () => curProperty === this.highlightRow1.property;

    // 上一行
    let lastRow: Row | undefined;

    // 处理黄色第一行
    if (isRowOne()) {
      // 拿到黄色上一行
      const currentIndex = rowData.findIndex(
        (item) => item.property === this.highlightRow1.property
      );
      lastRow = rowData[currentIndex - 1];

      // 把上一行的数字属性的值,复制到黄色第一行
      this.highlightRow1 = Object.assign(
        this.highlightRow1,
        omit(cloneDeep(lastRow), ['property', 'type'])
      );

      cells.forEach((cellId) => {
        this.emit({
          id: cellId,
          property: this.highlightRow1.property,
          value: this.highlightRow1[cellId as keyof Row],
        });
      });
    } else {
      // 处理黄色第二行
      this.highlightRow2 = Object.assign(
        this.highlightRow2,
        omit(cloneDeep(this.highlightRow1), ['property', 'type'])
      );
      cells.forEach((cellId) => {
        this.emit({
          id: cellId,
          property: this.highlightRow2.property,
          value: this.highlightRow2[cellId as keyof Row],
        });
      });
    }
  }
}
console.log(Comp);

export default {};

你可能感兴趣的:(copyRow)