zoukankan      html  css  js  c++  java
  • 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()方法。

    代码实现如下:

    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={{"25%",marginRight:0}}
    							name="beginTime"
    							rules= {[
    									{
    									  required: true,
    									  message: '请选择!',
    									}]}
    						 >
    							<DatePicker 
    							 showTime
    							 locale={locale}
    							 style={{195}}
    							 placeholder="请选择"
    							 format="YYYY-MM-DD HH:mm:ss"
    							/>
    						</Form.Item>
    						<Form.Item 
    							label= "结束时间"
    							style={{"25%",marginRight:0}}
    							name = "endTime"
    							rules= {[
    								{
    								  required: true,
    								  message: '请选择!',
    								}]}
    							>
    							<DatePicker 
    							 showTime
    							 locale={locale}
    							 style={{195}}
    							 placeholder="请选择"
    							 format="YYYY-MM-DD HH:mm:ss"
    							/>
    						</Form.Item>
    						<Form.Item
    							label="姓名"
    							style={{"25%",marginRight:0}}
    							name = "userName"
    						>
    							<Input  placeholder="请选择" />
    						</Form.Item>
    						<Form.Item
    							style={{"25%",marginRight:0}}
    						>
    							<Button type="primary" htmlType="submit" onClick={this.getFormData}>
    							    确定
    						    </Button>
    						</Form.Item>
    				</Form>
    				
    			</div>
    		)
    	}
    }
    export default FormItem;
    

      

  • 相关阅读:
    scanf和cin的返回值
    C++ STL中Map的按Key排序跟按Value排序
    name lookup of 'res' changed for new ISO 'res' scoping
    string中c_str()、data()、copy(p,n)函数的用法
    C中malloc的使用(转)
    codeforces 653B B. Bear and Compressing(dfs)
    codeforces 653A A. Bear and Three Balls(水题)
    hdu-5646 DZY Loves Partition(贪心)
    hdu-5645 DZY Loves Balls(水题)
    codeforces 655D D. Robot Rapping Results Report(拓扑排序+拓扑序记录)
  • 原文地址:https://www.cnblogs.com/changyuqing/p/13491644.html
Copyright © 2011-2022 走看看