复杂的编辑表格

需求描述

表格可以整体编辑;也可以单行弹框编辑;且整体编辑的时候,依然可以单行编辑

编辑只能给某一列(这里是参数运行值)修改,且根据数据内容的参数范围来判断展示不同的形式:input/数字输入/单选/多选

依据判断的参数范围内容:正则:xxx / 数字范围:[1,99] / 单选:[0,1] / 多选:['a','b','cc']

typescript 复制代码
// ConfigModalEdit.tsx

import {
  ModalForm,
  ProFormText,
} from '@ant-design/pro-components';
import { Button, Form, message } from 'antd';

const waitTime = (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

export default (props) => {
  const { modalConfig, onCancel } = props
  const { visible, data } = modalConfig
  const [form] = Form.useForm<{ name: string; company: string }>();
  return (
    <ModalForm
      title="修改"
      open={visible}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => console.log('run'),
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        await waitTime(2000);
        console.log(values.name);
        message.success('提交成功');
        return true;
      }}
    >
      
      <ProFormText width="sm" name="id" label="对应的select或者text类型" />
      
    </ModalForm>
  );
};
typescript 复制代码
// index.tsx

import { EditableProTable } from '@ant-design/pro-components';
import { Button, Form, Input, InputNumber, Radio, Select } from 'antd';
import React, { useEffect, useRef, useState } from 'react';
import ConfigModalEdit from './ConfigModalEdit';

const mockDefaultData: any[] = new Array(8).fill(1).map((_, index) => {

  return {
    id: index,
    name: `主题-${index}`,
    value: index,
    name2: `fq-${index}`,
    created_at: 1590486176000,
    type: index === 0 ? 'input' : index === 2 ? 'radio' : index === 3 ? 'select' :index ===4 ? 'input' : 'multiple_select'
  }
});
const EditTable: React.FC = () => {
  const [defaultData, setDefaultData] = useState<any[]>()
  const [dataSource, setDataSource] = useState<any[]>();
  const [editableKeys, setEditableKeys] = useState<React.Key[]>([]);
  const editableRowData= useRef();
  
  const [isEditable, setIsEditable] = useState(false);
  const [modalConfig, setModalConfig] = useState<any>({
    visible: false,
    recordParams: {
      data: {}
    },
  });
  const [editForm] = Form.useForm()
  
  const validator = (value, row) => {
    console.log('value11==', value, 'row=11=', row);
   
  }

  const columns = [
    {
      title: '参数',
      dataIndex: 'name',
      editable: false,
    },
    {
      title: '',
      dataIndex: 'type',
      hideInTable: true,
      renderFormItem:()=>''
    },
    {
      title: '参数运行值',
      dataIndex: 'value',
      renderFormItem: (_, { record }) => {
        // console.log('record==', record);
        const type = record?.type;
        // 判断数据类型并返回相应的DOM元素
        if (type === 'input') {
          return <Input />;
        } else if (type === 'multiple_select') {
          return <Select mode="multiple" />;
        } else if (type === 'select') {
          return <Select />;
        }
        else if (type === 'select') {
          return <InputNumber />;
        } else {
          return <Radio.Group value={record?.value}>
            <Radio value={1}>A</Radio>
            <Radio value={2}>B</Radio>
            <Radio value={3}>C</Radio>
            <Radio value={4}>D</Radio>
          </Radio.Group>
        }
      },
      formItemProps:{
        rules: [
          {
            validator: validator,
          }
        ]
      }
    },
    {
      title: '参数默认值',
      dataIndex: 'name2',
      editable: false,
    },
    {
      title: '参数范围',
      dataIndex: 'name',
      editable: false,

    },
    {
      title: '重启生效',
      dataIndex: 'name',
      editable: false,
    },
    {
      title: '操作',
      valueType: 'option',
      render: (_: any, record: any) => {
        return [
          <Button
            type="link"
            key="EditPassword"
            onClick={() => {
              setModalConfig({
                visible: true,
                data: record
              });
            }}
          >
            修改
          </Button>,
        ];
      },
    },
  ]

  useEffect(() => {
    setDefaultData(mockDefaultData)
    setDataSource(() => mockDefaultData)
  }, []);

  const handleEdit = () => {
    setDataSource(defaultData);
    setIsEditable(true);
    setEditableKeys([...defaultData?.map(item => item.id)]);
  };
  const handleCancel = () => {
    // 清除可编辑的行键
    setEditableKeys([]);
    // 将当前正在编辑的数据设置回原始数据
    setDataSource(() => defaultData);
    setIsEditable(false);
  };

  const toolBarRender = () => {
    return isEditable ? [
      <Button
        key="save"
      // onClick={handleSave}
      >
        保存数据
      </Button>,
      <Button key="cancel" onClick={handleCancel}>
        取消
      </Button>,
    ] : [
      <Button key="edit" type="primary" onClick={handleEdit}>
        编辑
      </Button>,
    ];
  };
  return (
    <>
      <EditableProTable
        headerTitle=""
        columns={columns}
        rowKey="id"
        // name='editableData'
        value={dataSource}
        onChange={setDataSource}
        recordCreatorProps={false}
        toolBarRender={toolBarRender}
        editable={{
          type: 'multiple',
          editableKeys,
          form: editForm,
          actionRender: (row)=>[<Button
            type="link"
            key="EditPassword"
            onClick={() => {
              setModalConfig({
                visible: true,
                data: row
              });
            }}
          >
            修改
          </Button>],
          onValuesChange: (record, recordList) => {
            setDataSource(recordList);
          },
          onChange: setEditableKeys,
        }}
      />

      <ConfigModalEdit
        modalConfig={modalConfig}
        onCancel={() => {
          setModalConfig({
            visible: false,
            data: {},
          });
        }}
      />
    </>)


};
export default EditTable;
相关推荐
景天科技苑8 分钟前
【vue3+vite】新一代vue脚手架工具vite,助力前端开发更快捷更高效
前端·javascript·vue.js·vite·vue项目·脚手架工具
石小石Orz17 分钟前
Three.js + AI:AI 算法生成 3D 萤火虫飞舞效果~
javascript·人工智能·算法
小行星12519 分钟前
前端预览pdf文件流
前端·javascript·vue.js
join820 分钟前
解决vue-pdf的签章不显示问题
javascript·vue.js·pdf
小行星12526 分钟前
前端把dom页面转为pdf文件下载和弹窗预览
前端·javascript·vue.js·pdf
疯狂的沙粒28 分钟前
如何在 React 项目中应用 TypeScript?应该注意那些点?结合实际项目示例及代码进行讲解!
react.js·typescript
Lysun00135 分钟前
[less] Operation on an invalid type
前端·vue·less·sass·scss
土豆湿42 分钟前
拥抱极简主义前端开发:NoCss.js 引领无 CSS 编程潮流
开发语言·javascript·css
J总裁的小芒果1 小时前
Vue3 el-table 默认选中 传入的数组
前端·javascript·elementui·typescript
Lei_zhen961 小时前
记录一次electron-builder报错ENOENT: no such file or directory, rename xxxx的问题
前端·javascript·electron