antd 4.x Form表单getFieldValue获取内容和清空内容

前言

1.   antd4.x版本不再支持create()的方法,所以原来的this.props.form.getFieldDecorator()的方法找不到,form没注册到props里面

2.   4.x现在只用name=“username”来代替原来的{getFieldDecorator('username', { rules: [{ required: true, message: 'Username is required!' }], })

3.   获取值的时候设置ref然后再获取form下的getFieldDecorator()方法。

代码实现如下:

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
import React from 'react';
import ReactDOM from 'react-dom';
import {Input,DatePicker,Form,Col,Button} from 'antd';
import 'antd/dist/antd.css';
import locale from 'antd/lib/date-picker/locale/zh_CN';
import 'moment/locale/zh-cn';
import moment from 'moment';
moment.locale('zh-cn');
const { RangePicker } = DatePicker;
 
class FormItem extends React.Component{
    constructor(props){
        super(props);
    }
    // 获取form的数据
    getFormData = (value)=>{
        console.log("formData:",this.refs.myForm.getFieldValue());
        // 获取form的值
        let formData = this.refs.myForm.getFieldValue();
        if(formData.beginTime){
            // 转换日期格式
            let beginTime = moment(formData.beginTime).format("YYYY-MM-DD HH:mm:ss");
        }
        if(formData.endTime){
            // 转换日期格式
            let endTime = moment(formData.endTime).format("YYYY-MM-DD HH:mm:ss");
        }
        // 清空form的数据
        this.refs.myForm.resetFields();
    }
    render(){
        debugger;
        console.log(this.props);
        // const { getFieldDecorator } = this.props.form;
        return(
            <div>
                <Form ref="myForm"
                    {...{
                        labelCol:{
                            xs:{span:24},
                            sm:{span:6},
                        },
                        wrapperCol:{
                            xs:{span:24},
                            sm:{span:18}
                        },
                    }}
                    style = {{paddingBottom:10,margin:20}} labelAlign="right" >
                        <Form.Item
                            label= "开始时间"
                            style={{width:"25%",marginRight:0}}
                            name="beginTime"
                            rules= {[
                                    {
                                      required: true,
                                      message: '请选择!',
                                    }]}
                         >
                            <DatePicker
                             showTime
                             locale={locale}
                             style={{width:195}}
                             placeholder="请选择"
                             format="YYYY-MM-DD HH:mm:ss"
                            />
                        </Form.Item>
                        <Form.Item
                            label= "结束时间"
                            style={{width:"25%",marginRight:0}}
                            name = "endTime"
                            rules= {[
                                {
                                  required: true,
                                  message: '请选择!',
                                }]}
                            >
                            <DatePicker
                             showTime
                             locale={locale}
                             style={{width:195}}
                             placeholder="请选择"
                             format="YYYY-MM-DD HH:mm:ss"
                            />
                        </Form.Item>
                        <Form.Item
                            label="姓名"
                            style={{width:"25%",marginRight:0}}
                            name = "userName"
                        >
                            <Input  placeholder="请选择" />
                        </Form.Item>
                        <Form.Item
                            style={{width:"25%",marginRight:0}}
                        >
                            <Button type="primary" htmlType="submit" onClick={this.getFormData}>
                                确定
                            </Button>
                        </Form.Item>
                </Form>
                 
            </div>
        )
    }
}
export default FormItem;

  

 

posted @   世界我快乐  阅读(9043)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)
历史上的今天:
2016-08-12 删除被其他程序占用的文件
点击右上角即可分享
微信分享提示