index.jsx 3.25 KB
Newer Older
wuhao's avatar
wuhao committed
1
import React, { useEffect, useRef } from "react";
wuhao's avatar
wuhao committed
2
import { EditableProTable } from "@ant-design/pro-table";
wuhao's avatar
wuhao committed
3 4
import { useUpdate } from 'ahooks';

wuhao's avatar
wuhao committed
5 6 7 8 9 10 11 12

const EditTable = ({
  columns,
  value,
  onChange,
  rowKey,
  recordCreatorProps,
  maxLength,
wuhao's avatar
wuhao committed
13 14
  linkconfig,
  style
wuhao's avatar
wuhao committed
15
}) => {
wuhao's avatar
wuhao committed
16 17 18 19 20 21 22 23 24 25
  const update = useUpdate();
  const editorFormRef = useRef();
  
  useEffect(()=>{
    if(value===undefined){
      update()
    }
  },[value])
  console.log(columns[0]);

wuhao's avatar
wuhao committed
26 27
  return (
    <EditableProTable
wuhao's avatar
wuhao committed
28
      style={{ ...(style ?? {}) }}
wuhao's avatar
wuhao committed
29 30 31 32 33 34 35 36
      columns={columns}
      rowKey={rowKey}
      value={value || []}
      onChange={onChange}
      recordCreatorProps={
        recordCreatorProps == "false"
          ? false
          : {
wuhao's avatar
wuhao committed
37 38 39 40 41
            newRecordType: "dataSource",
            record: () => ({
              [rowKey]: Date.now(),
            }),
          }
wuhao's avatar
wuhao committed
42
      }
wuhao's avatar
wuhao committed
43
      editableFormRef={editorFormRef}
wuhao's avatar
wuhao committed
44 45 46 47 48 49
      editable={{
        type: "multiple",
        editableKeys: value ? value.map((item) => item[rowKey]) : [],
        actionRender: (row, config, defaultDoms) => {
          return [defaultDoms.delete];
        },
wuhao's avatar
wuhao committed
50 51 52 53 54 55 56 57 58 59 60 61
        onValuesChange: async (record, recordList) => {
          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 => {
左玲玲's avatar
左玲玲 committed
62 63 64
                  // freshvals[its] = value[i][its];
                  freshvals[its] = it[its];

wuhao's avatar
wuhao committed
65 66 67 68 69 70 71 72 73 74 75 76 77
                })
                it = {
                  ...record,
                  ...freshvals
                }
              } else {
                it = value && value.length > 0 ? value.filter(its => its[rowKey] == it[rowKey])[0] : {}
              }
              return it
            });

            //参数获取
            let parames = {};
wuhao's avatar
wuhao committed
78
            let ifs = true;
wuhao's avatar
wuhao committed
79 80 81
            Object.keys(params).map(it => {
              if (params[it] == "linked") {
                parames[it] = record[it]
wuhao's avatar
wuhao committed
82 83
                if (record[it] != curvaluerow[it]) {
                  ifs = false;
wuhao's avatar
wuhao committed
84 85 86 87 88 89 90 91 92 93 94 95
                }
              } else {
                parames[it] = params[it]
              }
            })
            if (ifs) {
              //值未变化
            } else {
              let res = await database(parames);
              newvalue = newvalue.map((it, i) => {
                if (it[rowKey] == record[rowKey]) {
                  Object.keys(effectresult).map(items => {
wuhao's avatar
wuhao committed
96 97
                    let result = res?.data?.data ?? {}
                    it[items] = result[effectresult[items]];
wuhao's avatar
wuhao committed
98
                  })
wuhao's avatar
wuhao committed
99 100
                  editorFormRef.current?.setRowData?.(i, it)
                } else {
wuhao's avatar
wuhao committed
101 102 103 104 105 106 107
                }
                return it
              })
            };

          }
          onChange(newvalue);
wuhao's avatar
wuhao committed
108 109 110 111 112 113 114 115
        },
      }}
      maxLength={maxLength ?? 1000}
    />
  );
};

export default EditTable;