index.jsx 3.7 KB
Newer Older
wuhao's avatar
wuhao committed
1
import React, { useEffect, useMemo, useRef, useState } from "react";
wuhao's avatar
wuhao committed
2
import { EditableProTable } from "@ant-design/pro-table";
krysent's avatar
krysent committed
3
import { useUpdate } from "ahooks";
wuhao's avatar
wuhao committed
4 5 6 7 8 9 10 11

const EditTable = ({
  columns,
  value,
  onChange,
  rowKey,
  recordCreatorProps,
  maxLength,
wuhao's avatar
wuhao committed
12
  linkconfig,
krysent's avatar
krysent committed
13
  style,
wuhao's avatar
wuhao committed
14
}) => {
wuhao's avatar
wuhao committed
15
  const editorFormRef = useRef();
wuhao's avatar
wuhao committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
  //console.log(columns);
  const [addon, setaddon] = useState(0);

  const newcolumns = useMemo(() => {
    return columns?.map((item) => {
      return item?.params
        ? {
            ...item,
            params: {
              ...item.params,
              addon,
            },
          }
        : {
            ...item,
          };
    });
  }, [addon]);
wuhao's avatar
wuhao committed
34

wuhao's avatar
wuhao committed
35
  return (
wuhao's avatar
wuhao committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    <EditableProTable
      style={{ ...(style ?? {}) }}
      columns={newcolumns}
      rowKey={rowKey}
      value={value || []}
      onChange={onChange}
      recordCreatorProps={
        recordCreatorProps == "false"
          ? false
          : {
              newRecordType: "dataSource",
              record: () => ({
                [rowKey]: Date.now(),
              }),
            }
      }
      editableFormRef={editorFormRef}
      editable={{
        type: "multiple",
        editableKeys: value ? value.map((item) => item[rowKey]) : [],
        actionRender: (row, config, defaultDoms) => {
          return [defaultDoms.delete];
        },
        onValuesChange: async (record, recordList) => {
          setaddon(s=>Math.random()*1000000);
          let { urlchangeval } = linkconfig ?? {};
          let newvalue = [...recordList];
          if (urlchangeval && record) {
            //根据url 改变 数据值
            let { params, database, effectresult } = urlchangeval ?? {},
              curvaluerow =
                value && value.length > 0
                  ? value.filter((it) => it[rowKey] == record[rowKey])[0]
                  : {};
            //获取除本行之外已修改的value值
            newvalue = newvalue.map((it, i) => {
              if (!record) return it;
              if (it[rowKey] == record[rowKey]) {
                let freshvals = {};
                Object.keys(effectresult).map((its) => {
                  // freshvals[its] = value[i][its];
                  freshvals[its] = it[its];
                });
                it = {
                  ...record,
                  ...freshvals,
                };
              } else {
                it =
krysent's avatar
krysent committed
85
                  value && value.length > 0
wuhao's avatar
wuhao committed
86
                    ? value.filter((its) => its[rowKey] == it[rowKey])[0]
krysent's avatar
krysent committed
87
                    : {};
wuhao's avatar
wuhao committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
              }
              return it;
            });

            //参数获取
            let parames = {};
            let ifs = true;
            Object.keys(params).map((it) => {
              if (params[it] == "linked") {
                parames[it] = record[it];
                if (record[it] != curvaluerow[it]) {
                  ifs = false;
                }
              } else {
                parames[it] = params[it];
              }
            });
            if (ifs) {
              //值未变化
            } else {
              let res = await database(parames);
wuhao's avatar
wuhao committed
109 110
              newvalue = newvalue.map((it, i) => {
                if (it[rowKey] == record[rowKey]) {
wuhao's avatar
wuhao committed
111 112 113
                  Object.keys(effectresult).map((items) => {
                    let result = res?.data?.data ?? {};
                    it[items] = result[effectresult[items]];
krysent's avatar
krysent committed
114
                  });
wuhao's avatar
wuhao committed
115
                  editorFormRef.current?.setRowData?.(i, it);
wuhao's avatar
wuhao committed
116
                } else {
wuhao's avatar
wuhao committed
117
                }
krysent's avatar
krysent committed
118 119 120
                return it;
              });
            }
wuhao's avatar
wuhao committed
121 122 123 124 125 126
          }
          onChange(newvalue);
        },
      }}
      maxLength={maxLength ?? 1000}
    />
wuhao's avatar
wuhao committed
127 128 129 130
  );
};

export default EditTable;