Antdesign Form 实现页面控件的赋值加载

本文详细介绍了在AntDesign中如何为表单的Checkbox组件设置默认值。通过使用valuePropName属性,可以实现在页面加载时从后台获取数据并正确显示Checkbox的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Antdesign Form时,当页面加载时,需要从后台获取数据,对Form中控件的默认赋值。看似比较简单的需求,而且Antdesign 官方文档中也有相应介绍,然后对于Form 的CheckBox 的默认值加载,文档中并没有涉及。

需求: 当页面记载时,从后台获取当前Form 对象值,然后将值加载到控件中。如下图,

 Form控件定义方式如下, 官网中案例使用Form.create方法创建一个对象,mapPropsToFields方法完成对控件赋初值,对于Input, Select等控件是完全没问题,而对于checkbox 控件就无法生效了。

<FormItem label="Primary Keys" hasFeedback>
    {getFieldDecorator('primayKey', {rules: [{ required: true, message: 'Please Input Primay Key!' }],})(
   <Input placeholder="Please Input Primay Key" />)}
</FormItem>
<FormItem {...tailFormItemLayout}>
    {getFieldDecorator('isIgnoreFirstRow')(
      <Checkbox >Ignore First Row</Checkbox>
    )}
</FormItem>
const ingform = Form.create({ 
    name: 'ingestion_form' ,
    mapPropsToFields(props) {
        return {
            primayKey:Form.createFormField({
                ...props.stepObj,
                value:props.stepObj.paramMap.source_primary_keys
            }),
            ignoreFirstRow:Form.createFormField({
                ...props.stepObj,
                 value:props.stepObj.paramMap.source_is_ignore_first
            })

        }
    }
})(IngestionForm);

 察看了相关源码,不难发现,其实源码中有对其一定的描述,需要使用valuePropName对CheckBox 设置Checked状态。

 

所以将关于Checkbox 代码稍作修改,并且删除mapPropsToFields方法中定义Checkbox filed 代码段,就能正常工作了,而在form 中获取该字段的方法仍然和其他控件一样。

<FormItem {...tailFormItemLayout}>
                        {getFieldDecorator('isIgnoreFirstRow',{ 
                            valuePropName: 'checked',initialValue:stepObj.paramMap.source_is_ignore_first || false, })(
                        <Checkbox >Ignore First Row</Checkbox>
                        )}
</FormItem>

 

转载于:https://www.cnblogs.com/bradwarden/p/11346101.html

我的代码运行失败,需求为探访方式下拉框选择第一个选项,而非特定文本,网页元素为:探访方式的网页元素为:<div class="ant-col ant-form-item-control css-d2e3vi"><div class="ant-form-item-control-input"><div class="ant-form-item-control-input-content"><div class="ant-select ant-select-in-form-item css-d2e3vi ant-select-single ant-select-allow-clear ant-select-show-arrow" aria-required="true"><div class="ant-select-selector"><span class="ant-select-selection-search"><input type="search" id="VisitHeaderinfo-sp-49638_visit_method" autocomplete="off" class="ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="VisitHeaderinfo-sp-49638_visit_method_list" aria-autocomplete="list" aria-controls="VisitHeaderinfo-sp-49638_visit_method_list" aria-activedescendant="VisitHeaderinfo-sp-49638_visit_method_list_1" aria-required="true" readonly="" unselectable="on" value="" aria-expanded="false" style="opacity: 0;"></span><span class="ant-select-selection-placeholder">请选择探访方式</span></div><span class="ant-select-arrow" unselectable="on" aria-hidden="true" style="user-select: none;"><span role="img" aria-label="down" class="anticon anticon-down ant-select-suffix"><svg viewBox="64 64 896 896" focusable="false" data-icon="down" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3.1-12.7-6.4-12.7z"></path></svg></span></span></div></div></div></div>
03-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值