index.jsx 4.69 KB
Newer Older
wangjing2's avatar
wangjing2 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
/* eslint-disable react-hooks/exhaustive-deps */
/* eslint-disable react-hooks/rules-of-hooks */
import React, { useEffect, useRef, useState, memo, useMemo } from 'react';
import { EditableProTable } from '@ant-design/pro-components';
import { Tooltip } from 'antd';
import { doFetch } from '@/utils/doFetch';

const EditTable = (props) => {
  const {
    formRef, //表单Ref
    rowKey, // key
    columns = [], //columns
    style, //style
    path, //接口地址
    extraparams, //额外参数
    pageSize, //修改默认pageSize
    pagination, //分页设置
    x, //横向滚动
    refreshDep, //依赖刷新 (已废弃)
    getDefaultSelected, //存在默认选中向上返回选中值
    dataSource,
  } = props;



  let actionRefs = useRef(),
    formRefs = formRef ?? useRef(),
    ifspagination = pagination == 'false' || pagination === false,
    [size, setsize] = useState('small');

  //调用接口
  const request = async (params, sort, filter) => {
    if (!path)
      return {
        data: dataSource ?? [],
        success: true,
        total: dataSource?.length ?? 0,
      };
    // console.log('====================================');
    // console.log(extraparams);
    // console.log('====================================');
    let newparams = {
      ...params,
      ...extraparams, //父组件传参
      pageIndex: params.current,
      pageSize: params.pageSize || pageSize,
    };
    delete newparams.current;
    if (ifspagination) {
      delete newparams.pageIndex;
      delete newparams.pageSize;
    }
    const result = await doFetch({ url: path, params: newparams });
    //分页结果
    let data = result?.data?.page?.list,
      success = true,
      total = result?.data?.page?.total;
    //不带分页获取结果
    if (ifspagination || !data) {
      data = result?.data?.dataList;
      total = result?.data?.dataList?.length;
    }
    //存在默认选中向上返回选中值
    getDefaultSelected && getDefaultSelected(result?.data);
    return {
      data,
      success,
      total,
    };
  };

  let columncs = useMemo(() => {
    return columns.map((item, index) => {
      let it = { ...item };
      let itemwidth = it.width ? it.width : 'auto';
      let options = {};
      if (it.valueType == 'select' || it.valueType == 'checkbox') {
        if (Array.isArray(it.options)) {
          options = {
            fieldProps: {
              ...it?.fieldProps,
              options: [...it.options],
            },
          };
        } else if (it.options) {
          options = {
            request: async (params) => {
              let list = await doFetch({ url: it?.options?.path, params: it?.options?.params });
              return list.data.dataList;
            },
          };
        }
      }
      if (it.valueType == 'option') {
        options = {
          key: 'option',
          dataIndex: 'option',
          fixed: 'right',
        };
      }
      if (!it.render) {
        options = {
          ...options,
          render: (text, row) => {
            return (
              <Tooltip title={row[it.dataIndex]} placement="topLeft">
                <span className="table-cell">{row[it.dataIndex] ?? '-'}</span>
              </Tooltip>
            );
          },
        };
      }

      options = {
        ...options,
        width: itemwidth,
      };

      delete it.formItemProps;
      return {
        ...it,
        ...options,
      };
    });
  }, [columns]);

  return (
    <EditableProTable
      {...props}
      recordCreatorProps={false}
      size={size}
      params={{ ...extraparams }}
      onSubmit={(params) => {
        //console.log(params, 'onSubmit');
      }}
      onSizeChange={(size) => {
        localStorage.setItem('size', size); //设置全局表格规格缓存
        setsize(size);
      }}
      columns={columncs ?? []}
      style={style || {}}
      actionRef={actionRefs}
      formRef={formRefs}
      rowKey={rowKey ?? 'id'} //表格每行数据的key
      dateFormatter="string"
      request={request}
      scroll={
        x
          ? {
              x: x,
            }
          : {}
      }
      pagination={
        ifspagination
          ? false
          : {
              showTotal: (total, range) => <span>{total}</span>,
              showQuickJumper: true,
              showSizeChanger: true,
              pageSizeOptions: [5, 10, 15, 30, 50, 100, 200],
              defaultPageSize: pageSize || 15,
            }
      }
      editable={{
        type: 'multiple',
        editableKeys: props?.rowSelection?.selectedRowKeys ?? [],
        ...props?.editable,
      }}
      search={{
        filterType: 'light', //轻量模式
        placement: 'bottomLeft',
      }}
    />
  );
};

export default memo(EditTable);