您的位置:首页 > 新闻 > 热点要闻 > ProFormList --复杂数据联动ProFormDependency

ProFormList --复杂数据联动ProFormDependency

2025/7/13 7:23:33 来源:https://blog.csdn.net/qq_48691693/article/details/140276319  浏览:    关键词:ProFormList --复杂数据联动ProFormDependency

 需求:

(1)数据联动:测试数据1、2互相依赖,测试数据1<=测试数据2,测试数据2>=测试数据1。

(2)点击添加按钮,添加一行。

(3)自定义操作按钮。

(4)点击自定义操作按钮(禁用),禁用当前行。

代码实现:

import { StopOutlined } from '@ant-design/icons';
import { FormListActionType, ProCard, ProForm, ProFormDependency, ProFormList, ProFormText } from '@ant-design/pro-components';
import { gte, isEmpty, lte } from 'lodash';
import { useRef, useState } from 'react';const Demo = () => {const [refresh, setRefresh] = useState<boolean>(false);const actionRef = useRef<FormListActionType<{name: string;[key: string]: any;}>>();const childrenDom = (record: Record<string, any>) => {return (<ProForm.Group key="group"><ProFormDependency name={['test2']}>{(depValues) => {return (<ProFormTextdisabled={record.disabled}width="md"name="test1"label="测试数据1"rules={[{required: true,message: '必选字段不能为空',},{pattern: /^[-+]?[0-9]+(\.[0-9]+)?$/,message: '请输入正确的数字',},{validator: async (_, value) => {if (isEmpty(value) || isEmpty(depValues.test2)) {return Promise.resolve();}if (lte(parseInt(value), parseInt(depValues.test2))) {return Promise.resolve();} else {return Promise.reject(new Error('测试数据1不能大于测试数据2'));}},},]}/>);}}</ProFormDependency><ProFormDependency key="globalUseMode" name={['test1']}>{(depValues) => {return (<ProFormTextdisabled={record.disabled}width="md"name="test2"label="测试数据2"rules={[{required: true,message: '必选字段不能为空',},{pattern: /^[-+]?[0-9]+(\.[0-9]+)?$/,message: '请输入正确的数字',},{validator: async (_, value) => {if (isEmpty(value) || isEmpty(depValues.test1)) {return Promise.resolve();}if (gte(parseInt(value), parseInt(depValues.test1))) {return Promise.resolve();} else {return Promise.reject(new Error('测试数据2不能小于测试数据1'));}},},]}/>);}}</ProFormDependency></ProForm.Group>);};return (<ProForm submitter={false}><ProFormListname={'Test'}label="Test"initialValue={[{}]}actionRef={actionRef}actionRender={(field, action, defaultActionDom, count) => {return [...defaultActionDom,<StopOutlinedkey="disable"style={{ marginLeft: '5px' }}onClick={() => {const data = actionRef.current?.get(field.name);if (data) {data.disabled = true;setRefresh(!refresh);}}}/>,];}}itemRender={({ listDom, action }, { index, record }) => (<ProCard bordered style={{ marginBlockEnd: 8 }} title={`Test${index + 1}`} extra={action} bodyStyle={{ paddingBlockEnd: 0 }}>{childrenDom(record)}</ProCard>)}/></ProForm>);
};export default Demo;

结果展示:

重点代码截图:

(1)数据联动:测试数据1、2互相依赖,测试数据1<=测试数据2,测试数据2>=测试数据1。

(2)点击添加按钮,添加一行。

(3)自定义操作按钮。

(4)点击自定义操作按钮(禁用),禁用当前行。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com