ProComponents EditableProTable使用

import { EditableProTable } from '@ant-design/pro-components';
import { useEffect, useState } from 'react';

const EditModal = ()=>{
	const editableFormRef = useRef();
	const [tableData, setTableData] = useState<TableDataType[]>([]);
  	const [editableKeys, setEditableRowKeys] = useState<React.Key[]>([]);
	const tableColumns: any = [
	    {
	      title: '到货量(起)',
	      dataIndex: 'productCountStart',
	      valueType: 'digit',
	      fieldProps: {
	        min: 1,
	        precision: 0,
	        style: { width: '90%' },
	      },
	      formItemProps: () => {
	        return {
	          rules: [{ required: true }],
	        };
	      },
	    },
	    {
	      title: '到货量(止)',
	      dataIndex: 'productCountEnd',
	      valueType: 'digit',
	      fieldProps: {
	        min: 1,
	        precision: 0,
	        style: { width: '90%' },
	      },
	      formItemProps: () => {
	        return {
	          rules: [{ required: true }],
	        };
	      },
	    },
	    {
	      title: '最小质检量',
	      dataIndex: 'minQualityCheckNum',
	      valueType: 'digit',
	      fieldProps: {
	        min: 1,
	        precision: 0,
	        style: { width: '90%' },
	      },
	      formItemProps: () => {
	        return {
	          rules: [{ required: true }],
	        };
	      },
	    },
	    {
	      title: '操作',
	      valueType: 'option',
	      width: 150,
	      render: (text, record, _, action) => (
	        <>
	          <Button
	            type="link"
	            size="small"
	            onClick={() => {
	              action?.startEditable?.(record.id);
	            }}
	          >
	            编辑
	          </Button>
	          <Popconfirm
	            title="已保存数据,是否确认删除"
	            cancelText="取消"
	            okText="确定"
	            onConfirm={() =>
	              setTableData(tableData.filter((item) => item.id !== record.id))
	            }
	          >
	            <Button size="small" type="link">
	              删除
	            </Button>
	          </Popconfirm>
	        </>
	      ),
	    },
	  ];
  	const onSave = () => {
	    const values = formRef.current?.getFieldsValue();
	    if (editableKeys.length) {
	      return message.warning('请先保存再提交');
	    }
	    if (!tableData.length) return message.warning('请添加');
	    API.add(values).then(() => {
	      message.success('保存成功');
	    });
	  };
  useEffect(() => {
    if (id) {
      Api.getList({ id }).then(({ data }) => {
        setTableData(data || []);
      });
    }
  }, [id]);
	return (
		<EditableProTable
			rowKey="id"
			editableFormRef={editableFormRef}
			columns={tableColumns}
			value={tableData}
			onChange={setTableData}
			recordCreatorProps={{
				record: () => ({ id: (Math.random() * 1000000).toFixed(0) }),
			}}
			editable={{
				type: 'single',
				editableKeys,
				onChange: setEditableRowKeys,
			}}
			style={{
				maxHeight: 500,
				overflow: 'hidden',
				overflowY: 'scroll',
			}}
		/>
	)
}

效果如下:
在这里插入图片描述

其他场景

1、表单联动

fieldProps:(form, { entity = {}, rowIndex }) => {
	return {
		onChange: (val) => {
			editableFormRef.current?.setRowData?.(rowIndex, {	
				otherVal: 1,//控制另一个表单项otherVal
			});
		},
	}
}
formItemProps: (form, {entity}) => {
 return {
   rules: [
     { required: !!entity.otherVal, message: '请输入' },//otherVal有值此项必填
   ],
 };
},
//或者放在render
render: (_, row, index) => {
<Button
	type="primary"
	onClick={() => {
		editableFormRef.current?.setRowData?.(index, {
			otherVal: 1,
		});
	}}
>
	点击
</Button>
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值